我正在尝试在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)
答案 0 :(得分:1)
我只使用颜色列表并使用循环,或使用所述列表的迭代器,如下所示:
np.random.seed(1234)
N_series = 6
N_bars = 4
xticks = ['2016', '2017', '2018', '2019']
df = pd.DataFrame({f'Series{i+1}': np.random.randint(1,10,size=(N_bars,)) for i in range(N_series)}, index=xticks)
colors = iter(sns.color_palette('Set1', n_colors=N_series, desat=.75))
fig, ax = plt.subplots()
ax.bar(xticks, df["Series1"], bottom=0, label="Series1", color=next(colors))
ax.bar(xticks, df["Series2"], bottom=df["Series1"], label="Series2", color=next(colors))
ax.bar(xticks, df["Series3"], bottom=df["Series2"]+df["Series1"], label="Series3", color=next(colors))
ax.bar(xticks, df["Series4"], bottom=df["Series3"]+df["Series2"]+df["Series1"], label="Series4", color=next(colors))
ax.bar(xticks, df["Series5"], bottom=df["Series4"]+df["Series3"]+df["Series2"]+df["Series1"], label="Series5", color=next(colors))
ax.bar(xticks, df["Series6"], bottom=df["Series5"]+df["Series4"]+df["Series3"]+df["Series2"]+df["Series1"], label="Series6", color=next(colors))
ax.legend(ncol=1, frameon=True, loc='upper left', bbox_to_anchor=(1, 0.5))
ax.set(title="Same Color for each series", ylabel="YAxis", xlabel="XAxis")
sns.despine(left=True, bottom=True)
PS
plt.bar()
不同的方式使用seaborn时,为什么要使用seaborn?在我的示例中,我直接使用了bar()
,但是如果替换为sns.barplot()
bottom=
参数中犯了一个错误,必须提供以下所有小节的和才能获得所需的输出