matplotlib align twinx tick marks

前端 未结 3 1109
小鲜肉
小鲜肉 2020-12-09 09:24

Is it possible to make a plot with two independent y-axes such that the tick marks align?

Below is an example of half of the solution. I\'ve doubled the y-axis using

3条回答
  •  天命终不由人
    2020-12-09 10:10

    Adding belatedly to the answers: for those who have both negative and positive values in their plots, the solution I have found is as follows:

    max1 = np.nanmax(ax1.get_ybound()) #in case you have nan values
    max2 = np.nanmax(ax2.get_ybound())
    ax1.set_yticks(np.linspace(-max1, max1, 5))
    ax2.set_yticks(np.linspace(-max2, max2, 5))
    

    This results in symmetrical axis distances from zero, with the zero "line" on the y-axes aligned.

    The difficulty with set_yticks is that it calculates between min and max, rather than min, 0, max.

提交回复
热议问题