-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
constrained_layout and colorbar for a subset of axes #11641
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
Also interesting is if you pass
|
Thanks, @QuLogic I meant to report that too but forgot. The behaviour for |
Colorbar either steals space from an axes or a GridSpec. If you specify multiple axes in the same GridSpec it steals from the whole GridSpec. To get what you want, you need to make a nested GridSpec. See the docomplicated example in the tutorial. |
I agree that its inconsistent between In the meantime, the way to do what you want is indeed a bit longer. It also loses the idea that the subplots have the same size. OTOH, if you are adding a colorbar to one column, but not the second, my guess would be they contain different data types. import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(constrained_layout=True)
gs0 = gridspec.GridSpec(1, 2, figure=fig )
gsl = gridspec.GridSpecFromSubplotSpec(2, 1, gs0[0])
gsr = gridspec.GridSpecFromSubplotSpec(2, 1, gs0[1])
data = np.random.randn(10, 10)
axs = []
for i in range(2):
ax = fig.add_subplot(gsl[i])
mappable = ax.pcolormesh(data)
axs += [ax]
fig.colorbar(mappable, ax=axs)
for i in range(2):
ax = fig.add_subplot(gsr[i])
mappable = ax.pcolormesh(data)
plt.show() |
I think I can make this consistent, but milestoning 3.1 because there is a workaround and its a bit of an obscure use case. |
Yes, different data types but on the same x,y grid. Practically, the two axes on the right could have a shared colorbar or one colorbar each and that would make the axes the same size which would be fine. My example was over-simplified. The below is what I'm aiming for with the big colorbar for the 2 panels on the left.
|
@jklymak This looks great! Thanks for all your hard work. It has certainly made life a whole lot easier. |
Bug report
Bug summary
constrained_layout seems to place colorbars at the "extreme right" of the image. (not sure how to phrase this; see below)
Code for reproduction
Actual outcome
Expected outcome
I would like the constrained_layout version of this --- which results when setting
constrained_layout=False
in theplt.subplots
call aboveMatplotlib version
print(matplotlib.get_backend())
):module://ipykernel.pylab.backend_inline
The text was updated successfully, but these errors were encountered: