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

前端 未结 4 840
情深已故
情深已故 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:36

    The best way is to interact with the axes object directly

    import numpy as np
    import matplotlib.pyplot as plt
    x = np.arange(0, 10, 0.1)
    y1 = 0.05 * x**2
    y2 = -1 *y1
    
    fig, ax1 = plt.subplots()
    
    ax2 = ax1.twinx()
    ax1.plot(x, y1, 'g-')
    ax2.plot(x, y2, 'b-')
    
    ax1.set_xlabel('X data')
    ax1.set_ylabel('Y1 data', color='g')
    ax2.set_ylabel('Y2 data', color='b')
    
    plt.show()
    

    example graph

提交回复
热议问题