Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
15
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

Plotlyで折れ線グラフや複合グラフ

Plotlyで折れ線グラフや複合グラフ

この記事では plotly 2.3.0を利用しています。

はじめに


Plotlyは大変便利です。
実際に業務で利用する上で設定している項目などまとめてみました。ここでは折れ線や複合グラフ。
時系列のチャートを想定しています。なお、チャートは静止画です。

折れ線グラフ



# coding:utf-8

import pandas as pd
import numpy as np

import plotly.plotly as py
import plotly.graph_objs as go
import datetime

import plotly.offline as offline
offline.init_notebook_mode()


# 日付の作成。100日

d1 = datetime.date(2017,1,1)

d1_list = []

for i in range(0,100): 

    temp = d1 + datetime.timedelta(i)

    d1_list.append(temp)

# データの作成
X = np.random.randint(0,50,100) 
Y = np.random.randint(0,100,100) 

trace0 = go.Scatter(x = d1_list, y = X, mode = 'lines', name = 'X')
trace1 = go.Scatter(x = d1_list, y = Y, mode = 'lines', name = 'Y')

# レイアウトの指定
layout = go.Layout(xaxis = dict(title = 'date', type='date', dtick = 'M1'),  # dtick: 'M1'で1ヶ月ごとにラベル表示 
              yaxis = dict(title = 'value'))

fig = dict(data = [trace0, trace1], layout = layout)

offline.iplot(fig)

oresen_1.png

2軸の複合グラフ(折れ線と棒グラフ)



trace0 = go.Bar(x = d1_list, y = X, name = 'X',  yaxis='y1')
trace1 = go.Scatter(x = d1_list,  y = Y, mode = 'lines', name = 'Y', yaxis='y2')

# レイアウトの指定
layout = go.Layout(xaxis = dict(title = 'date', type='date', dtick = 'M1'),  # dtick: 1か月ごとは'M1' 
              yaxis = dict(title = 'value(x)', side = 'left', showgrid=False, # 2軸だと見誤る場合があるので目盛り線は表示させない(showgrid=False)
                           range = [0, max(X)]),                             # rangeで指定したほうがよい。ゼロが合わない場合などがある。
              yaxis2 = dict(title = 'value(y)', side = 'right', overlaying = 'y', range = [0, max(Y)], showgrid=False))


fig = dict(data = [trace0, trace1], layout = layout)
offline.iplot(fig)

oresen_2.png

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
15
Help us understand the problem. What are the problem?