0

So I'm using a csv file to analyze heart failure info. I'm able to extract all the ages using the following code:

import csv
from plotly.graph_objs import Bar, Layout
from plotly import offline

filename3 = 'heart.csv'
decade_30 = []
decade_40 = []
decade_50 = []
decade_60 = []
decade_70 = []

with open(filename3) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    for row in reader:
        age = int(row[0])
        if age < 40:
            decade_30.append(age)
        elif age > 50:
            decade_40.append(age)
        elif age > 60:
            decade_50.append(age)
        else:
            decade_60.append(age)

x_values = [1,2,3,4,5,]
y_values = [len(decade_30), len(decade_40), len(decade_50), len(decade_60),len(decade_70)]
data = [Bar(x=x_values, y=y_values)]

x_axis_config = {'title': 'Age Range'}
y_axis_config = {'title': 'Deaths out of 918'}
my_layout = Layout(title='Ages of Heart Failure Victims', xaxis=x_axis_config, yaxis=y_axis_config)
offline.plot({'data':data, 'layout': 'my_layout'}, filename='d6.html')

This returns:

ValueError: 
    Invalid value of type 'builtins.str' received for the 'layout' property of 
        Received value: 'my_layout'

At first glace, can anyone tell me what's wrong with my_layout. I just finished the Data Visualization chapter of Python Crash Course. This is the first bar graph I've attempted to make on my own so I just tried emulating what I did with the bar graph on that project.

1
  • Could you also tell me how to label the x-axis items. Instead of the 0, 1, 2, 3, 4,? Commented Dec 28, 2021 at 19:53

1 Answer 1

2

You are passing the string 'my_layout' instead of the object my_layout. The correct line should be:

offline.plot({'data':data, 'layout': my_layout}, filename='d6.html')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that! I should of caught that. Could you also tell me how to label the x-axis items. Instead of the default 0, 1, 2, 3, 4,?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.