-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
missing imshow() subplots when using tight_layout() #4976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
... what I can reproduce this on current master. |
There is a simple work around here, just call |
yes, the work around works, thanks. Calling tight_layout inside the loop is admittedly an inefficient approach but not one I expected to delete a figure... |
I agree, something very strange is going on here. Removing the call to I suggest re-writing this loop as import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(4, 4)
for i, ax in enumerate(axes.ravel()):
im = ax.imshow(np.random.normal(size=100).reshape([10,10]))
ax.set_title(i)
plt.tight_layout() |
That is f-ed up. I wonder if the axes isn't really removed, just misplaced?
|
This is my attempted fix of the problem It seems like tight_layout is not removing the subplots but when you tried to call tight_layout on the figure it resized the subplots that were already on it(which made them bigger because it had avaliable space), then when you add another subplot to the figure with the already resized plot they will disappear because subplot is plotting in their region. I'm not really sure if this is a good fix though. |
@hamgyu1230 Can you open a pull request with that change (with a better commit message!). Can you explain why clipping these values at 0 fixes the problem? |
by making these at 0, the subplot can only shrink or stay the same even when there is avaliable space, thus the existing subplots shouldn't be overwritten by any new ones when a new subplot is plotted. |
Was this fixed? I have the same problem |
Not really, but if you just call |
This is caused by |
I actually think the "remove overlapping subplots" behavior is pretty bad and should be deprecated and removed; see #11435 and #7377 (comment). |
I agree, but I'm not sure how you deprecate that without a huge rigamarole. |
I think(?) you just emit a warning if an axes that would have been removed under the old behavior will no longer be removed with the new one? (telling whoever was relying on the old behavior that they need to explicitly call ax.remove() to suppress the warning) |
I have the same problem MacOS, matplotlib 3.1.3, MacOSX backend In my case, @jklymak 's workaround does not work. Once in a while some figures disappear. I do not want to call code:
example result (once in a while) |
Here you are only calling tight layout at the end. If this is indeed reproducible then please provide a self-contained example and open a new issue. If it's not reproducible, because you are really calling tight_layout in the middle of the process sometimes, then it is the same bug. Also if you want to add subplots to an existing figure just call fig.subplots(1,2) |
@jklymak Thanks I will look at it to see how to reproduce it |
@jklymak Thanks you saved my day. |
Please why is this code not running
|
2 similar comments
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
@Godfreyoise for usage questions please go to https://discourse.matplotlib.org/. You will need to provide a complete code example so that others can reproduce the problem. |
Hello, I've found an unexpected behavior when I make grids of subplots with imshow and tight_layout the second to last plot is missing, for example:
generates:

Whereas the same loop with pcolor works:
gives:

If I omit the tight_layout() call from the first example it works:

I'm using matplotlib 1.4.3 and numpy 1.9.2 in Anaconda 2.2.0.
The text was updated successfully, but these errors were encountered: