## 实际Python：怎么实现用多个Y轴对齐分组条形图中的X轴？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (625)

```import plotly.offline as plt
import plotly.graph_objs as go
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 = go.Layout(barmode='group',
yaxis=dict(title='y actual', rangemode="tozero", anchor='x', overlaying='y2'),
yaxis2=dict(title='y2 actual', side='right', rangemode = "tozero", anchor='x'))
fig = go.Figure(data=traces, layout=layout)
plt.iplot(fig)```

### 1 个回答

```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)```