【发布时间】:2020-04-17 19:00:16
【问题描述】:
我使用下面的代码创建了一个 Seaborn 条形图(它来自 https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/)
我希望所有条都堆叠起来没有空格,但一直无法这样做。如果我添加宽度,它会抱怨 barh 中的多个宽度值。这可能是因为 seaborn 有自己的算法来确定宽度。周围有吗?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Read data
df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/email_campaign_funnel.csv")
# Draw Plot
plt.figure(figsize=(13, 10), dpi=80)
group_col = 'Gender'
order_of_bars = df.Stage.unique()[::-1]
colors = [plt.cm.Spectral(i/float(len(df[group_col].unique())-1)) for i in
range(len(df[group_col].unique()))]
for c, group in zip(colors, df[group_col].unique()):
sns.barplot(x='Users', y='Stage', data=df.loc[df[group_col]==group, :],
order=order_of_bars, color=c, label=group)
# Decorations
plt.xlabel("$Users$")
plt.ylabel("Stage of Purchase")
plt.yticks(fontsize=12)
plt.title("Population Pyramid of the Marketing Funnel", fontsize=22)
plt.legend()
plt.show()
【问题讨论】:
标签: python matplotlib seaborn