简体   繁体   English

Plotly中双轴堆积条形图

[英]Stacked bar chart with dual axis in Plotly

Please consider the example below:请考虑以下示例:

import pandas as pd

mydata = pd.DataFrame({'time': [pd.to_datetime('2021-01-02'),
                               pd.to_datetime('2021-01-02'),
                               pd.to_datetime('2021-01-03'),
                               pd.to_datetime('2021-01-03')],
                      'group': ['a','b','a','b'],
                      'value': [1,3,3,5]})

myline = pd.DataFrame({'time': [pd.to_datetime('2021-01-02'),
                                 pd.to_datetime('2021-01-03')],
                       'value':[39,46]})

mydata
Out[222]: 
        time group  value
0 2021-01-02  a     1    
1 2021-01-02  b     3    
2 2021-01-03  a     3    
3 2021-01-03  b     5    

I am trying to create a dual axis chart with the line in myline on one axis and the stacked bar charts from mydata on another.我正在尝试创建一个双轴图表,其中一个轴上的myline中的线和另一个轴上的mydata堆积条形图。

My code below almost achieves that, but there are few issues:我下面的代码几乎实现了这一点,但几乎没有问题:

  1. The bars are not stacked !酒吧没有堆叠!
  2. Because my data has many different categories, I need to use a colorscale with many different colors like dark24 ( https://plotly.com/python/discrete-color/ ).因为我的数据有许多不同的类别,所以我需要使用具有许多不同colorscale的色标,例如dark24https://plotly.com/python/discrete-color/ )。 How can I specify that for the fill color of the bars?如何为条形的填充颜色指定它?

Any suggestion greatly appreciated.任何建议都非常感谢。 Thanks!谢谢!

在此处输入图像描述

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(
    go.Scatter(x=myline['time'], 
               y=myline["value"], 
               name="Column1", 
               mode="lines"),
    secondary_y=True
)

layout = go.Layout(barmode='stack')

for t in mydata['group'].unique():
    dfp = mydata.loc[mydata['group']==t]
    fig.add_trace(go.Bar(x=dfp['time'], 
                          y = dfp['value'], 
                          name=t,
                          text = t),
                  secondary_y=False)

fig.update_xaxes(title_text="Letter")

# Set y-axes titles
fig.update_yaxes(title_text="Column2", secondary_y=False)
fig.update_yaxes(title_text="Column1", secondary_y=True)
fig.update(layout_showlegend=False)
fig.show()

The stacked graph is set, but it was not enabled, so I changed the bar graph mode to stacked along with hiding the legend.堆叠图已设置,但未启用,因此我将条形图模式更改为堆叠并隐藏图例。 Also, I couldn't find a colormap setting in graph_objects, so I updated the default colormap with the desired colormap.此外,我在 graph_objects 中找不到颜色图设置,因此我使用所需的颜色图更新了默认颜色图。 I checked the test data with three objects, and they are updated with the new color map.我用三个对象检查了测试数据,它们更新为新颜色 map。 However, I did not go as far as to check the 24 colors, so you will have to check it yourself.但是,我没有 go 来检查 24 colors,所以您必须自己检查。

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px

colors = px.colors.qualitative.Dark24

fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(
    go.Scatter(x=myline['time'], 
               y=myline["value"], 
               name="Column1", 
               mode="lines"),
    secondary_y=True
)

#layout = go.Layout(barmode='stack')

for t in mydata['group'].unique():
    dfp = mydata.loc[mydata['group']==t]
    fig.add_trace(go.Bar(x=dfp['time'], 
                          y = dfp['value'], 
                          name=t,
                          text = t),
                  secondary_y=False)

fig.update_xaxes(title_text="Letter")

# Set y-axes titles
fig.update_yaxes(title_text="Column2", secondary_y=False)
fig.update_yaxes(title_text="Column1", secondary_y=True)
fig.update_layout(showlegend=False, barmode='stack')

fig.layout.colorway = colors
fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM