pandas 基于 DataFrame 列名称的颜色 seaborn boxplot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33544910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Color seaborn boxplot based in DataFrame column name
提问by Freek
I'd like to create a list of boxplots with the color of the box dependent on the name of the pandas.DataFrame column I use as input.
我想根据我用作输入的 pandas.DataFrame 列的名称创建一个箱线图列表,其中框的颜色。
The column names contain strings that indicate an experimental condition based on which I want the box of the boxplot colored.
列名称包含指示实验条件的字符串,我希望根据该条件为箱线图的框着色。
I do this to make the boxplots:
我这样做是为了制作箱线图:
sns.boxplot(data = data.dropna(), orient="h")
plt.show()
This creates a beautiful list of boxplots with correct names. Now I want to give every boxplot that has 'prog +, DMSO+' in its name a red color, leaving the rest as blue.
这将创建一个带有正确名称的漂亮箱线图列表。现在,我想给每个名称中包含“prog +, DMSO+”的箱线图设置为红色,其余部分为蓝色。
I tried creating a dictionary with column names as keys and colors as values:
我尝试创建一个以列名作为键和颜色作为值的字典:
color = {}
for column in data.columns:
if 'prog+, DMSO+' in column:
color[column] = 'red'
else:
color[column] = 'blue'
And then using the dictionary as color:
然后使用字典作为颜色:
sns.boxplot(data = data.dropna(), orient="h", color=color[column])
plt.show()
This does not work, understandably (there is no loop to go through the dictionary). So I make a loop:
这是行不通的,可以理解(没有遍历字典的循环)。所以我做了一个循环:
for column in data.columns:
sns.boxplot(data = data[column], orient='h', color=color[column])
plt.show()
This does make boxplots of different colors but all on top of each other and without the correct labels. If I could somehow put these boxplot nicely in one plot below each other I'd be almost at what I want. Or is there a better way?
这确实制作了不同颜色的箱线图,但都在彼此之上并且没有正确的标签。如果我能以某种方式将这些箱线图很好地放在一个彼此下方的图中,我几乎可以达到我想要的效果。或者,还有更好的方法?
回答by mwaskom
You should use the palette
parameter, which handles multiple colors, rather than color
, which handles a specific one. You can give palette
a name, an ordered list, or a dictionary. The latter seems best suited to your question:
您应该使用palette
处理多种颜色的参数,而不是color
处理特定颜色的。您可以提供palette
名称、有序列表或字典。后者似乎最适合您的问题:
import seaborn as sns
sns.set_color_codes()
tips = sns.load_dataset("tips")
pal = {day: "r" if day == "Sat" else "b" for day in tips.day.unique()}
sns.boxplot(x="day", y="total_bill", data=tips, palette=pal)
回答by tmdavison
You can set the facecolor of individual boxes after plotting them all in one go, using ax.artists[i].set_facecolor('r')
您可以在一次性绘制完各个框后设置它们的 facecolor,使用 ax.artists[i].set_facecolor('r')
For example:
例如:
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(
[[2, 4, 5, 6, 1],
[4, 5, 6, 7, 2],
[5, 4, 5, 5, 1],
[10, 4, 7, 8, 2],
[9, 3, 4, 6, 2],
[3, 3, 4, 4, 1]
],columns=['bar', 'prog +, DMSO+ 1', 'foo', 'something', 'prog +, DMSO+ 2'])
ax = sns.boxplot(data=df,orient='h')
boxes = ax.artists
for i,box in enumerate(boxes):
if 'prog +, DMSO+' in df.columns[i]:
box.set_facecolor('r')
else:
box.set_facecolor('b')
plt.tight_layout()
plt.show()