Create multiple axis on the fly using a for loop

Hi,

I have multiple bar charts that I want to display in a completely different chart area -similar to a bullet chart-. In order to generate the different chart I need to use multiple xaxis and yaxis.

I am generating my bars using a for loop. Is there a way to generate the different xaxis/ yaxis within a loop and add them to the layout? I have seen that the name of the axis changes i.e. 'xaxis', 'xaxis1', 'xaxis2' etc. so I thought that there might be a way to replicate using a for loop.

Any clue how to tackle the above?

Hello,

If you’re doing it in Plotly, you can use subplots or dropdowns.
If you wish to specifically have one fixed axis (eg. x-axis is constant) and multiple axes on the axis (eg. 2 y-axes), you can use:

#assuming Y and Y2 are the two y values for data1 and data2 
fig={'data': [data1,data2],'layout' : go.Layout(xaxis=dict(range=[min(X),max(X)]),
                                                          yaxis=dict(range=[min(Y2),max(Y2*4)], title='Volume', side='right'),
                                                          yaxis2=dict(range=[min(Y),max(Y)], side='left', overlaying='y',title='sentiment'),
                                                          
                                                          title='Live sentiment ..'}

Hope this helped!

Hi @mr.t,

Once you know how many subplots you want, another approach is to construct the figure using the tools.make_subplots function. See https://plot.ly/python/subplots/#simple-subplot for some examples.

-Jon

1 Like