yaxis
和offsetgroup
参数在go.Bar()
对象中,以及layout
参数中的yaxis2
参数在go.Figure()
对象中。代码如下:import plotly.graph_objects as go
animals = ['giraffes', 'orangutans', 'monkeys']
fig = go.Figure(
data=[
go.Bar(name='SF Zoo', x=animals, y=[200, 140, 210], yaxis='y', offsetgroup=1),
go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29], yaxis='y2', offsetgroup=2)
],
layout={
'yaxis': {'title': 'SF Zoo axis'},
'yaxis2': {'title': 'LA Zoo axis', 'overlaying': 'y', 'side': 'right'}
}
)
# Change the bar mode
fig.update_layout(barmode='group')
fig.show()
编辑
要制作一个水平条形图,请按照以下步骤进行:
import plotly.graph_objects as go
animals = ['giraffes', 'orangutans', 'monkeys']
fig = go.Figure(
data=[
go.Bar(name='SF Zoo', y=animals, x=[200, 140, 210], xaxis='x', offsetgroup=1, orientation='h'),
go.Bar(name='LA Zoo', y=animals, x=[12, 18, 29], xaxis='x2', offsetgroup=2, orientation='h')
],
layout={
'xaxis': {'title': 'SF Zoo axis'},
'xaxis2': {'title': 'LA Zoo axis', 'overlaying': 'x', 'side': 'top'}
}
)
# Change the bar mode
fig.update_layout(barmode='group')
fig.show()
对于现在看到这篇文章的人,plotly 现在在条形图中有一个 offsetgroup
属性来解决这个问题。设置 barmode='grouped'
仍然不起作用。