Adding a y-axis label to secondary y-axis in matplotlib

前端 未结 4 839
情深已故
情深已故 2020-11-27 10:04

I can add a y label to the left y-axis using plt.ylabel, but how can I add it to the secondary y-axis?

table = sql.read_frame(query,connection)
         


        
4条回答
  •  粉色の甜心
    2020-11-27 10:35

    There is a straightforward solution without messing with matplotlib: just pandas.

    Tweaking the original example:

    table = sql.read_frame(query,connection)
    
    ax = table[0].plot(color=colors[0],ylim=(0,100))
    ax2 = table[1].plot(secondary_y=True,color=colors[1], ax=ax)
    
    ax.set_ylabel('Left axes label')
    ax2.set_ylabel('Right axes label')
    

    Basically, when the secondary_y=True option is given (eventhough ax=ax is passed too) pandas.plot returns a different axes which we use to set the labels.

    I know this was answered long ago, but I think this approach worths it.

提交回复
热议问题