I was not expecting this error ("AttributeError: module 'plotly' has no attribute 'plot'") and have not been able to find the exact error. I am thinking its not the exact error because plotly obviously has plotting abilities and that somewhere along the way my data isnt formatted correctly, for this particular exercise.
I am open to suggestions on new methods. This is just what I used because it was easy to follow, its what I want in the end, and its centralized.
Error occurs on last line py.plot( fig, filename='d3-cloropleth-map' )
I have copied the code from the example: United States Choropleth Map
And here is my code:
import plotly as py
import pandas as pd
import numpy as np
py.tools.set_credentials_file(username='user', api_key='xxxXxxXxxx')
df = pd.DataFrame.from_csv("C:/Users/d/Documents/Personal/Python Scripts/Python/Connect.csv")
for col in df.columns:
df[col] = df[col].astype(str)
df[['Open Balance','Amount', 'Aging']] = df[['Open Balance','Amount',
'Aging']].apply(pd.to_numeric, errors='ignore')
df[['Date', 'Due Date']] = df[['Date','Due Date']].apply(pd.to_datetime)
state_total_byitem = df.groupby(by = ['State', 'Item']).agg({'Open Balance':'sum','Amount':'sum','Paid':'count','Aging':'mean'})
sti = state_total_byitem
sti.reset_index(level=['State', 'Item'], inplace=True)
for col in sti.columns:
sti[col] = sti[col].astype(str)
sti['text'] = 'State ' + sti['State'] + ' Item ' + sti['Item'] + '<br>' +\
' Open Balance ' + sti['Open Balance'] + ' Paid ' + sti['Paid'] + '<br>' +\
' Amount ' + sti['Amount'] + ' Aging ' + sti['Aging']
scl = [[0.0, 'rgb(220,224,225)'],[0.2, 'rgb(204,220,224)'],[0.4, 'rgb(158,192,200)'],\
[0.6, 'rgb(100,166,184)'],[0.8, 'rgb(60,175,206)'],[1.0, 'rgb(10,206,255)']]
data = [ dict(
type='choropleth',
colorscale = scl,
autocolorscale = False,
locations = sti['State'],
z = sti['Amount'].astype(float),
locationmode = 'USA-states',
text = sti['text'],
marker = dict(
line = dict (
color = 'rgb(255,255,255)',
width = 2
) ),
colorbar = dict(
title = "$ in USD")
) ]
layout = dict(
title = 'Invoices by State<br>(Hover for breakdown)',
geo = dict(
scope='usa',
projection=dict( type='albers usa' ),
showlakes = True,
lakecolor = 'rgb(255, 255, 255)'),
)
fig = dict( data=data, layout=layout )
py.plot( fig, filename='d3-cloropleth-map' )
Instead of
import plotly as py
You should have
import plotly.plotly as py
For future reference, try to get a MVCE before posting. Oftentimes it will help you find the error on your own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With