簡體   English   中英

從一個數據框中繪制多條線並添加輔助軸以繪制不同的數據框 - Pandas

[英]Plotting multiple lines from one dataframe and adding a secondary axis to plot a different dataframe - Pandas

我在使用 matplotlib 時遇到問題。 我想用兩個 y 軸制作一個散點圖,兩個軸應該對應兩個不同的數據框。 但我遇到的問題是從一個數據框中繪制多條線。

第一個數據幀(IV_dv):

**year is the index
year    ninetyeight_x   EC_pmdv     ninetyeight_y   C_pmdv   ninetyeight     B_pmdv
2009    35.69           35.69       39.78           39.78    25.35           25.34
2010    24.0            29.84       31.50           35.64    12.83           19.08
2011    28.43           29.37       31.03           34.10    17.08           18.42
2012    28.24           26.89       37.392          33.31    22.016          17.31
2013    25.83           27.50       27.43           31.95    16.44           18.51

第二個數據框(rainavg):

year    precip
2010    161.798
2011    64.262
2012    62.991
2013    91.440

我想制作一個散點圖,左 y 軸是 PM2.5 濃度 (ug/m3),這是 EC_pmdv、C_pmdv 和 B_pmdv 列所描述的。 我希望正確的 y 軸是降水量 (mm)。 我希望 x 軸為年份。 我無法從 IVdv 繪制所有三行(我想繪制 x1=IVdv.year, y1=IVdv.EC_pmdv, x2=IVdv.year, y2=IVdv.C_pmdv, x3=IVdv.year, y3=IVdv.B_pmdv )。 我知道如何制作兩個 y 軸。 我附上了我到目前為止編寫的代碼:

fig, ax = plt.subplots()
ax.plot(x1=IVdv.index, y1=IVdv.EC_pmdv, x2=IVdv.index, y2=IVdv.C_pmdv, x3=IVdv.index,
        y3=IVdv.B_pmdv, color='k', linewidth=2.0, label1='El Centro',
        label2='Calexico', label3='Brawley', marker='v') 
ax.set_xticks(IVdv.index)
ax.title.set_text('PM2.5 Design Values')
ax.set_ylim(0,100)
ax.set_ylabel('PM2.5 Design Value (ug/m3)')
ax.set_xlabel('Year')

ax2=ax.twinx()
ax2.plot(rainavg.year, rainavg.precip, color='c', 
        linewidth=2.0, label='Imperial County annual precipitation', marker='o')
ax2.set_ylim(25,170)
ax2.set_xticks(rainavg.year)
ax2.set_ylabel('Annual Precipitation Average (mm)')
lines_1, labels_1 = ax.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()

lines = lines_1 + lines_2
labels = labels_1 + labels_2

ax.legend(lines, labels, loc='upper center')

但是,它只是繪制降雨數據。 我認為這不是正確的語法,但我似乎找不到任何東西來回答我的問題。 我只是發現,無論是講解如何從一個數據幀如何繪制兩個Y軸繪制多行論壇。

在一張圖中繪制多個 Pandas 數據框此鏈接說我需要使用 ax=ax 但我不確定如何在保留輔助 y 軸的同時對其進行格式化。

讓我們使用熊貓圖更容易:

fig, ax = plt.subplots(figsize=(10,8))
IVdv.plot(ax = ax, marker='v')
ax.title.set_text('PM2.5 Design Values')
ax.set_ylim(0,100)
ax.set_ylabel('PM2.5 Design Value (ug/m3)')
ax.set_xlabel('Year')
ax.set_xticks(IVdv.index)

ax2=ax.twinx()
ax2.plot(rainavg.year, rainavg.precip, color='c', 
        linewidth=2.0, label='Imperial County annual precipitation', marker='o')
ax2.set_ylim(25,170)
# ax2.set_xticks(rainavg.year)
ax2.set_ylabel('Annual Precipitation Average (mm)')
lines_1, labels_1 = ax.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()

lines = lines_1 + lines_2
labels = labels_1 + labels_2

ax.legend(lines, labels, loc='upper center')

輸出:

在此處輸入圖片說明

更新多個標記樣式且無行:

fig, ax = plt.subplots(figsize=(10,8))
markerstyles = ['v','o','+','*','.']
for i, col in zip(markerstyles, IVdv):
    IVdv[col].plot(ax = ax, marker=i, linestyle='none')
    
ax.title.set_text('PM2.5 Design Values')
ax.set_ylim(0,100)
ax.set_ylabel('PM2.5 Design Value (ug/m3)')
ax.set_xlabel('Year')
ax.set_xticks(IVdv.index)

ax2=ax.twinx()
ax2.plot(rainavg.year, rainavg.precip, color='c', 
        linewidth=2.0, label='Imperial County annual precipitation', marker='o')
ax2.set_ylim(25,170)
# ax2.set_xticks(rainavg.year)
ax2.set_ylabel('Annual Precipitation Average (mm)')
lines_1, labels_1 = ax.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()

lines = lines_1 + lines_2
labels = labels_1 + labels_2

ax.legend(lines, labels, loc='upper center')

輸出:

在此處輸入圖片說明

暫無
暫無

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

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