Frank_Synopsis
Frank_Synopsis

Reputation: 21

How to plot x-axis range values in plotly?

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.

Upvotes: 0

Views: 216

Answers (1)

Jack Rickman
Jack Rickman

Reputation: 76

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')

Upvotes: 2

Related Questions