5

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

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I will review the link (MVCE) and try harder next time, It may look like I know things but I know absolutely nothing and my background is excel- haha. I appreciate the help.
@DavisVance No worries, I asked much worse questions when I got started. Don't forget, if an answer has solved your problem, accept it and upvote it (:
I have import plotly.plotly as py and have a related error: AttributeError: module 'plotly.plotly' has no attribute 'plot'
@Revolucion for Monica: import ploty.plotly; plotly.plot() did not work for me either. Using an alias did: import ploty.plotly as py; py.plot(). If you write plotly.plot(), this seems to call the outer Plotly module. So, if you do not use an alias, you need to write plotly.plotly.plot(). Enoug "plots" for now.

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.