简体   繁体   English

根据SEMI已知标签在堆积条形图中设置颜色

[英]Set colors in stacked bar plot per SEMI-known Label

I asked a similar question before which was answered perfectly. 我问了一个类似的问题,之前得到了完美的回答。

Set colors in stacked bar plot per label 在每个标签的堆积条形图中设置颜色

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))

作品

Now I am building on that for example, what if we don't know the label extension. 现在我正在建立这个,例如,如果我们不知道标签扩展怎么办。 (There is no way of knowing the label extensions in advance, and they will come with names like B1_Active, B2_Missing, B3_Double.. (The name after the B* is called the status of the box) (没有办法提前知道标签扩展名,它们会带有B1_Active,B2_Missing,B3_Double等名称。(B *后面的名称称为框的状态)

def gen_colors(df):
    col_d = {'B1': 'red','B1_Missing': 'firebrick', 'B2': 'black', 'B3': 'green'}
    return [col_d[col] for col in df.columns if 'B' in col]

t = {'DAY': [55,56,58,65], 'B1_Active': [2,6,6,1],  'B3_Missing': [0,1,0,1]}
    toy1 = pd.DataFrame(data = t)

    try:
        toy1.set_index('DAY').plot(kind='bar', stacked=True, color=gen_colors(toy1))
    except:
        toy1.set_index('DAY').plot(kind='bar', stacked=True)

This is just resulting to colors to be randomly selected. 这只是随机选择的颜色。 How can I make a dictionary to give different shades of colors of red when the label starts with B1 for example B1_Active = red, B1_Missing = firebrick etc.. My point is I want to keep the main color same for all combinations of B1 but add a slight shade to differentiate the status.. is this possible? 当标签以B1开头时,如何制作字典以给出不同色调的红色,例如B1_Active = red,B1_Missing = firebrick等。我的观点是我想保持B1的所有组合的主色相同但添加稍微遮挡以区分状态..这可能吗? I searched on "catch all" dictionary but could not work with regex.. Thanks 我搜索了“catch all”字典,但无法使用正则表达式..谢谢

在此输入图像描述

Also this is the palette: 这也是调色板:

在此输入图像描述

Your label extensions have the format B{number}_{status}. 您的标签扩展名的格式为B {number} _ {status}。 Consider we call them labex. 考虑我们称他们为labex。 The split function from the regular expression library allows you to separate the status from the rest. 正则表达式库中的split函数允许您将状态与其余部分分开。 For example 例如

import re
labex = 'B1_Active'
re.split(r'_', labex)
# ['B1', 'Active']

Now I will consider that you know how many B and status possibilities there are. 现在我会考虑你知道有多少B和状态可能性。 You can simply use a defaultdict (imported from collections) to store your colours according to the B value and a list to store the index given to each status. 您可以简单地使用defaultdict(从集合导入)根据B值存储颜色,并使用列表存储为每个状态指定的索引。

from collections import defaultdict
import re
colours = defaultdict(list)
colours['B1'] = ['xkcd:red', 'xkcd:bright red', 'xkcd:crimson']
colours['B2'] = ['xkcd:green', 'xkcd:grass green', 'xkcd:forest green']
colours['B3'] = ['xkcd:blue', 'xkcd:sky blue', 'xkcd:cerulean']
status = ['Active', 'Missing', 'Double']
labex = 'B2_Missing'
colours[re.split(r'_', labex)[0]][status.index(re.split(r'_', labex)[1])]
# xkcd:grass green

This should give you a place to start. 这应该给你一个开始的地方。 The status values do not have to be known in advance, as you can simply initialise status as '[]' and append to it new values as you encounter them. 状态值不必事先知道,因为您可以简单地将状态初始化为“[]”并在遇到它们时附加新值。 The same logic applies for the B values, but you need to have either way somewhere in memory the colour shades stored, or you need to be able to generate them on the fly. 相同的逻辑适用于B值,但您需要在内存中的某处存储颜色阴影,或者您需要能够动态生成它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM