简体   繁体   English

Seaborn 条形图条之间没有空格

[英]No whitespace between Seaborn barplot bars

I created a Seaborn barplot using the code below (it comes from https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/ )我使用下面的代码创建了一个 Seaborn 条形图(它来自https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/

I would like all the bars to stack up without whitespace, but have been unable to do so.我希望所有的条形都可以在没有空格的情况下堆叠起来,但一直无法这样做。 If I add width it complains about multiple values for width in barh.如果我添加宽度,它会抱怨 barh 中宽度的多个值。 This is probably as seaborn has its own algo to determine the width.这可能是因为 seaborn 有自己的算法来确定宽度。 Is there anyway around it?反正周围有吗?

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

在此处输入图片说明

Not a matplotlib expert by any means, so there may be a better way to do this.无论如何都不是 matplotlib 专家,所以可能有更好的方法来做到这一点。 Perhaps you can do something like the following, which is similar to the approach in this answer :也许您可以执行以下操作,这类似于此答案中的方法:

# Draw Plot
fig, ax = plt.subplots(figsize=(13, 10), dpi=80)
...

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, ax=ax)

# Adjust height    
for patch in ax.patches:
    current_height = patch.get_height()
    patch.set_height(1)
    patch.set_y(patch.get_y() + current_height - 1)

在此处输入图片说明

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

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