[PYTHON] Where to fix code when using plotly for free

Today's study session was graph visualization, but I found that plotly was really cool, so I will leave a note. スクリーンショット 2017-09-20 4.29.47.png

I misunderstood that plotly had to log in, I was told at a study session, but it seemed that I didn't need to register to run it in offline mode. Thank you for telling me.

If you want to try it, please use it with jupyter notebook etc. plotly is on the side of. https://github.com/miyamotok0105/pydata-book/blob/master/ch08-J.ipynb

In the first place, please refer to the graph here. https://qiita.com/alchemist/items/544d45480ce9c1ca2c16

Code fix

It's OK if you modify it to use plotly.offline instead of plotly.plotly </ b>

For example, map mapping modifications

スクリーンショット 2017-09-20 4.10.28.png

python


import plotly.plotly as py#here
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')

for col in df.columns:
    df[col] = df[col].astype(str)

scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
            [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]

df['text'] = df['state'] + '<br>' +\
    'Beef '+df['beef']+' Dairy '+df['dairy']+'<br>'+\
    'Fruits '+df['total fruits']+' Veggies ' + df['total veggies']+'<br>'+\
    'Wheat '+df['wheat']+' Corn '+df['corn']

data = [ dict(
        type='choropleth',
        colorscale = scl,
        autocolorscale = False,
        locations = df['code'],
        z = df['total exports'].astype(float),
        locationmode = 'USA-states',
        text = df['text'],
        marker = dict(
            line = dict (
                color = 'rgb(255,255,255)',
                width = 2
            ) ),
        colorbar = dict(
            title = "Millions USD")
        ) ]

layout = dict(
        title = '2011 US Agriculture Exports 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.iplot( fig, filename='d3-cloropleth-map' )#here

Changed from plotly.plotly to plotly.offline.

# import plotly.plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

Changed from py.iplot to iplot.

python


fig = dict( data=data, layout=layout )
#py.iplot( fig, filename='d3-cloropleth-map' )
iplot( fig, filename='d3-cloropleth-map' )

You can change it in this way.

Full text.

python


# import plotly.plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')

for col in df.columns:
    df[col] = df[col].astype(str)

scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
            [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]

df['text'] = df['state'] + '<br>' +\
    'Beef '+df['beef']+' Dairy '+df['dairy']+'<br>'+\
    'Fruits '+df['total fruits']+' Veggies ' + df['total veggies']+'<br>'+\
    'Wheat '+df['wheat']+' Corn '+df['corn']

data = [ dict(
        type='choropleth',
        colorscale = scl,
        autocolorscale = False,
        locations = df['code'],
        z = df['total exports'].astype(float),
        locationmode = 'USA-states',
        text = df['text'],
        marker = dict(
            line = dict (
                color = 'rgb(255,255,255)',
                width = 2
            ) ),
        colorbar = dict(
            title = "Millions USD")
        ) ]

layout = dict(
        title = '2011 US Agriculture Exports 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 )
iplot( fig, filename='d3-cloropleth-map' )

It's cool to be able to make even with a button like this.

スクリーンショット 2017-09-20 4.29.47.png スクリーンショット 2017-09-20 4.30.41.png

There are more samples, so I wonder if I can do it again at a meeting to touch this area. https://plot.ly/python/

See you soon

Recommended Posts

Where to fix code when using plotly for free
Things to watch out for when using default arguments in Python
[VS Code] ~ Tips when using python ~
How to call when using double underscore (Private attribute) for class function
AI drug discovery to start for free using papers and public databases
[python, multiprocessing] Behavior for exceptions when using multiprocessing
Precautions when using for statements in pandas
First-principles calculations for free using Google Colaboratory
Code etc. that beginners referred to when they stumbled when using WebDriver with Selenium
Things to keep in mind when using Python for those who use MATLAB
How to code a drone using image recognition
Precautions when using tf.keras.layers.TimeDistributed for tf.keras custom layer
How to fix when Terminal input becomes abnormal