Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

20
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-02-10

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

20
17
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

Qiita Advent Calendar is held!

Qiita Advent Calendar is an article posting event where you post articles by filling a calendar 🎅

Some calendars come with gifts and some gifts are drawn from all calendars 👀

Please tie the article to your calendar and let's enjoy Christmas together!

20
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?