条件颜色样式 : plotly go.Bar
Conditional color style : plotly go.Bar
我想将条件样式添加到 plotly stacked bar plot。具体来说,堆栈的顶部栏将根据 x-axis 值进行设置。
这是我的代码:
# 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-axis 个值的列表。
原方法:你可以根据你的条件在你的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-axis创建一个颜色列表,所以有很多不同的方法。我建议将此作为如何制作带条件的颜色列表的示例。
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()
我想将条件样式添加到 plotly stacked bar plot。具体来说,堆栈的顶部栏将根据 x-axis 值进行设置。
这是我的代码:
# 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-axis 个值的列表。
原方法:你可以根据你的条件在你的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-axis创建一个颜色列表,所以有很多不同的方法。我建议将此作为如何制作带条件的颜色列表的示例。
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()