我正在處理這個資料集,我有下面的代碼
x = df2.groupby(by = ['LearnCode', 'Age']).size()
chart = x.unstack()
axs = chart.plot.barh(subplots=True,figsize=(20,50), layout=(9,1), legend=False, title=chart.columns.tolist())
ax_flat = axs.flatten()
for ax in ax_flat:
ax.yaxis.label.set_visible(False)

如何僅對每個圖的每個類別的值進行排序?
uj5u.com熱心網友回復:
你可以這樣做,但可能你必須分別繪制每個子圖。
df2 = pd.DataFrame({'LearnCode': ['A', 'B', 'B', 'B', 'B', 'A', 'C', 'C', 'B', 'A', 'C', 'C', 'B'],
'Age': [18, 18, 18, 18, 18, 18, 18, 24, 24, 24, 24, 24, 24]})
x = df2.groupby(by = ['LearnCode', 'Age']).size()
chart = x.unstack()
f, axs = plt.subplots(nrows=len(chart.columns), ncols=1, figsize=(20,10), sharex='col')
#to each subplot to have different color
colors = plt.rcParams["axes.prop_cycle"]()
for i, age in enumerate(chart):
chart[age].sort_values().plot.barh(title = age,
ax = axs[i],
color = next(colors)["color"],
xlabel = '')

PS。對我來說,擁有原始圖表比這樣的圖表更好(跟蹤組之間的差異要容易得多)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406718.html
標籤:
上一篇:同時創建多個matplotlib圖表時如何顯示網格?
下一篇:根據情節選擇復制檔案
