簡體   English   中英

twinx和sns.barplot seaborn是重疊的酒吧

[英]twinx and sns.barplot seaborn are overlapping bars

我想用sns.seaborn顯示np.sumnp.mean 2個不同的軸線(與ax2 = ax1.twinx()我假定)。 我的問題是圖形重疊且不可讀。

我能正確解決問題嗎? 我該怎么做才能使那些酒吧彼此相鄰?

import seaborn as sns
tips = sns.load_dataset("tips")
f, ax1 = plt.subplots()
ax2 = ax1.twinx()
sns.barplot(x="day", y="total_bill", data=tips, estimator=np.mean, ax=ax1)
sns.barplot(x="day", y="total_bill", data=tips, estimator=np.sum, ax=ax2, ci=None)

謝謝你的幫助

拉里

您最好將數據與pandas聚合,然后再使用標准的matplotlib ax.bar()函數來繪制結果數據ax.bar()

如果您堅持使用seaborn,則以下內容是獲得所需結果的一種“駭人聽聞”的方式。

為了將每個小節稍微向左或向右移動,我創建了一個用於分類色調的虛擬分類列,並使用hue_order=參數請求將其中一個圖放在左邊,然后將相反,第二個條形圖在右側。

# create a dummy categorical column with only one category
invoicedb.loc[:,'dummy'] = 'dummy'

f, ax1 = plt.subplots()
ax2 = ax1.twinx()
sns.barplot(x="InvoiceMonth", y="TotalInvoice", hue='dummy', data=invoicedb, estimator = np.mean, ax = ax1, color = 'r', hue_order=['dummy','other'])
sns.barplot(x="InvoiceMonth", y="TotalInvoice", hue='dummy', data=invoicedb, estimator = np.sum, ci = None, ax = ax2, color = 'b', hue_order=['other','dummy'])
# hue-nesting automatically creates a legend that we need to remove by hand
ax1.legend_.remove()
ax2.legend_.remove()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM