在某些业务需求中,我们并不希望坐标轴上的刻度是连续型的,而是具有一些我们指定的间距,这个时候需要我们指定轴刻度。本文中介绍的是如何在plotly
实现轴刻度的设置。
改变起始值
改变坐标轴的起始值,有时候不需要从0开始
代码语言:javascript复制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()
自定义刻度
改变坐标轴上的默认刻度值,用自定义的刻度。通过数组的形式来实现
代码语言:javascript复制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()
改变轴刻度属性
代码语言:javascript复制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()
坐标轴使用刻度线
代码语言:javascript复制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()
改变标签位置