import plotly.graph_objects as go
x = [1, 2, 3]
line_y = [5, 3, 2]
scatter_y = [-2, 4, 3]
bar_y = [1, 3, 4]
line_trace = go.Scatter(
x=x,
y=line_y,
# 折れ線グラフのスタイル
# colorを16進数で指定
line={"width": 5, "color": "#1f77b4", "dash": "dashdot"},
opacity=0.4, # 不透明度
name="line",
)
scatter_trace = go.Scatter(
x=x,
y=scatter_y,
mode="markers",
# 要素のスタイル
marker={
"size": 20,
# colorをrgbaで指定
"color": "rgba(255, 127, 14, 0.5)",
"line": {"width": 3, "color": "rgba(214, 39, 40, 0.5)"},
},
name="scatter",
)
bar_trace = go.Bar(
x=x,
y=bar_y,
width=0.3,
marker={
# colorをrgbで指定
"color": ["rgb(255, 127, 14)", "rgb(44, 160, 44)", "rgb(214, 39, 40)"],
"line": {"width": 3, "color": "black"},
},
opacity=0.4, # 不透明度
name="bar",
)
layout = go.Layout(
# グラフタイトルのスタイル
# colorをCSSカラーネームで指定
title={
"text": "Title",
"font": {"family": "arial", "size": 20, "color": "green"},
},
# X軸のスタイル
xaxis={
"title": {
"text": "X軸",
"font": {"family": "arial", "size": 10, "color": "navy"},
},
"tickfont": {"family": "arial", "size": 10, "color": "olive"},
"tickangle": 45,
},
# Y軸のスタイル
yaxis={
"title": {
"text": "Y軸",
"font": {"family": "arial", "size": 10, "color": "darkviolet"},
},
"showline": True,
"linewidth": 2,
"linecolor": "darkgray",
"gridwidth": 1,
"gridcolor": "indianred",
"zeroline": True,
"zerolinewidth": 2,
"zerolinecolor": "indigo",
},
)
go.Figure([line_trace, scatter_trace, bar_trace], layout=layout).show()
fig = go.Figure(
go.Scatter(x=["2020-01-01", "2020-01-02", "2020-01-03"], y=[3, 5, 2])
)
fig.update_layout(
autosize=False,
width=300,
height=300,
# ❶ bを50に設定しているが、xaxis.automarginが優先される
margin={"l": 50, "r": 50, "b": 50, "t": 50, "pad": 15},
paper_bgcolor="lightcoral",
xaxis={"title": {"text": "X"}},
yaxis={"title": {"text": "y"}},
)
fig.show()
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2)
# ❶ X軸に日付、Y軸に対数をとった折れ線グラフ
fig.add_trace(
go.Scatter(x=["2010", "2011", "2012"], y=[1, 10, 1000]), row=1, col=1
)
fig.update_xaxes(type="date", row=1, col=1) # 日付
fig.update_yaxes(type="log", row=1, col=1) # 対数
# ❷ サブカテゴリを持った棒グラフ
fig.add_trace(go.Bar(x=[["1年", "1年"], ["A組", "B組"]], y=[70, 60]), row=1, col=2)
fig.add_trace(go.Bar(x=[["2年", "2年"], ["A組", "B組"]], y=[70, 60]), row=1, col=2)
fig.update_xaxes(type="multicategory", row=1, col=2) # 階層カテゴリ
fig.show()
# Y軸に2軸目が存在するサブプロット
two_yaxis_fig = make_subplots(specs=[[{"secondary_y": True}]])
two_yaxis_fig.add_trace(go.Scatter(x=[1, 2, 3], y=[3, 2, 4], name="1st"))
two_yaxis_fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[1, 20, 15], name="2nd"),
secondary_y=True, # traceを2軸目とする
)
two_yaxis_fig.update_yaxes(title={"text": "1st"}, showgrid=False)
two_yaxis_fig.update_yaxes(
secondary_y=True, # 2軸目の設定
title={"text": "2nd"}, # 軸ラベルの指定
showgrid=False # 補助線を非表示
)
two_yaxis_fig.show()
config_tick_fig = make_subplots(
rows=2, cols=2, horizontal_spacing=0.15, vertical_spacing=0.2
)
config_tick_fig.add_trace(go.Scatter(x=[1, 2, 3], y=[5, 3, 4]))
config_tick_fig.update_xaxes(
tick0=2, dtick=0.3, title="2を基準に刻み幅を0.3", row=1, col=1
)
config_tick_fig.update_yaxes(
autorange="reversed", title="Y軸の順序を逆に設定", row=1, col=1
)
config_tick_fig.add_trace(go.Scatter(x=[1, 2, 3], y=[5, 3, 4]), row=1, col=2)
config_tick_fig.update_xaxes(
tickvals=[1.8, 2, 2.2], title="表示する目盛を指定", row=1, col=2
)
config_tick_fig.update_yaxes(range=[2, 4], title="描画範囲を指定", row=1, col=2)
config_tick_fig.add_trace(go.Scatter(x=[-1, 0, 1], y=[4, 5, 3]), row=2, col=1)
config_tick_fig.update_xaxes(
rangemode="nonnegative", title="正の値の範囲のみを描画", row=2, col=1
)
config_tick_fig.update_yaxes(
rangemode="tozero", title="0からの範囲を描画", row=2, col=1
)
config_tick_fig.show()
x = [1, 2, 3]
various_legend_fig = go.Figure()
various_legend_fig.add_trace(go.Scatter(x=x, y=[5, 3, 2])) # ❶
various_legend_fig.add_trace(go.Scatter(x=x, y=[4, 2, 3], name="line2")) # ❷
various_legend_fig.add_trace(
go.Scatter(x=x, y=[3, 5, 4], name="line3", showlegend=False) # ❸
)
various_legend_fig.show()
horizontal_legend_fig = go.Figure()
horizontal_legend_fig.add_trace(go.Scatter(x=x, y=[5, 3, 2], name="line1"))
horizontal_legend_fig.add_trace(go.Scatter(x=x, y=[4, 2, 3], name="line2"))
horizontal_legend_fig.update_layout(legend_orientation="h") # ❶ 横並びの凡例
horizontal_legend_fig.show()
moved_legend_fig = go.Figure()
moved_legend_fig.add_trace(go.Scatter(x=x, y=[5, 3, 2], name="line1"))
moved_legend_fig.add_trace(go.Scatter(x=x, y=[4, 2, 3], name="line2"))
moved_legend_fig.layout.update(legend={"x": -0.15, "y": 0.5}) # ❶ 凡例の位置
moved_legend_fig.show()
grouped_legend_fig = go.Figure()
grouped_legend_fig.add_trace(
go.Scatter(x=x, y=[5, 3, 2], name="A-1", legendgroup="groupA")
)
grouped_legend_fig.add_trace(
go.Scatter(x=x, y=[3, 5, 4], name="B-1", legendgroup="groupB")
)
grouped_legend_fig.add_trace(
go.Scatter(x=x, y=[4, 2, 3], name="A-2", legendgroup="groupA")
)
grouped_legend_fig.add_trace(
go.Scatter(x=x, y=[2, 3, 4], name="B-2", legendgroup="groupB")
)
grouped_legend_fig.show()
import numpy as np
np.random.seed(1)
go.Figure(
go.Heatmap(
z=np.random.randn(10, 10),
colorscale="PuBu", # カラースケール
zmin=-1, # ❶ 値の最小値
zmax=3, # ❶ 値の最大値
)
).show()
np.random.seed(1)
x, y, z = np.random.randn(3, 100)
go.Figure(
go.Scatter(
x=x,
y=y,
mode="markers",
marker={
"color": z,
"colorscale": "Greens",
"cmin": -1,
"cmax": 1,
# ❶ カラーバー、0を基準に0.2刻みの目盛を表示
"colorbar": {"title": "z", "tick0": 0, "dtick": 0.2},
},
)
).show()
go.Figure(
go.Heatmap(
z=np.random.randn(10, 10), # ❶ カスタマイズしたカラースケール
colorscale=[
[0, "rgb(0,255,255)"],
[0.5, "rgb(0,80,80)"],
[1, "rgb(0,20,20)"],
],
)
).show()