我想根據位置以及該用戶是否離開來顯示收入的分布。對于這個任務,我應該使用哪個圖表。如何根據其他 2 個分類列顯示數字列的分布?

uj5u.com熱心網友回復:
您可以使用
@JohanC 在評論中建議的另一個解決方案是使用小提琴圖,在 x 軸上您有不同的位置,在 y 軸上是收入,使用色調來區分離開的用戶和沒有離開的用戶't (此外,小提琴被色調分成兩半):
fig, ax = plt.subplots()
sns.violinplot(ax = ax, data = df, x = 'Location', y = 'Income', hue = 'Left', split = True)
plt.show()

如果不允許使用,則可以通過僅在不同位置上回圈seaborn使用來實作與第一個示例類似的結果:matplotlib
fig, ax = plt.subplots(1, 2, sharex = 'all', sharey = 'all', figsize = (8, 4))
for location in df['Location'].unique():
ax[0].hist(x = df[(df['Location'] == location) & (df['Left'] == 0)]['Income'], label = location, alpha = 0.7, edgecolor = 'black')
ax[1].hist(x = df[(df['Location'] == location) & (df['Left'] == 1)]['Income'], label = location, alpha = 0.7, edgecolor = 'black')
ax[0].set_title('Left = 0')
ax[1].set_title('Left = 1')
ax[0].set_xlabel('Income')
ax[1].set_xlabel('Income')
ax[0].set_ylabel('Count')
ax[1].legend(title = 'Location', loc = 'upper left', bbox_to_anchor = (1.05, 1))
plt.tight_layout()
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/412609.html
標籤:
