我没想到会出现这个错误(“AttributeError:module'plotly'没有属性'plot'”)并且无法找到确切的错误。我认为这不是确切的错误,因为情节明显具有绘图能力,并且在我的数据未正确格式化的某个地方,对于这个特定的练习。
我愿意接受有关新方法的建议。这正是我所使用的,因为它很容易遵循,最终我想要的东西,以及它的集中化。
最后一行py.plot( fig, filename='d3-cloropleth-map' )
我复制了示例中的代码:United States Choropleth Map
这是我的代码:
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' )
答案 0 :(得分:4)