从一个数据框中绘制多条线并添加辅助轴以绘制不同的数据框 - Pandas [英] Plotting multiple lines from one dataframe and adding a secondary axis to plot a different dataframe - Pandas
问题描述
我在使用 matplotlib 时遇到了问题.我想用两个 y 轴制作一个散点图,两个轴应该对应两个不同的数据框.但我遇到的问题是从一个数据框中绘制多条线.
第一个数据帧(IV_dv):
**year 是索引年份 Ninetyeight_x EC_pmdv Ninetyeight_y C_pmdv Ninetyeight B_pmdv2009 年 35.69 35.69 39.78 39.78 25.35 25.342010 24.0 29.84 31.50 35.64 12.83 19.082011 28.43 29.37 31.03 34.10 17.08 18.422012 28.24 26.89 37.392 33.31 22.016 17.312013 25.83 27.50 27.43 31.95 16.44 18.51
第二个数据帧(rainavg):
年降水量2010 161.7982011 64.2622012 62.9912013 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 设计值')ax.set_ylim(0,100)ax.set_ylabel('PM2.5 设计值 (ug/m3)')ax.set_xlabel('年')ax2=ax.twinx()ax2.plot(rainavg.year,rainavg.precip,color='c',linewidth=2.0, label='皇县年降水量',marker='o')ax2.set_ylim(25,170)ax2.set_xticks(rainavg.year)ax2.set_ylabel('年平均降水量(mm)')行_1,标签_1 = ax.get_legend_handles_labels()行_2,标签_2 = ax2.get_legend_handles_labels()线 = 线_1 + 线_2标签 = 标签_1 + 标签_2ax.legend(线,标签,loc='上中心')
然而,它只是绘制降雨数据.我认为这不是正确的语法,但我似乎找不到任何东西来回答我的问题.我只找到解释如何从一个数据框中绘制多条线或如何绘制两个 y 轴的论坛.
更新多个标记样式且没有线条:
fig, ax = plt.subplots(figsize=(10,8))标记样式 = ['v','o','+','*','.']对于 i, col in zip(markerstyles, IVdv):IVdv[col].plot(ax = ax,marker=i, linestyle='none')ax.title.set_text('PM2.5 设计值')ax.set_ylim(0,100)ax.set_ylabel('PM2.5 设计值 (ug/m3)')ax.set_xlabel('年')ax.set_xticks(IVdv.index)ax2=ax.twinx()ax2.plot(rainavg.year,rainavg.precip,color='c',linewidth=2.0, label='皇县年降水量',marker='o')ax2.set_ylim(25,170)# ax2.set_xticks(rainavg.year)ax2.set_ylabel('年平均降水量(mm)')行_1,标签_1 = ax.get_legend_handles_labels()行_2,标签_2 = ax2.get_legend_handles_labels()线 = 线_1 + 线_2标签 = 标签_1 + 标签_2ax.legend(线,标签,loc='上中心')
输出:
I'm having issues with matplotlib. I want to make a scatterplot with two y-axes and the two axes should correspond to two different dataframes. But the problem I'm encountering is plotting multiple lines from one dataframe.
the first dataframe (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
the second dataframe (rainavg):
year precip
2010 161.798
2011 64.262
2012 62.991
2013 91.440
I want to make a scatterplot with the left y-axis being PM2.5 concentration (ug/m3), which is what columns EC_pmdv, C_pmdv, and B_pmdv are describing. I want the right y-axis to be precipitation (mm). And I want the x-axis to be year. I am having trouble plotting all three lines from IVdv (I want to plot x1=IVdv.year, y1=IVdv.EC_pmdv, x2=IVdv.year, y2=IVdv.C_pmdv, x3=IVdv.year, y3=IVdv.B_pmdv). I know how to make two y axes. I've attached the code I've written so far:
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')
However, it is only plotting rain data. I don't think this is the correct syntax for this, but I can't seem to find anything to answer my question. I am only finding forums that explain either how to plot multiple lines from one data frame or how to plot two y-axes.
plot multiple pandas dataframes in one graph This link says I need to use ax=ax but I'm not sure how to format it while also retaining my secondary y-axis.
Let's use pandas plot it is easier:
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')
Output:
Update for multiple markerstyles and no lines:
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')
Output:
这篇关于从一个数据框中绘制多条线并添加辅助轴以绘制不同的数据框 - Pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!