Axes.invert_axis () does not work with sharey = True for matplotlib sub-payments

I am trying to make 4 subheadings (2x2) with the inverted y axis, and also dividing the y axis between the subheadings. Here is what I get:

import matplotlib.pyplot as plt import numpy as np fig,AX = plt.subplots(2, 2, sharex=True, sharey=True) for ax in AX.flatten(): ax.invert_yaxis() ax.plot(range(10), np.random.random(10)) 

enter image description here

It seems that ax.invert_axis() ignored when sharey=True . If I set sharey=False , I get the inverted y axis in all the subheadings, but obviously the y axis is no longer split between the subheadings. Am I doing something wrong here, is this a mistake, or is there no point in doing something like this?

+5
source share
1 answer

Since you set sharey=True , all three axes now behave as if they were. For example, when you invert one of them, you affect all four. The problem is that you are inverting the axes in a for loop that runs an iterable length four, you are thus inverting ALL axes for an even number of times ... By inverting an already inverted ax, you simply restore its original orientation. Instead, try with an odd number of subheadings, and you will see that the axes are successfully inverted.

To solve your problem, you have to invert the y axis of one subtitle (and only once). The following code works for me:

 import matplotlib.pyplot as plt import numpy as np fig,AX = plt.subplots(2, 2, sharex=True, sharey=True) ## access upper left subplot and invert it AX[0,0].invert_yaxis() for ax in AX.flatten(): ax.plot(range(10), np.random.random(10)) plt.show() 
+8
source

Source: https://habr.com/ru/post/1211861/


All Articles