Dash学习记录

添加子图

效果

subplot

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=2, start_cell="bottom-left")

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
row=1, col=1)

fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
row=1, col=2)

fig.add_trace(go.Scatter(x=[300, 400, 500], y=[600, 700, 800]),
row=2, col=1)

fig.add_trace(go.Scatter(x=[4000, 5000, 6000], y=[7000, 8000, 9000]),
row=2, col=2)

fig.show()

定时刷新app

Dash app 的实时刷新功能主要依赖于 dash_core_components.Interval, 将Interval元素作为回调函数的输入,需要实时刷新的元素的相关属性值作为输出,通过监听Interval元素的 n_intervals 属性从而实现app的动态自动更新。Interval元素的interval属性可以控制刷新频率,该属性单位为毫秒,取整数值。例如需要app每分钟刷新一次,可以设置 interval = 60*1000

代码

1
2
3
4
5
dcc.Interval(
id='interval',
interval=second * 1000,
n_intervals=0
)

自定义子图

效果

custom subplot

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(
rows=5, cols=2,
specs=[[{}, {"rowspan": 2}],
[{}, None],
[{"rowspan": 2, "colspan": 2}, None],
[None, None],
[{}, {}]],
print_grid=True)

fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(1,1)"), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(1,2)"), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(2,1)"), row=2, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(3,1)"), row=3, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(5,1)"), row=5, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(5,2)"), row=5, col=2)

fig.update_layout(height=600, width=600, title_text="specs examples")
fig.show()

多类型子图

效果

multi-types-subplot

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(
rows=2, cols=2,
specs=[[{"type": "xy"}, {"type": "polar"}],
[{"type": "domain"}, {"type": "scene"}]],
)

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()

共享x轴

效果

shared-x

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import plotly.graph_objects as go

trace1 = go.Scatter(
x=[0, 1, 2],
y=[10, 11, 12]
)
trace2 = go.Scatter(
x=[2, 3, 4],
y=[100, 110, 120],
yaxis="y2"
)
trace3 = go.Scatter(
x=[3, 4, 5],
y=[1000, 1100, 1200],
yaxis="y3"
)
data = [trace1, trace2, trace3]
layout = go.Layout(
yaxis=dict(
domain=[0, 0.33]
),
legend=dict(
traceorder="reversed"
),
yaxis2=dict(
domain=[0.33, 0.66]
),
yaxis3=dict(
domain=[0.66, 1]
)
)
fig = go.Figure(data=data, layout=layout)
fig.show()

按照指定字段划分出子图

效果

鸢尾花数据集按照species字段划分出子图

subplot

代码

facet_col = "species"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import plotly.express as px
import plotly.graph_objects as go

df = px.data.iris()

fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", facet_col="species",
title="Adding Traces To Subplots Within A Plotly Express Figure")

reference_line = go.Scatter(x=[2, 4],
y=[4, 8],
mode="lines",
line=go.scatter.Line(color="gray"),
showlegend=False)

fig.add_trace(reference_line, row=1, col=1)
fig.add_trace(reference_line, row=1, col=2)
fig.add_trace(reference_line, row=1, col=3)

fig.show()

指定颜色

效果

color

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from plotly.subplots import make_subplots

fig = make_subplots(rows=1, cols=2)

fig.add_scatter(y=[4, 2, 3.5], mode="markers",
marker=dict(size=20, color="LightSeaGreen"),
name="a", row=1, col=1)

fig.add_bar(y=[2, 1, 3],
marker=dict(color="MediumPurple"),
name="b", row=1, col=1)

fig.add_scatter(y=[2, 3.5, 4], mode="markers",
marker=dict(size=20, color="MediumPurple"),
name="c", row=1, col=2)

fig.add_bar(y=[1, 3, 2],
marker=dict(color="LightSeaGreen"),
name="d", row=1, col=2)

fig.show()

不同风格的主题

效果

1

2

3

4

5

代码

1
2
3
4
5
6
7
8
9
10
11
12
import plotly.express as px

df = px.data.gapminder()
df_2007 = df.query("year==2007")

for template in ["plotly", "plotly_white", "plotly_dark", "ggplot2", "seaborn", "simple_white", "none"]:
fig = px.scatter(df_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
template=template, title="Gapminder 2007: '%s' theme" % template)
fig.show()

注册自己的主题+混合主题

效果

theme

代码

theme

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import plotly.io as pio
import plotly.express as px
import plotly.graph_objects as go

pio.templates["draft"] = go.layout.Template(
layout_annotations=[
dict(
name="draft watermark",
text="DRAFT",
textangle=-30,
opacity=0.1,
font=dict(color="black", size=100),
xref="paper",
yref="paper",
x=0.5,
y=0.5,
showarrow=False,
)
]
)
pio.templates.default = "seaborn+draft"

df = px.data.gapminder()
df_2007 = df.query("year==2007")

fig = px.scatter(df_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
title="Gapminder 2007: current default theme")
fig.show()

另存为

效果

支持很多格式

save-as

代码

1
2
3
4
5
fig.write_image("images/fig1.png")
fig.write_image("images/fig1.jpeg")
fig.write_image("images/fig1.webp")
fig.write_image("images/fig1.svg")
fig.write_image("images/fig1.pdf")

保存为网页

代码

1
plotly.offline.plot(fig, html_name)