条件颜色样式:plotly go.Bar

kms*_*kms 3 python plotly plotly-dash plotly-python

我想向堆积条形图添加条件样式。具体来说,堆栈的顶部栏将根据 x 轴值进行条件调整。

这是我的代码:

# sample df
df = pd.DataFrame({'Date': ['2010 - Q3','2010 - Q4','2011 - Q1','2011 - Q2','2011 - Q3','2011 - Q4'],
                   'Rate' : ['11.4','12.2','14.4','15.5','10.1','13.1'],
                   'Rate1': ['2.1','2.3','1.9','1.6','2.5','1.1']
                 })


clrs = 'rgb(222,0,0)'


fig = go.Figure(

            data=[

                  go.Bar(
                        x=df['Date'],
                        y=df['Rate'],
                        name='Natural Level'
                        ),

                  go.Bar(
                        x=df['Date'],
                        y=df['Rate1']),
                        name='Change',
                        marker=dict(color=clrs)
                        )

                  ],

            layout=go.Layout(

                title='Measuring excess demand and supply in the market.',

                xaxis=dict(
                    tickangle=90,
                    tickfont=dict(family='Rockwell', color='crimson', size=14)
                ),

                yaxis=dict(
                    title='Rate',
                    showticklabels=True
                ),

                barmode='stack',

            )
        ) 
Run Code Online (Sandbox Code Playgroud)

clrs变量采用以 x 轴值列表为条件的颜色值。IE clrs = rgb(222,0,0) if df['Date'] in lst else rgb(0,222,0)

其中lst是 x 轴值列表。

在此输入图像描述

Der*_*k O 5

Color原始方法:您可以根据您的条件在 df 中创建一列,然后将此列传递给color中的参数marker=dict(color=clrs)

编辑:您可以根据列是否Date在 lst 中对数据帧进行切片,然后分别使用跟踪添加数据帧的两个部分,并指定每个跟踪的颜色。这不是最漂亮的解决方案,如果您有两种以上的颜色,则应该用循环替换,但希望能在这里完成工作。

import pandas as pd
import plotly.graph_objects as go

## sample df
df = pd.DataFrame({'Date': ['2010 - Q3','2010 - Q4','2011 - Q1','2011 - Q2','2011 - Q3','2011 - Q4'],
                   'Rate' : ['11.4','12.2','14.4','15.5','10.1','13.1'],
                   'Rate1': ['2.1','2.3','1.9','1.6','2.5','1.1']
                 })

## first and last bar are in lst
lst = ['2010 - Q3', '2011 - Q4']

## NOT NEEDED ##

## add a color column to the df, apply along row
## df['Color'] = df.apply(lambda x: 'rgb(222,0,0)' if x['Date'] in lst else 'rgb(0,222,0)', axis=1)
## clrs = 'rgb(222,0,0)'

fig = go.Figure(

            data=[

                  go.Bar(
                        x=df['Date'],
                        y=df['Rate'],
                        name='Natural Level'
                        ),

                  ],

            layout=go.Layout(

                title='Measuring excess demand and supply in the market.',

                xaxis=dict(
                    tickangle=90,
                    tickfont=dict(family='Rockwell', color='crimson', size=14)
                ),

                yaxis=dict(
                    title='Rate',
                    showticklabels=True
                ),

                barmode='stack',

            )
        ) 

## part of the dataframe in the lst
fig.add_trace(go.Bar(
                        x=df[df['Date'].isin(lst)]['Date'],
                        y=df[df['Date'].isin(lst)]['Rate1'],
                        name='Change1',
                        marker=dict(color='rgb(222,0,0)')
                        )

    )
fig.add_trace(go.Bar(
                        x=df[~df['Date'].isin(lst)]['Date'],
                        y=df[~df['Date'].isin(lst)]['Rate1'],
                        name='Change2',
                        marker=dict(color='rgb(0,222,0)')
                        )

    )

fig.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述