【问题标题】:Plotting multiple lines from one dataframe and adding a secondary axis to plot a different dataframe - Pandas从一个数据帧绘制多条线并添加辅助轴以绘制不同的数据帧 - Pandas
【发布时间】:2021-01-08 18:36:59
【问题描述】:

我在使用 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 轴的论坛。

plot multiple pandas dataframes in one graph 此链接说我需要使用 ax=ax 但我不确定如何格式化它同时还保留我的辅助 y 轴。

【问题讨论】:

    标签: python pandas matplotlib scatter-plot multiple-axes


    【解决方案1】:

    让我们使用 pandas 绘图更容易:

    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')
    

    输出:

    【讨论】:

    • 跟进:我需要为每个设计值设置不同的标记,并且没有行,我认为现在编写代码的方式是不可能的。我想出了如何使用linestyle='None' 删除线条,但不确定如何制作单独的标记。
    猜你喜欢
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2022-01-06
    • 2016-07-11
    • 2015-11-24
    • 2015-05-27
    相关资源
    最近更新 更多