首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >条件颜色样式: plotly go.Bar

条件颜色样式: plotly go.Bar
EN

Stack Overflow用户
提问于 2020-09-05 13:34:33
回答 2查看 1.2K关注 0票数 3

我想将条件样式添加到绘图堆叠的条形图中。具体地说,堆栈的顶部栏将是基于x轴值的条件。

下面是我的代码:

代码语言:javascript
复制
# 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',

            )
        ) 

clrs变量根据x轴值列表获取颜色值。即clrs = rgb(222,0,0) if df['Date'] in lst else rgb(0,222,0)

其中lst是x轴值的列表。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-05 15:55:06

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

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

代码语言:javascript
复制
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()

票数 5
EN

Stack Overflow用户

发布于 2020-09-05 16:13:01

基本形式是如何为x轴创建颜色列表,因此有许多不同的方法。我建议将此作为如何在有条件的情况下制作颜色列表的示例。

代码语言:javascript
复制
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']
                 })

clrs = []
for i in range(len(df)):
    if df.loc[i,'Date'][:4] == '2010':
        clrs.append('rgb(222,0,0)')
    else:
        clrs.append('rgb(0,100,0)')
#clrs = ['rgb(222,0,0)','rgb(222,0,0)','rgb(0,100,0)','rgb(0,100,0)','rgb(0,100,0)','rgb(0,100,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',
            )
        )

fig.show()

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63750767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档