簡體   English   中英

如何在 Python 中繪制二進制彩色直方圖?

[英]How to plot binary colored histogram in Python?

我正在研究具有多個定量變量的二元分類項目。 我想在探索性分析階段繪制如下所示的直方圖。 不過在網上研究了兩天,還是找到了Python的解決方法。

在 R 中,我知道如何使用一行代碼: ggplot(train, aes(var_x, fill = var_y)) 使用 Python,誰能告訴我 matplotlib 或 seaborn 的等效語法?

在此處輸入圖片說明

要獲得類似 seaborn 中的重疊條形圖,它比 R 中的一行代碼稍微復雜一些。

您實際上需要創建兩個圖,一個用於“背景”,一個用於“前景”

stacked_bar_data["total"] = stacked_bar_data.Series1 + stacked_bar_data.Series2 # Creates 'total column of two series you're interested in.


#Plot 1 - background - "total" (top) series
sns.barplot(x = stacked_bar_data.Group, y = stacked_bar_data.total, color = "blue")

#Plot 2 - overlay - "bottom" series
bottom_plot = sns.barplot(x = stacked_bar_data.Group, y = stacked_bar_data.Series1, color = "red")


topbar = plt.Rectangle((0,0),1,1,fc="red", edgecolor = 'none')
bottombar = plt.Rectangle((0,0),1,1,fc='#0000A3',  edgecolor = 'none')
l = plt.legend([bottombar, topbar], ['Bottom Bar', 'Top Bar'], loc=1, ncol = 2, prop={'size':16})
l.draw_frame(False)

以上代碼摘自: http : //randyzwitch.com/creating-stacked-bar-chart-seaborn/

暫無
暫無

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

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