Python 条件颜色样式:plotly go.Bar
我想将条件样式添加到绘图堆叠的条形图中。具体来说,堆栈的顶部栏将基于x轴值设置条件 这是我的密码:Python 条件颜色样式:plotly go.Bar,python,plotly,plotly-dash,plotly-python,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'],
# 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轴值列表为条件获取颜色值。i、 eclrs=rgb(222,0,0),如果在lst else rgb(0222,0)中df['Date']
其中lst
是x轴值的列表
原始方法:您可以根据您的条件在df中创建一个Color
列,然后将此列传递给marker=dict(Color=clrs)
中的Color
参数
编辑:您可以根据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()
基本形式是如何为x轴创建颜色列表,因此有许多不同的方法。我建议将此作为一个示例,说明如何创建带条件的颜色列表
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()
您能提供您的df或样品df吗?这将使您的代码具有可复制性,以便其他人可以更容易地帮助您。在示例df中添加。这很有效。然而,传奇并没有改变。如何更改图例以显示两种颜色?