簡體   English   中英

Seaborn 條形圖條之間沒有空格

[英]No whitespace between Seaborn barplot bars

我使用下面的代碼創建了一個 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()

在此處輸入圖片說明

無論如何都不是 matplotlib 專家,所以可能有更好的方法來做到這一點。 也許您可以執行以下操作,這類似於此答案中的方法:

# 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