In matplotlib, how do you display an axis on both sides of the figure?

前端 未结 3 1522
眼角桃花
眼角桃花 2020-12-16 12:32

I want to draw a plot with matplotlib with axis on both sides of the plot, similar to this plot (the color is irrelevant to this question):

3条回答
  •  感动是毒
    2020-12-16 13:02

    I've done this previously using the following:

    # Create figure and initial axis    
    fig, ax0 = plt.subplots()
    # Create a duplicate of the original xaxis, giving you an additional axis object
    ax1 = ax.twinx()
    # Set the limits of the new axis from the original axis limits
    ax1.set_ylim(ax0.get_ylim())
    

    This will exactly duplicate the original y-axis.

提交回复
热议问题