Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the y axis step size?

Tags:

python

plotly

This is my code:

from plotly import graph_objs as go
import numpy as np
import os
from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=1)

# Table product views
fig.add_trace(
        go.Table(
            header=dict(values=["Period", "Views"]),
            cells=dict(values=[
                [
                    "01/09/2019 - 07/09/2019",
                    "08/09/2019 - 14/09/2019", 
                    "15/09/2019 - 21/09/2019",
                    "22/09/2019 - 28/09/2019"
                ],
                [15, 25, 35, 32]
            ])
        )
)

# Chart product views
fig.add_trace(

        go.Bar(
            x=[
                "01/09/2019 - 07/09/2019",
                "08/09/2019 - 14/09/2019", 
                "15/09/2019 - 21/09/2019",
                "22/09/2019 - 28/09/2019"
                ],
            y=[15, 25, 35, 32],
        ),
        row=2,
        col=1

)

if not os.path.exists("files"):
    os.mkdir("files")

fig.update_yaxes(title_text="Product views", range=[0, 40], row=2, col=1)
fig.update_layout(height=600, width=600, title_text="<b>Library Shelving</b> statistic")
fig.write_image("files/statistic.pdf")

It renders one table and one bar chart. On the bar chart Y axis steps are like follow: 30,25,20,15,10,5,0. How can I show the rest of the numbers? Or atleast make the step 2 ( not 5 ). E.g. 30,28,26,24 etc.

Current plot:

enter image description here

like image 466
Toma Tomov Avatar asked Oct 16 '25 14:10

Toma Tomov


2 Answers

Just set dtick=2 in fig.update_yaxes() like this:

Snippet 1

fig.update_yaxes(title_text="Product views", range=[0, 40], row=2, col=1, dtick=2)

Plot 1:

enter image description here

This makes the plot a little weird, so I'd adjust your height to 800 as well:

Snippet 2:

fig.update_layout(height=800, width=600, title_text="<b>Library Shelving</b> statistic")

Plot 2:

enter image description here

like image 76
vestland Avatar answered Oct 19 '25 08:10

vestland


You can use the dtick parameter: https://plot.ly/python/reference/#layout-yaxis-dtick

like image 30
nicolaskruchten Avatar answered Oct 19 '25 10:10

nicolaskruchten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!