【问题标题】:No whitespace between Seaborn barplot barsSeaborn barplot 条之间没有空格
【发布时间】: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


    【解决方案1】:

    无论如何都不是 matplotlib 专家,所以可能有更好的方法来做到这一点。或许你可以做如下的事情,类似于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)
    

    【讨论】:

      猜你喜欢
      • 2020-06-27
      • 2019-05-12
      • 2021-03-16
      • 2021-03-04
      • 2021-09-03
      • 1970-01-01
      • 2018-08-01
      • 2020-07-13
      • 2017-11-11
      相关资源
      最近更新 更多