-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
colorbar might shrink plots if used with twinx #8823
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
I would call it a shortcoming of the twinx implementation: it makes a second Axes at the same position as the first, but it has no mechanism for ensuring that if the position of either is subsequently changed, the position of the other is also changed to match it. Ideally, the position of both Axes would be the same object so that it would not be necessary to use callbacks to keep them synchronized. There are several attributes related to position, so I'm not sure whether this would be a simple change, a difficult one, or a complete can of worms. |
#9082 addresses this (though you can't use the pyplot interface) data = np.random.random((64, 128)) * 2.0
x = np.arange(128)
y = np.arange(64)
fig, ax = plt.subplots(constrained_layout=True)
pcm = ax.pcolormesh(x, y, data)
#plt.colorbar() --> here it works fine
ax2 = ax.twinx()
fig.colorbar(pcm)
# --> here it gives wrong x values for the orange plot
ax2.plot(x, np.sin(x/10), color="orange", lw=4)
plt.show() |
@jklymak Thanks for posting this. I will cherry pick your pull request. |
This is also addressed by #10033. |
Closed by #10033. |
Please open a new bug report including all relevant version info. However I do not think removing a Colorbar will magically reallocate its space using tight_layout. It may work with constrained_layout |
Bug report
Changing plot alignment for different placing of colorbar() in twinx plot
If one combines a
pcolormesh
plot/hist2d
plot with a standardplot
by usingtwinx
, the graph from theplot
moves to wrong x values if the colorbar is called after theplot
call. If it is called before, the alignment is fine.I am not sure if this is a bug or just wrong usage.
However, one of the resulting graphs has a wrong extent that might lead to misinterpretations.
Code for reproduction
Actual outcome
Next to the confusing layout the plot shows actually a wrong graph:
the orange plot just goes to around x=100 but should go to x=128.
(The axis of the colorbar is correct and shows the range of
data
.)Expected outcome
If
colorbar
is called beforetwinx
, the orange graph has the correct extent.I am not sure if this is a bug or a wrong usage of matplotlib. In any case the wrong extent of the (in this case) orange plot is a dangerous behavior that might lead to misinterpretations of the data.
Matplotlib version
%matplotlib inline
)I installed matplotlib via pip.
The text was updated successfully, but these errors were encountered: