如何在 Plotly 中设置 xaxis 刻度?

How to set xaxis ticks in Plotly?

这是我的数据:

z=[[2.021127032949411,
  2.405934869144868,
  6.005238252515181,
  0.43308358365566557,
  6.80624302463342,
  1.4243920241458794],
 [6.754588097147502,
  17.66441844606136,
  17.66863225189955,
  4.796490376261003,
  4.100119672126023,
  2.6461740133188454],
 [30.522227304933793,
  25.244026806049867,
  44.77345477001106,
  24.58566233495368,
  5.070470289616061,
  0.9441603389397017],
 [2.154134557312937,
  4.310863690800093,
  6.1213216109229505,
  3.1274613380516687,
  2.5391663573164514,
  0.3578307481864878],
 [19.520038969668185,
  10.092300407536902,
  1.980581522863168,
  2.792253899319521,
  0.7083651529637687,
  1.7654249187194606]]
x=['0.0',
 '6.784919041781837',
 '13.569838083563674',
 '20.354757125345515',
 '27.139676167127348',
 '33.92459520890919']
y= [0.0,
 1306.1224489795918,
 2612.2448979591836,
 3918.3673469387754,
 5224.489795918367]

我的代码如下所示:

fig = go.Figure(data=go.Heatmap(
                z=z,
                x=x,
                y=y,
                colorscale='Viridis'))

fig.update_layout(
                  xaxis = dict(
                      tickmode = 'linear',
                      tick0 = 0,
                      dtick = 20,
                    ),
                   font=dict(size=18, color="black"))

plotly.offline.plot(fig)

结果是

如何设置漂亮的 x 轴刻度,例如没有小数点的 [0, 5, 10...]

看来我上面对 tick0dtick 的尝试没有帮助。

xaxis 字典中,您可以发送密钥 tickvalsticktext。可以在 Plotly 文档中找到一个很好的例子。

https://plotly.com/python/tick-formatting/

解决方案类似于

xaxis = dict(
  tickmode = 'linear',
  tick0 = 0,
  dtick = 20,
  tickvals = np.arange(0, 35, 1)
  ticktext = [f"{i}u" for i in np.arange(0, 35, 1)]
)

嗯,你好o/

按照下面的代码adapt/update按照您的意愿进行;)

import plotly.graph_objects as go

fig = go.Figure(data=go.Heatmap(
                   z=z,
                   x=x,
                   y=y,
                   colorscale='Viridis'))

fig.update_layout(
                  xaxis = dict(
                    tickmode='array', #change 1
                    tickvals = x, #change 2
                    ticktext = [0,5,10,15,20,25], #change 3
                    ),
                   font=dict(size=18, color="black"))

fig.show()

结果(点击放大查看):