LoginSignup
16
21

More than 5 years have passed since last update.

plotlyを無料で使う時のコード修正場所

Last updated at Posted at 2017-09-19

今日の勉強会はグラフ可視化だったのですが、plotlyがすごいかっこいいことがわかったのでメモを残します。
スクリーンショット 2017-09-20 4.29.47.png

plotlyはログインしないといけないと勘違いしてましたが、
勉強会で教えて頂いたのですがオフラインモードで動かすには登録必要なかったようでした。
教えて頂きありがとうございます。

使ってみる場合はjupyter notebookなどでお使いください。
plotlyは、した〜の方にあります。
https://github.com/miyamotok0105/pydata-book/blob/master/ch08-J.ipynb

そもそもグラフって、、時はこちらなど参考に。
https://qiita.com/alchemist/items/544d45480ce9c1ca2c16

コード修正点

plotly.plotlyではなく、plotly.offlineを使用するように修正すればOK

例えば地図マッピングの修正点

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

import plotly.plotly as py#ここ
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' )#ここ

plotly.plotlyからplotly.offlineに変更。

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

py.iplotからiplotに変更。

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

この要領で変更すればいけます。

全文。

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

コォいうボタン付きのまで作れるのはカッチョいい。

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

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

サンプルはもっと色々あるのでまたこの辺を触ってみる会でもやろうかなぁ
https://plot.ly/python/

ではでは

16
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
21