Plotly secondary Y axis in Python

Hi everyone,

I am trying to create a chart with secondary axis and having trouble doing so. I can’t find a direct reference in documentation apart from this:

I have found similar question with a solution on how we can create the chart with secondary axis:
https://plot.ly/~empet/14349/plotly-plot-with-two-y-axes/#/

The above example works but the secondary axis are off and graph doesn’t look correct:

In this image above, there are 2 graphs. One created by Plotly & Other created in Excel.

You can see how line graph is ploted nicely compared to Plotly graph.

I think this is because of the way the secondary Y axis are being calculated as highlighted.

I have tried playing around with tick0=0, dtick=0.25 as per this doc:

This also doesn’t work because it only affects both left Y axis as I want something dynamic like in excel.

Have anyone have this issue and found a solution for this?

I appreciate your help.

Kind Regards

@rahul Could you explain better what is wrong with the Plotly plot? The two yaxes are independent!!!

Hi @rahul,

Are you concerned that the range of the right yaxis doesn’t start at zero? If so, you can control the axis extent explicitly with the range property of the axis (e.g. set range to [0, 200000]). If you want the range to be dynamic, and you want it to include zero, set the rangemode property to tozero.

Hope that helps,
-Jon

1 Like

@empet Apologies my post above is not clear but basically what I want to achieve is exactly what I have shown in the screenshot. For the Plotly graph to look exactly like Excel output.
Thanks for looking into this all I needed was to set rangemode property to tozero as @jmmease mentioned

1 Like

@jmmease Thanks for your input! This is exactly what I was trying to achieve but having trouble explain.
Could you let me know how I would have been able to find out that I needed to set rangemode property to tozero ?
I did went through multiple documentations but was not able to find this setting :smiley:
Is this because I am not familiar with this framework enough yet and I need to spend more time going through documentations?
Thanks again for your time and help!
Kind Regards

Hi @rahul,

Great! I’m glad that worked for you. For searching for specific properties there are two general approaches that I use.

The first is to use the Full API Reference page. This has a search box that lets you filter through the names and descriptions of all of the properties. Here’s what you get searching for “range”.

You can see that yaxis.rangemode shows up at the bottom of the screen.

But the approach that I usually use myself (in version 3) is to search through an objects properties and descriptions right in the notebook based on the docstrings. Here’s a GIF of the process I actually used to find rangemode after reading your question.

Hope that helps!
-Jon

2 Likes

@jmmease Thanks! This is very helpful