我正在嘗試繪制一個 x 軸強制為 12、1、2 的 df(12 月、1 月、2 月),但我看不到如何執行此操作。Matplot 一直想按 1、2、12 的順序繪制 x 軸。該示例的 DF (analogs_re) 部分列如下所示:
Month 2000 1995 2009 2014 1994 2003
0 12 -0.203835 0.580590 0.233124 0.490193 0.605808 0.016756
1 1 -0.947029 -1.239794 -0.977004 0.207236 0.436458 -0.501948
2 2 -0.059957 0.708626 0.111840 0.422534 1.051873 -0.149000
我需要用 x 軸按 12、1、2 順序繪制的 y 資料,如“月”列所示。
我的代碼:
fig, ax = plt.subplots()
#for name, group in groups:
analogs_re.set_index('Month').plot(figsize=(10,5),grid=True)
analogs_re.plot(x='Month', y=analogs_re.columns[1:len(analogs_re.columns)])

uj5u.com熱心網友回復:
當您設定Month為 x 軸時,顯然它會按數字順序(0、1、2、3...)進行繪制,因為連續序列不是以 12、1、2、... 開頭
訣竅是使用原始索引作為 x 軸,然后使用月份編號標記這些刻度:
fig, ax = plt.subplots()
analogs_re.drop(columns='Month').plot(figsize=(10,5), grid=True, ax=ax)
ax.set_xticks(analogs_re.index)
ax.set_xticklabels(analogs_re["Month"])

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428728.html
標籤:熊猫 matplotlib x轴
