【发布时间】:2020-01-30 08:46:28
【问题描述】:
我正在尝试在 seaborn 中创建一个堆积条形图作为 shown here。
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")
# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(6, 15))
# Load the example car crash dataset
crashes = sns.load_dataset("car_crashes").sort_values("total", ascending=False)
# Plot the total crashes
sns.set_color_codes("pastel")
sns.barplot(x="total", y="abbrev", data=crashes,
label="Total", color="b")
# Plot the crashes where alcohol was involved
sns.set_color_codes("muted")
sns.barplot(x="alcohol", y="abbrev", data=crashes,
label="Alcohol-involved", color="b")
# Add a legend and informative axis label
ax.legend(ncol=2, loc="lower right", frameon=True)
ax.set(xlim=(0, 24), ylabel="",
xlabel="Automobile collisions per billion miles")
sns.despine(left=True, bottom=True)
我注意到在代码中每个barplot 中都手动设置了颜色。这看起来很乏味,而且我知道 seaborn 有一些很棒的颜色可以利用。制作如上所示的堆叠条形图时,如何自动设置每个单独系列(堆叠)的颜色?
提前致谢
编辑:
为了回答下面的问题,这里是一个堆叠图的示例,当我没有手动定义每个系列的颜色时,我使用了我用来实现它的代码。
f, ax = plt.subplots()
xtics = df.index.astype('str')
sns.color_palette("Set1", n_colors=6, desat=.5)
sns.barplot(xtics, df["Series1"], label="Series1")
sns.barplot(xtics, df["Series2"], bottom=df["Series1"], label="Series2")
sns.barplot(xtics, df["Series3"], bottom=df["Series 2"], label="Series3")
sns.barplot(xtics, df["Series4"], bottom=df["Series3"], label="Series4")
sns.barplot(xtics, df["Series5"], bottom=df["Series4"], label = "Series5")
sns.barplot(xtics, df["Series6"], bottom=df["Series5"], label = "Series6")
ax.legend(ncol=1, frameon=True, loc='upper left', bbox_to_anchor=(1, 0.5))
ax.set(title="Different Color for Each Bars, Same Color for each series", ylabel="YAxis", xlabel="XAxis")
sns.despine(left=True, bottom=True)
这是我所追求的:
实际颜色并不重要,我只是希望它们对于每个系列都不同,而无需自己手动选择每种颜色。注意下面代码中的color=。
f, ax = plt.subplots()
xtics = df.index.astype('str')
sns.color_palette("Set1", n_colors=6, desat=.5)
sns.barplot(xtics, df["Series1"], label="Series1", color="#000000")
sns.barplot(xtics, df["Series2"], bottom=df["Series1"], label="Series2", color="#004949")
sns.barplot(xtics, df["Series3"], bottom=df["Series 2"], label="Series3", color="#009292")
sns.barplot(xtics, df["Series4"], bottom=df["Series3"], label="Series4", color="#ff6db6")
sns.barplot(xtics, df["Series5"], bottom=df["Series4"], label = "Series5", color="#490092")
sns.barplot(xtics, df["Series6"], bottom=df["Series5"], label = "Series6", color="#ffb6db")
ax.legend(ncol=1, frameon=True, loc='upper left', bbox_to_anchor=(1, 0.5))
ax.set(title="Different Color for Each Bars, Same Color for each series", ylabel="YAxis", xlabel="XAxis")
sns.despine(left=True, bottom=True)
【问题讨论】:
-
请更好地解释所需的输出。您不必指定颜色,让 seaborn 为您选择一种。但请注意,在没有
hue=的情况下,seaborn 会为每个条形选择不同的颜色。这是您想要的,还是您希望所有条形图都具有相同的颜色,但为每个堆栈级别使用两种不同的颜色? -
您显示的代码确实通过
sns.set_color_codes(...)行使用了调色板。因此,我认为不清楚要求的是什么。 -
@ImportanceOfBeingErnest 你说得对,它确实允许我为系列使用调色板,但如果我不必为我添加的每个系列手动选择颜色,我真的更喜欢(seaborn.pydata.org/generated/seaborn.set_color_codes.html)。如果我用一个系列创建一个条形图,它会自动为该系列中的每个条形选择不同的颜色。我希望它对不同的系列而不是不同的酒吧做同样的事情。
-
@DizietAsahi,seaborn 肯定会自动为每个条形选择颜色;我希望它自动为每个系列选择一种颜色,而不是像文档中所示手动设置它:seaborn.pydata.org/generated/seaborn.set_color_codes.html
-
@ImportanceOfBeingErnest 我已经更新了问题以试图澄清
标签: python matplotlib graph seaborn