| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
Marking WIP as I'd like to add a couple of examples.... Done |
Sorry, something went wrong.
|
Re milestoning as a bug fix. The behaviour here is much better than in 3.0.0 |
Sorry, something went wrong.
| cm = ['RdBu_r', 'viridis'] | ||
| for nn in range(2): | ||
| for mm in range(2): | ||
| ax = axs[mm, nn] |
There was a problem hiding this comment.
Maybe use row, col instead of mm, nn.
Sorry, something went wrong.
| # ``constrained_layout=True`` | ||
|
|
||
| fig, axs = plt.subplots(3, 3, constrained_layout=True) | ||
| for ax in axs.flatten(): |
There was a problem hiding this comment.
I would use ax.flat. While flatten() reshapes the array, flat directly returns an iterator over the array, which is all we are needing here.
Sorry, something went wrong.
| # For the `~.axes.Axes.pcolormesh` kwargs (``pc_kwargs``) we use a | ||
| # dictionary. Below we will assign one colorbar to a number of axes each | ||
| # containing a `~.cm.ScalarMappable`; specifying the norm and colormap | ||
| # ensuresthe colorbar is accurate for all the axes. |
There was a problem hiding this comment.
missing space
Sorry, something went wrong.
| # | ||
| # ``constrained_layout`` usually adjusts the axes positions on each draw | ||
| # of the figure. If you want to get the spacing provided by | ||
| # ``constrained_layout`` but then not have it update, then do the initial |
There was a problem hiding this comment.
leave out the "then" before "do". This sentence has too much "then" 😄
Sorry, something went wrong.
There was a problem hiding this comment.
Thanks - I took out the first "then" so it now reads:
# If you want to get the spacing provided by # ``constrained_layout`` but not have it update, then do the initial # draw and then call ``fig.set_constrained_layout(False)``.
Sorry, something went wrong.
|
Thanks for the careful review @timhoffm |
Sorry, something went wrong.
|
I only had a quick look through the code, too quick to give a green checkmark here already, but in principle this looks good and it's definitely correct to not let the two cases of contrained layout=True or False differ substantially. Now this PR introduces a new "Placing colorbars" example, which is a great idea. If that is the place to look for how to place colorbars in matplotlib, it's not yet complete, since there are lots of other ways, mostly via the cax argument.
Seeing how long that list has become, it might as well be suitable as tutorial? |
Sorry, something went wrong.
|
My proposal would be for this PR to go in, and then if someone wants to add a couple more examples to the new example file, that would be great. I'd love to see how anchor and panchor work - I've never played with those or seen them in the wild. They could sure be better documented than they are now. |
Sorry, something went wrong.
There was a problem hiding this comment.
After going through this in more detail, I tried to construct cases for which this would fail. Turns out I didn't find any. So I agree that this should go in to be able to move on.
Sorry, something went wrong.
|
Great - any of those cases you think would be useful as tests we can add. Certainly I could break it in the future! |
Sorry, something went wrong.
…648-on-v3.0.x Backport PR #11648 on branch v3.0.x (FIX: colorbar placement in constrained layout)
|
The test added here seems to have broken on Travis master somehow. |
Sorry, something went wrong.
|
Actually, it was already broken when it was merged, but it's been 9 days since the PR build, so I'm not certain what might have affected it. |
Sorry, something went wrong.
|
The test passes locally on master... |
Sorry, something went wrong.
|
@QuLogic 3.1 tests are passing on Master. Was it 3.0.x tests that have trouble? |
Sorry, something went wrong.
|
Well, I'm not sure what happened; it seems to have magically fixed itself. It was definitely failing right after merging this PR and also after merging the fix from #12366 that otherwise broke things. |
Sorry, something went wrong.
|
@jklymak Since you asked for special cases for potential tests: The following produces a rather strange figure import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
for CL in (True, False):
mappable = []
fig = plt.figure(constrained_layout=CL, figsize=(4.5, 4))
gs = GridSpec(2,2, figure=fig)
ax1 = fig.add_subplot(gs[0,0])
ax2 = fig.add_subplot(gs[1,0])
ax3 = fig.add_subplot(gs[1,1])
for nn, ax in enumerate([ax1,ax2,ax3]):
data = np.random.randn(10, 10)
mappable += [ax.pcolormesh(data)]
fig.colorbar(mappable[0], ax=[ax1,ax3])
fig.suptitle('constrainedlayout %s'%CL)
plt.show()
But constrainedlayout is perfectly capable of handling it: In contrast, when you add another axes later on, it'll overlap, both with and without constrained layout. import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
for CL in (True, False):
mappable = []
fig = plt.figure(constrained_layout=CL, figsize=(4.5, 4))
gs = GridSpec(2,2, figure=fig)
ax1 = fig.add_subplot(gs[0,0])
ax2 = fig.add_subplot(gs[1,0])
ax3 = fig.add_subplot(gs[1,1])
for nn, ax in enumerate([ax1,ax2,ax3]):
data = np.random.randn(10, 10)
mappable += [ax.pcolormesh(data)]
fig.colorbar(mappable[0], ax=[ax1,ax3])
ax3.remove()
ax4 = fig.add_subplot(gs[0,1])
fig.suptitle('constrainedlayout %s'%CL)
plt.show()
Not sure if any of those should be considered valid or useful though. |
Sorry, something went wrong.
|
Those were brain teasers. I'm OK w/ the first case inconsistency. Colorbar steals space from the axes you list, and there is no way for the third axes to know space was stolen from the other two without a draw-time hook like ConstrainedLayout. The second case is a bit more concerning; I'd have thought ideally CL would forget about the lost axes, and move the colorbar over to the left. But... that's a pretty hard pathological case, so I'm not going to worry about it, unless there is a good use case for it, in which case it may be worth the effort. Thanks a lot for sending these! Great examples... |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
PR Summary
Closes #11641
Previously, while using constrained_layout putting a colorbar on a figure put the colorbar to the right (or other location) of the parent gridspec of the axes specified.
However, the non-constrained_layout way of doing this would just put to the right of the listed axes.
This PR makes the constrained_layout behaviour the same as non-constrained_layout
Before:
Behaviour of where first colorbar went changed significantly under constrained_layout
After
Behaviour more consistent...
PR Checklist