How to set minor ticks in plotly

How can I create minor ticks in plotly? Minor ticks have shorter tick lengths and not labelled. Can one specify a frequency, like 4 minor ticks between major ticks?

Upvotes: 2

Views: 5138

Answers (2)

So here is the javascript version for minor ticks:

var data = {
  x: [1, 2, 3, 4, 5],
  y: [3, 4, 1, 6, 3],
};
var layout = {
  yaxis: {
    ticks: 'inside',
    ticklen: 10,
    tickcolor: 'black',
    minor: {
      ticks: 'inside',
      ticklen: 6,
      tickcolor: 'black'
    }
  }
};
Plotly.newPlot('myDiv', [data], layout);
<script src='https://cdn.plot.ly/plotly-2.14.0.min.js'></script>
<div id='myDiv'></div>

It seems minor_ticks='inside' works in python, but not in js. In js it should be minor: {ticks: 'inside'}.

PS: I just figured out it works with https://cdn.plot.ly/plotly-2.14.0.min.js, but not with https://cdn.plot.ly/plotly-latest.min.js (v1.58.5).

Upvotes: 1

Redox
Redox

Reputation: 9967

Please see below code for an example. You will need to use the minor attribute to set this. The length is controlled by ticklen, color by tickcolor, number of ticks by nticks, position (inside/outside) by minor_ticks and so on. More information is available here.

Code

import plotly.express as px
import pandas as pd

df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="sex")
fig.update_xaxes(minor=dict(ticklen=6, tickcolor="black", tickmode='auto', nticks=10, showgrid=True))
fig.update_yaxes(minor_ticks="inside")
fig.show()

enter image description here

Upvotes: 3

Related Questions