4
2

More than 3 years have passed since last update.

【複数のグラフを描画とラベル】plotlyで動的な可視化をする【python3,make subplot, xlabel, ylabel】

Posted at

python==3.8
plotly==4.10.0

plotlyの設定てきなもので
よく使うものを追加していくつもり

図面を割り振る(subplot)

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(y=[4, 2, 1], mode="lines"), row=1, col=1)
fig.add_trace(go.Bar(y=[2, 1, 3]), row=1, col=2)
fig.show()

image.png

背景色

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(y=[4, 2, 1], mode="lines"), row=1, col=1)
fig.add_trace(go.Bar(y=[2, 1, 3]), row=1, col=2)

fig.update_layout(
    margin=dict(l=20, r=20, t=20, b=20),
    paper_bgcolor="LightSteelBlue",
)

fig.show()

image.png

x,yラベルのタイトル

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(y=[4, 2, 1], mode="lines"), row=1, col=1)
fig.add_trace(go.Bar(y=[2, 1, 3]), row=1, col=2)

fig.update_layout(
    margin=dict(l=20, r=20, t=20, b=20),
    paper_bgcolor="LightSteelBlue",
)


fig.update_xaxes(title_text="xlabel")
fig.update_yaxes(title_text="ylabel", hoverformat=".3f")

fig.show()

image.png

特殊なplotの割り振り

barやscattorはmake_subplotsのままで指定できるが、
pieやscatter3Dはこのままでは指定できない
make_subplotsに何を指定したいかを指示しておく

specsで指定

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(
    rows=2, cols=2,
    specs=[[{"type": "bar"}, {"type": "barpolar"}],
           [{"type": "pie"}, {"type": "scatter3d"}]],
)

fig.add_trace(go.Bar(y=[2, 3, 1]),row=1, col=1)
fig.add_trace(go.Barpolar(theta=[0, 45, 90], r=[2, 3, 1]),row=1, col=2)
fig.add_trace(go.Pie(values=[2, 3, 1]),row=2, col=1)
fig.add_trace(go.Scatter3d(x=[2, 3, 1], y=[0, 0, 0], z=[0.5, 1, 2], mode="lines"),row=2, col=2)

fig.update_layout(height=700, showlegend=False)

fig.show()

image.png

以上

追記記録

2020-09-27 作成

4
2
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
4
2