Plotly.py: Multiple axis support in version 3

Created on 9 Aug 2018  路  4Comments  路  Source: plotly/plotly.py

When I try to add another yaxis to the layout imperatively, the traces in the original yaxis seem to dissapear, here's a minimal example:

fig = go.FigureWidget()

trace1 = go.Bar()
trace1.x = [1, 2, 3, 4]
trace1.y = [1, 2, 3, 4]
trace1.yaxis = 'y1'
fig.add_trace(trace1)

trace2 = go.Scatter()
trace2.x = [1, 2, 3, 4]
trace2.y = [1, 2, 3, 4]
trace2.yaxis = 'y2'
fig.add_trace(trace2)

fig.layout['yaxis2'] = go.layout.YAxis()
fig.layout.yaxis2.side = 'right'

fig

I'm not so comfortable with fig.layout['yaxis2'] = go.layout.YAxis() also, it might be the root of the issue. Maybe a method to add an axis would be nice.
I'm loving version 3 btw.

Most helpful comment

Hi @vlizanae ,
It looks like you need to add the overlaying='y1' option to yaxis2

fig = go.FigureWidget()

trace1 = go.Bar()
trace1.x = [1, 2, 3, 4]
trace1.y = [1, 2, 3, 4]
trace1.yaxis = 'y1'
fig.add_trace(trace1)

trace2 = go.Scatter()
trace2.x = [1, 2, 3, 4]
trace2.y = [1, 2, 3, 4]
trace2.yaxis = 'y2'
fig.add_trace(trace2)

fig.layout['yaxis2'] = go.layout.YAxis(side='right', overlaying='y1')

fig

newplot-5
See also the examples at https://plot.ly/python/multiple-axes/

All 4 comments

@vlizanae, After fig.add_trace(trace2) just update the fig.layout as follows:

fig.layout.update(yaxis2=dict(side='right'))
Now both fig.data and fig.layout are well defined, but the bar trace isn't displayed.
I tried the "classical" definition:

tr1=dict(type='bar',
        x=[1,2,3,4],
        y=[1,2,3,4],
        xaxis='x1',
        yaxis='y1')
tr2=dict(type='scatter',
        x=[1,2,3,4],
        y=[4, 2,1,3],
        xaxis='x1',
        yaxis='y2')
layout=dict(yaxis2=dict(side='right'))
fw=go.FigureWidget(data=[tr1, tr2], layout=layout)
fw

Even in this case the bar doesn't appear!!!

I tried the old way with offline, Figure and the declarative mode and still won't show it.

I'm using plotly 3.1.0 on python 3.6.6

Hi @vlizanae ,
It looks like you need to add the overlaying='y1' option to yaxis2

fig = go.FigureWidget()

trace1 = go.Bar()
trace1.x = [1, 2, 3, 4]
trace1.y = [1, 2, 3, 4]
trace1.yaxis = 'y1'
fig.add_trace(trace1)

trace2 = go.Scatter()
trace2.x = [1, 2, 3, 4]
trace2.y = [1, 2, 3, 4]
trace2.yaxis = 'y2'
fig.add_trace(trace2)

fig.layout['yaxis2'] = go.layout.YAxis(side='right', overlaying='y1')

fig

newplot-5
See also the examples at https://plot.ly/python/multiple-axes/

Thank you!

Was this page helpful?
0 / 5 - 0 ratings