Fork me on GitHub

plotly-express-20-plotly中设置轴刻度

在某些业务需求中,我们并不希望坐标轴上的刻度是连续型的,而是具有一些我们指定的间距,这个时候需要我们指定轴刻度。本文中介绍的是如何在plotly实现轴刻度的设置。

改变起始值

改变坐标轴的起始值,有时候不需要从0开始

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

fig = go.Figure(go.Scatter(
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
y = [28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9]
))

fig.update_layout(
xaxis = dict(
tickmode = 'linear',
tick0 = 0.5, # 起始点
dtick = 0.75 # 间距
)
)

fig.show()

自定义刻度

改变坐标轴上的默认刻度值,用自定义的刻度。通过数组的形式来实现

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

go.Figure(go.Scatter(
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
y = [28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9]
))

fig.update_layout(
xaxis = dict(
tickmode = 'array',
tickvals = [1, 3, 10, 7, 9, 12], # 10表示的是第10个数据
ticktext = ['One', 'Three', 'Five', 'Seven', 'Nine', 'Eleven']
)
)

fig.show()

改变轴刻度属性

1
2
3
4
5
6
7
8
9
import plotly.graph_objects as go

go.Figure(go.Scatter(
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
y = [28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9]
))

fig.update_layout(yaxis_tickformat = '%')
fig.show()

image-20200729003130606

坐标轴使用刻度线

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.Bar(
x = ["apples", "oranges", "pears"],
y = [1, 2, 3]
))

fig.update_xaxes(
showgrid=True,
ticks="outside",
tickson="boundaries",
ticklen=20
)

fig.show()

改变标签位置

本文标题:plotly-express-20-plotly中设置轴刻度

发布时间:2020年07月29日 - 00:07

原始链接:http://www.renpeter.cn/2020/07/29/plotly-express-20-plotly%E4%B8%AD%E8%AE%BE%E7%BD%AE%E8%BD%B4%E5%88%BB%E5%BA%A6.html

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

Coffee or Tea