简体   繁体   中英

How to set xaxis ticks in Plotly?

This is my data:

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]

My code looks like this:

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)

And the result is

在此处输入图像描述

How can I set nice x axis ticks, for example [0, 5, 10...] with no decimal points?

Looks like my attempt above with tick0 and dtick is not helpful.

Well, hello o/

Follow the code below and adapt/update it how you want;)

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

Result ( click to zoom in and check ):

在此处输入图像描述

At the xaxis dict you can send the keys tickvals and ticktext . A nice example can be found at the Plotly documentation.

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

The solution would be something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM