AttributeError:模块'plotly'没有属性'plot'

时间:2017-05-02 16:28:14

标签: python-3.x jupyter-notebook plotly

我没想到会出现这个错误(“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' )

1 个答案:

答案 0 :(得分:4)

而不是

import plotly as py

你应该

import plotly.plotly as py

为了将来参考,请在发布前尝试获取MVCE。通常它会帮助您自己找到错误。