Fork me on GitHub

plotly-express-17-plotly绘图技巧之图例与标题(二)

Plotly-express-17-图例legend和标题设置

本文中介绍的是Plotly中对于图形图例设置的技巧,主要包含:

  • 整体基本设置
  • 修改图例名称
  • 隐藏图例入口(第一个图例)
  • 图例位置显示
  • 自定义优美图例
  • 图例散点大小设置
  • 组图例设置
  • 标题设置

参考

https://plotly.com/python/figure-labels/

https://plotly.com/python/legend/

https://plotly.com/python/reference/#layout

整体设置

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
fig = go.Figure()

fig.add_trace(go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 1, 2, 3, 4, 5, 6, 7, 8],
name="Name of Trace 1" # 第一个图例名称
))

fig.add_trace(go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[1, 0, 3, 2, 5, 4, 7, 6, 8],
name="Name of Trace 2", # 第2个图例名称
visible='legendonly' # 将第2图例变成灰色,点击可见图形

))

fig.update_layout(
title="Plot Title", # 主标题
xaxis_title="x Axis Title", # 2个坐标轴的标题
yaxis_title="y Axis Title",
font=dict(
family="Courier New, monospace",
size=18,
color="#7f7f7f"
)
)
fig.update_layout(showlegend=False, # 隐藏图例,默认是True
legend_title_text='Trend' # 修改图例的名称
)
fig.show()

图例

隐藏图例入口

修改图例名称

图例显示位置

图例作为legend,位置在左上角

自定义图例

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

fig = go.Figure()

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

fig.update_layout(
legend=dict(x=0,y=1, # 图例的位置:将坐标轴看做是单位1
traceorder="normal",
font=dict(
family="sans-serif",
size=12,
color="black"),
bgcolor="LightSteelBlue", # 背景颜色,边框颜色和宽度
bordercolor="Black",
borderwidth=2
)
)

fig.show()

散点大小

Grouped Legend

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
35
36
37
38
39
40
41
import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
x=[1, 2, 3],
y=[2, 1, 3],
legendgroup="group", # this can be any string, not just "group"
name="first legend group", # 名称
mode="markers", # 散点类型:markers,lines
marker=dict(color="Crimson", size=10) # mode的设置
))

fig.add_trace(go.Scatter(
x=[1, 2, 3],
y=[2, 2, 2],
legendgroup="group",
name="first legend group - average",
mode="lines",
line=dict(color="Crimson")
))

fig.add_trace(go.Scatter(
x=[1, 2, 3],
y=[4, 9, 2],
legendgroup="group2",
name="second legend group",
mode="markers",
marker=dict(color="MediumPurple", size=10)
))

fig.add_trace(go.Scatter(
x=[1, 2, 3],
y=[5, 5, 5],
legendgroup="group2",
name="second legend group - average",
mode="lines",
line=dict(color="MediumPurple")
))

fig.show()

标题设置-Align Plot Title

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import plotly.graph_objects as go

fig = go.Figure(go.Scatter(
y=[3, 1, 4],
x=["Mon", "Tue", "Wed"]))

fig.update_layout(
title={
'text': "Plot Title", # 标题名称
'y':0.9, # 位置,坐标轴的长度看做1
'x':0.5,
'xanchor': 'center', # 相对位置
'yanchor': 'top'})

fig.show()

本文标题:plotly-express-17-plotly绘图技巧之图例与标题(二)

发布时间:2020年07月09日 - 20:07

原始链接:http://www.renpeter.cn/2020/07/09/plotly-express-17-plotly%E7%BB%98%E5%9B%BE%E6%8A%80%E5%B7%A7%E4%B9%8B%E5%9B%BE%E4%BE%8B%E4%B8%8E%E6%A0%87%E9%A2%98.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea