Matplotlib imshow subplots sharey breaks x limits
I am drawing a series of heatmaps using matplotlib. Without a common y-axis, it works fine. However, I ran into a problem when I try to use the y axis. It looks like the x-axis limits are getting crippled.
Consider the following MWE:
import matplotlib
print matplotlib.__version__ # prints "1.4.2"
import matplotlib.pyplot as plt
data = [[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]]
nrows, ncols = 1, 4
fig, axes = plt.subplots(nrows, ncols, sharey=True)
for j in range(ncols):
xs = axes[j]
# seems to have no impact when sharey=True
#xs.set_xlim(-0.5, 2.5)
xs.imshow(data, interpolation='none')
plt.show()
The wrong output from this looks like this:
Whereas a simple change sharey=True
to sharey=False
leads to the correct output (except that I want the y-axis to be split, of course, which it isn't now):
Is there a way to fix this?
source to share
Answer from here :
ax.set_adjustable('box-forced')
So:
for j in range(ncols):
xs = axes[j]
xs.set_adjustable('box-forced')
xs.imshow(data, interpolation='none')
It looks like this is intentional behavior and you need to specify this to reconcile the differences between how imshow () behaves in one graph and how it should be in a sub-block.
source to share