Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upaxes collage #16603
axes collage #16603
Conversation
| For example :: | ||
| x = [['A', 'A', 'B'], | ||
| ['C', 'D', 'B']] |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
tacaswell
Feb 28, 2020
Author
Member
I suggested that and the argument @story645 had against it is that we should be encouraging long names like
x = [['map', 'histogram'],
['time_series', 'time_series']]
This comment has been minimized.
This comment has been minimized.
tacaswell
Feb 28, 2020
Author
Member
but, that said, I suspect that could be implemented in the same pre-processing of the input that we would need to do for the auto-expanding logic.
This comment has been minimized.
This comment has been minimized.
anntzer
Feb 28, 2020
Contributor
but that still works, the point is to support List[str] vs List[List[str]]
I don't have a really strong opinion there though
This comment has been minimized.
This comment has been minimized.
story645
Mar 2, 2020
Member
if we support strings of arbitrary length, then how do we know that ["ABCD"] is ["A", "B", "C", "D"] vs ["AB", "CD"]?
This comment has been minimized.
This comment has been minimized.
story645
Apr 27, 2020
Member
What's the deterministic parse rule on that? Single letter equals 1 subplot?
This comment has been minimized.
This comment has been minimized.
tacaswell
Apr 27, 2020
Author
Member
Yeah, you either pass in a single string or a list.
@staticmethod
def _normalize_grid_string(layout):
return [list(ln) for ln in layout.strip().split('\n')]
This comment has been minimized.
This comment has been minimized.
story645
Apr 27, 2020
Member
So the point I could be clearer in making is the docs have to be 100 percent explicit that for string input, each character (excluding new lines and , ) is parsed as a subplot.
This comment has been minimized.
This comment has been minimized.
thesamovar
Apr 27, 2020
First of all - thanks for looking at that gist - would love to see this in matplotlib. I'd definitely encourage you to include the multi-line string approach because even though it's not super-generalisable it covers a very common use case for scientists preparing figures.
Two other random notes:
- I updated my gist so that you can now use a '.' to indicate an empty axis because then I don't have to worry about spaces.
- If you can find a good way to add the panel labels so that they align correctly with
tight_layouteven with different axis sizes without having to do any fiddling, that would be amazing. There might already be a good way of doing this but I've never figured it out. My current solution in the gist sort of works but is a bit hacky and the alignment isn't quite right (the letters should line up on the left not the right).
Thanks for all your work on matplotlib - it's very much appreciated.
This comment has been minimized.
This comment has been minimized.
jklymak
Apr 27, 2020
Contributor
constrained_layout handles empty gridspec slots by placing invisible axes in the empty slots.
|
I wouldn't want autoexpansion of [['a'], ['b', 'c']] to [['a', 'a'], ['b', 'c']]. Perhaps could go in as provisional/unstable API? (I think we should try provisional APIs more :-)) |
|
Great! Quick thoughts:
|
Would the number just be labels or would the also be used to size the axes?
That is very clever and a good idea
I don't see any way to spell that API that does not turn into an unusable knot very quickly. Maybe we should push on (or just document if it is already possible and I don't know how to do it) to set up the shared axes after the fact. |
|
I wrote this up on the plane because it was fun (I don't write much actual code anymore). There was some question about how hard this would be (in my head) and some question if people think it is a good idea. With @story645 @anntzer and @ImportanceOfBeingErnest on board I think we have the answer to the second question and this code is too bad so I think we have the answer on the first. If someone is super excited about this and wants to take over leading / championing its implementation feel free to do so! |
|
see #15287 for post-creation sharing. |
|
This seems workable as long as the scope doesn't get out of control. As soon as you do this, people are going to want to add colorbar positioning, etc etc. Not sure about the return dictionary versus just a list. I guess you have to do it this way so the order is unambiguous? It is a bit unexpected to have to deal with a dictionary, but maybe it has advantages too. |
I don't see how to use numbers for sizes in a reasonable way, so this is really just a question of whether the user is expected to do
Two options come to mind here:
|
Once things are layout out in a complex ways it is not clear to me that there is an un-ambiguous order through them. Returning a dictionary is also stable under adding nesting layout and adding / removing other axes from the layout.
Option 2 seems better (you can easily opt-in to sharing a sub-set of the axes) |
|
A dict is quite nice when working with complex layouts (at least personally I often stuff my axes in a dict anyways...).
and I guess we could even support sharing multiple axes at once ( |
|
Implemented @ImportanceOfBeingErnest 's two suggestions and @jklymak 's internal API usage suggestions. |
znstrider
commented
Mar 1, 2020
|
Hey, great job! I wrote very similar code a couple days ago, in style of plt.subplots which allows you to simply do: I also accounted for None inputs. Have a look here and feel free to adopt if it suits you: |
|
Thanks @znstrider . |
| def _normalize_grid_string(layout): | ||
| return [list(ln) for ln in layout.strip().split('\n')] | ||
|
|
||
| def build_grid(self, layout, *, subplot_kw=None, gridspec_kw=None, |
This comment has been minimized.
This comment has been minimized.
|
|
||
| x = [["A", "B"], ["C", "D"]] | ||
|
|
||
| y = [["F"], [x]] |
This comment has been minimized.
This comment has been minimized.
jklymak
May 25, 2020
•
Contributor
OK, so what happens if I do y = [["F" [x]]["G", [x]]]? where the x subgrid spans the two rows? Not saying you have to support that, but it is valid to do gs_n = GridSpecFromSubplotSpec(gs[:, 1], nrows=2, ncols=2).
| return [list(ln) for ln in layout.strip('\n').split('\n')] | ||
|
|
||
| def quilt_layout(self, layout, *, subplot_kw=None, gridspec_kw=None, | ||
| empty_sentinel='-'): |
This comment has been minimized.
This comment has been minimized.
timhoffm
May 25, 2020
•
Member
Patchwork uses '#' as empty sentinel. Is it intentional to use a different default?
Without having read the docs first, I have misinterpreted the example
-a-
bAc
as a spanning all columns (which may or may not be a reasonable extension). Point is, I'd recommend a less "directional" / "semantically connecting" character as the empty sentinel. Possible choices for the string case include:
#to mimic patchwork.: space is an obvious choice but makes parsing a little harder (but AFAICS still managable).: If we don't want to add the extra effort for space-parsing, a period looks almost empty.
The natural sentinel for nested lists is None.
Additional remark: I wouldn't make the empty_sentinel configurable now. Keep it simple. I expect that most people can live with whatever default we choose and only few would spend the effort and clutter to define their own. Additionally, one can still work around if one cares a lot about the sentinel.
quilt_layout(
"""
XaX
bXc
""".replace('X', '.'))
Finally, we can always add an empty_sentinel later if necessary.
This comment has been minimized.
This comment has been minimized.
tacaswell
May 25, 2020
Author
Member
Good point about - looking like "span" instead of "blank". I spent a while trying with whitespace as the blank, but it gets gnarly with both the de-denting we want to be able to do so you don't have to push these things against the left edge of the file and to work around editors having a "strip trailing whitespace" mode.
I think it is simpler to leave empty_sentinel configurable than to document that the sentinel is dependent on the type of the input (I had written that out, but decided I didn't like it before I pushed it). The reason the sentinel came in at all was @ImportanceOfBeingErnest request to be able to support integers (in numpy arrays).
|
My favorite names:
Naming this |
moorepants
commented
May 25, 2020
•
I also like "named_subplots". Edit: My second choice would be "compose_axes" |
|
Somewhat weak point for preferring something with "mosaic" over something with "quilt" (just comparing these two, not comparing with the other options): I think at least among French speakers, the number of people who understand the word "quilt" (Fr: "courtepointe") is waaaaay less than the number of people who understand the word "mosaic" (Fr: "mosaique"). |
"""
.a.
b.c
"""vs """
#a#
b#c
"""I think I like the I am sold on both 'mosaic' over 'quilt' and on including 'subplots' in some form. I think the pool is down to
I think starting with My current favorite is (agreeing with @timhoffm ) Should probably export this to pyplot as well... |
moorepants
commented
May 25, 2020
|
I'm not a MPL developer, but for what it is worth, "mosaic" is a bit abstract wrt to MPL objects. When I hear that word I think of mosaics that artists create, not a structured layout or composure of axes to plot on. The language in MPL already has the words "subplot", "axes", "figure", etc. My vote would be to use the words MPL users are familiar with. All of these use terminogly that a MPL user would understand:
|
|
On the call we came to the consensus that it should start with
mosaic : suggests too much like art, bit whimsical |
|
I second @jklymak's favoring of "subplot_mosaic". "subplot_grid" is another candidate now. |
|
I would say that while That said, if we think that having the ability to return the GridSpec could be useful to users, then So I'm +1 on either of those. |
I don't think you would specify an actual |
moorepants
commented
May 25, 2020
•
|
The bootstrap library refers to their layout specification as a "grid": https://getbootstrap.com/docs/4.5/layout/grid/, which has a similar functionality to this command. |
|
WRT documentation, I wonder if it would be better if we had a new tutorial that was "laying out subplots" and discussed the various ways we do this in increasing complexity. i.e.
|
|
I don't disagree about a more broader tutorial, I don't think a holistic description of all of the ways to do it should block this PR. |
|
Perhaps not, but you've added another tutorial, at a different level than the already existing tutorials that deal with the same subject. I'd suggest at least moving this tutorial to |
|
@jklymak I think .subplots_* isn't a more advanced way of doing subplots, it's a semantically driven layout rather than index based. Even if you've got a 2*1 grid, there are advantages to using the mosaic version for keeping track of ordering. |
|
Well thats a serious question. Are we going to start filling the docs with Though I find it a bit hard to imagine someone wanting this for straight grids of subplots: |
more that like my next tutorial would totally be something like: fig, axes = subplots_mosaic([['map', 'hist']['time', 'time']])
axes['time'].plot()ok let's flip that: fig, axes = subplots_mosaic([['time', 'time'],['map', 'hist']])
axes['time'].plot()whereas the current way to do it is: fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(2, 2)
ax1 = fig.add_subplot(gs[0,0])
ax2 = fig.add_subplot(gs[0,1])
ax3 = fig.add_subplot(gs[1,:])
ax1.pcolormesh(d)
ax2.hist(d.flatten())
ax3.plot(d.flatten()) |
|
Again, spanning columns is "intermediate" usage. |
|
@jklymak but that's the point, we don't really want spanning columns to be intermediate usage 'cause this kind of composite plot is beyond common. |
|
but even if I forget spanning for a moment, I would do a tutorial with like: fig, axes = plt.subplots_mosaic([['data', 'predicted',] ['lineregress', 'roc']])'cause |
|
Spanning rows and columns is more advanced than not spanning rows and columns. Yes, the point of this PR is to make that even simpler than it was, but it still is by definition more complicated. If there are going to be a bunch of tutorials that have "semantically driven layout" for simple grids that have no axes spanning rows or columns, then I think we need to discuss how that will look in the docs, because I think it will be confusing to have yet another idiom sprinkled about. For the more complicated column-spanning layouts, there are at least two other idioms that do not resort to gridspecs: Which comes back around to my original point that getting all the idioms on the same tutorial would really help because then we could have some commentary about their plusses and minuses, and the user has a point of comparison versus something they found on the web from before the new way was available. I'm actually concerned about having a big long tutorial about this particular idiom, without it specifically referencing the other ones, particularly as this is so new. |
I am 1000% on board with this, I just agree with Tom that it shouldn't hold up this PR and this feature by itself is intended to be introductory.
I kind of agree with you here on the mixed idioms not being the greatest...but that we also want to preserve both styles. |
#17376 is intended to go into that direction (but still needs a lot of additions to become a comprehensive tutorial). Anybody willing to work on this feel free to include the flowchart, modify it or come up with better illustrations. It may as well be worth creating such a diagram "by hand" because the amount of control in graphviz is limited. |
|
@ImportanceOfBeingErnest & @rougier are working on cheatsheets at matplotlib/cheatsheets & I wonder if #17376 might be a better fit w/ that - also maybe a separate something on axes creation |
|
In the context of this PR, a) I think this method should be clearly marked experimental, to allow us to change it. b) I don't think its an introductory tutorial, and if anything this would be better as an (extended) example under Beyond this PR, I think something comprehensive should replace the 'arranging subplots with gridspecs' tutorial and the gory details of that gridspec tutorial go into "advanced". |
|
By the way, there's also this MEP: #4384:
|
|
Yes I remember that MEP. Not sure I understand the discussion around colorbars, but I think the rest of it is what @tacaswell has implemented here. |
|
@rougier sorry, I had forgotten about that MEP :( I'll take another look at it tomorrow and see if we missed anything. |
|
Original PR is 5 years old, I can understand you've forgotten it |



tacaswell commentedFeb 28, 2020
•
edited
PR Summary
This is an attempt at implementing a nicer API for laying out complex axes schemes like patchwork for ggplot. This is following on from a conversation @anntzer and @story645 had a few weeks ago
https://hackmd.io/4zWKhuLXQ16Y_oUfi9hnKA#Gridspec-Easy-API and was done on a red-eye flight home
The big win here is things like
do what you expect.
open questions:
[['A'], ['B', 'C']]<->[['A', 'A'][ ['B', 'C']])PR Checklist