plotly express:如何控制bars的起始位置?

plotly express: how to control bars start position?

我有这样简单的股票数据:

Company Performance label
0 TEVA -25.46 neg
1 AAL -17.30 neg
2 OXY -11.56 neg
3 LBTYK -10.33 neg
4 KHC -10.00 neg
5 AAPL 13.16 pos
6 PG 13.43 pos
7 UPS 16.03 pos
8 STNE 17.58 pos
9 RH 47.78 pos

我想创建一个条形图,其中粗线为零(比如 10px),条形图开始于 y = +5(正数)和 y = -5(负数)。是否可以在 plotly express 中控制条形图的起始位置?

代码如下:

fig = px.bar(df_win_los, x='Company', y='Performance', color='label',barmode='relative', color_discrete_map={'neg':'orangered', 'pos':'limegreen'}, 
         title=dict(text='BH Q419 top winners and losers', x=0.5, xanchor='center'),
         text='Performance', template='none+xgridoff+ygridoff+plotly_dark')

fig.update_layout(font=dict(size=22 ), showlegend=False, yaxis_title='Performance (%)', xaxis_title='Symbols')
fig.update_yaxes(zeroline=True, zerolinewidth=10, zerolinecolor='rgb(90,90,90)', nticks=5)

这会生成条形图,其中条形图从零开始(而不是分别从 +5、-5 开始),如下所示:enter image description here

感谢任何想法

为了实现它,您应该设置参数 base,它在 go.Bar 中可用,但在 px.bar 中不可用。

import pandas as pd
import plotly.graph_objs as go

df_neg = df[df["label"]=="neg"]
df_pos =  df[df["label"]=="pos"]
fig = go.Figure()
fig.add_trace(
    go.Bar(x=df_neg.index,
           y=df_neg["Performance"],
           marker_color="orangered",
           showlegend=False,
           base=-1.7))
fig.add_trace(
    go.Bar(x=df_pos.index,
           y=df_pos["Performance"],
           marker_color="limegreen",
           showlegend=False,
           base=+1.7))
fig.update_yaxes(zeroline=True,
                 zerolinewidth=10,
                 zerolinecolor='rgb(90,90,90)', nticks=5)
fig.update_xaxes(ticktext=df["Company"],
                 tickvals=df.index)
fig.update_layout(font=dict(size=22 ),
                  yaxis_title='Performance (%)',
                  xaxis_title='Symbols',
                  template='none+xgridoff+ygridoff+plotly_dark'
                 )
fig.show()