在每个标签的堆积条形图中设置颜色
Set colors in stacked bar plot per label
我想设置每次 运行 这个条形图时弹出相同的颜色。例如:B1 = 绿色,B2,红色,B3 = 蓝色等
到目前为止,我已经尝试过 .setcolor,但它没有让我为各个框编号(B1、B2 等)设置颜色——我无法弄清楚。
import pandas as pd
import seaborn as sns
d = {'DAY': [55,56,58,65], 'B1': [2,6,6,1], 'B2': [1,0,21,0], 'B3': [0,1,0,1]}
data1= pd.DataFrame(data = d)
sns.set()
data1.set_index('DAY').plot(kind='bar', stacked=True)
这行得通,但是一旦我有了新数据,它就会为 B1、B2、B3 等分配不同的颜色..
例如,让我们给它一些玩具数据:
t = {'DAY': [55,56,58,65], 'B1': [2,6,6,1], 'B3': [0,1,0,1]}
toy1= pd.DataFrame(data = t)
sns.set()
toy1.set_index('DAY').plot(kind='bar', stacked=True)
B3 在这里是橙色的,而第一个是绿色的。
创建字典并将列映射到这些颜色。因为我们遍历列,所以颜色的顺序与列的绘制顺序相同。您需要为所有不同的列定义颜色。
def gen_colors(df):
col_d = {'B1': 'red', 'B2': 'black', 'B3': 'green'}
return [col_d[col] for col in df.columns if 'B' in col]
sns.set()
d = {'DAY': [55,56,58,65], 'B1': [2,6,6,1], 'B2': [1,0,21,0], 'B3': [0,1,0,1]}
data1 = pd.DataFrame(data = d)
data1.set_index('DAY').plot(kind='bar', stacked=True, color=gen_colors(data1))
t = {'DAY': [55,56,58,65], 'B1': [2,6,6,1], 'B3': [0,1,0,1]}
toy1 = pd.DataFrame(data = t)
toy1.set_index('DAY').plot(kind='bar', stacked=True, color=gen_colors(toy1))
但是,这并不能确保顺序一致。也就是说,如果您的 DataFrame 排序不同,虽然 B3 始终是相同的颜色,但在堆叠时它可能会高于或低于 B1。一个更一致的解决方案是重新索引。您必须包括 all Bi
,它们在您所有需要一致绘制的 DataFrame 中找到。这里我选择了1-9的任意大数:
t = {'DAY': [55,56,58,65], 'B3': [0,1,0,1], 'B1': [2,6,6,1]}
toy1 = pd.DataFrame(data = t)
toy1 = toy1.reindex(['DAY'] + [f'B{i}' for i in range(1,10)], axis=1)
toy1.set_index('DAY').plot(kind='bar', stacked=True)
虽然 B3 在 DataFrame 中排在第一位,但它仍然绘制在 B1 之上,并且是循环中的第三种颜色。
我想设置每次 运行 这个条形图时弹出相同的颜色。例如:B1 = 绿色,B2,红色,B3 = 蓝色等
到目前为止,我已经尝试过 .setcolor,但它没有让我为各个框编号(B1、B2 等)设置颜色——我无法弄清楚。
import pandas as pd
import seaborn as sns
d = {'DAY': [55,56,58,65], 'B1': [2,6,6,1], 'B2': [1,0,21,0], 'B3': [0,1,0,1]}
data1= pd.DataFrame(data = d)
sns.set()
data1.set_index('DAY').plot(kind='bar', stacked=True)
这行得通,但是一旦我有了新数据,它就会为 B1、B2、B3 等分配不同的颜色..
例如,让我们给它一些玩具数据:
t = {'DAY': [55,56,58,65], 'B1': [2,6,6,1], 'B3': [0,1,0,1]}
toy1= pd.DataFrame(data = t)
sns.set()
toy1.set_index('DAY').plot(kind='bar', stacked=True)
B3 在这里是橙色的,而第一个是绿色的。
创建字典并将列映射到这些颜色。因为我们遍历列,所以颜色的顺序与列的绘制顺序相同。您需要为所有不同的列定义颜色。
def gen_colors(df):
col_d = {'B1': 'red', 'B2': 'black', 'B3': 'green'}
return [col_d[col] for col in df.columns if 'B' in col]
sns.set()
d = {'DAY': [55,56,58,65], 'B1': [2,6,6,1], 'B2': [1,0,21,0], 'B3': [0,1,0,1]}
data1 = pd.DataFrame(data = d)
data1.set_index('DAY').plot(kind='bar', stacked=True, color=gen_colors(data1))
t = {'DAY': [55,56,58,65], 'B1': [2,6,6,1], 'B3': [0,1,0,1]}
toy1 = pd.DataFrame(data = t)
toy1.set_index('DAY').plot(kind='bar', stacked=True, color=gen_colors(toy1))
但是,这并不能确保顺序一致。也就是说,如果您的 DataFrame 排序不同,虽然 B3 始终是相同的颜色,但在堆叠时它可能会高于或低于 B1。一个更一致的解决方案是重新索引。您必须包括 all Bi
,它们在您所有需要一致绘制的 DataFrame 中找到。这里我选择了1-9的任意大数:
t = {'DAY': [55,56,58,65], 'B3': [0,1,0,1], 'B1': [2,6,6,1]}
toy1 = pd.DataFrame(data = t)
toy1 = toy1.reindex(['DAY'] + [f'B{i}' for i in range(1,10)], axis=1)
toy1.set_index('DAY').plot(kind='bar', stacked=True)
虽然 B3 在 DataFrame 中排在第一位,但它仍然绘制在 B1 之上,并且是循环中的第三种颜色。