一种可能的解决办法:
设置range元素,使这两个图成正比,然后轴将对齐。基本上,你的问题是一个轴必须显示负数,而另一个轴不显示负数。通过告诉y2为了表示负数,我们达到了我们的目标。from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)
traces = [
go.Bar(
x=[1, 2, 3, 4],
y=[-1, 2, -3, 4],
name='y actual'
),
go.Bar(
x=[1],
y=[0],
name='y dummy',
hoverinfo='none',
showlegend=False
),
go.Bar(
x=[1],
y=[0],
yaxis='y2',
name='y2 dummy',
hoverinfo='none',
showlegend=False
),
go.Bar(
x=[1, 2, 3, 4],
y=[22, 2, 13, 25],
yaxis='y2',
name='y2 actual'
)
]
# layout
layout = go.Layout(
barmode='group',
yaxis=dict(
title='y actual',
rangemode="tozero",
#anchor='x',
overlaying='y2',
side="left",
range = [-4, 10]
),
yaxis2=dict(
title='y2 actual',
side='right',
rangemode = "tozero",
#anchor='x',
range = [-12, 30]
)
)
# make fig
fig = go.Figure(data=traces, layout=layout)
iplot(fig)