Hi, all. There have been some discussions about using axes_grid1.AxisDivider with polar projected images to make axis for a colorbar with proper height/width (E.g., my comment on an old issue #3286 and ). It appears to me that there is a bug in axes_grid1.axes_size.AxisX and AxisY that assume rectangular projection.
I changed mpl_toolkits.axes_grid1.axes_size.py a little to support polar projection (almost) properly. Please see https://gist.github.com/jimgreen/839f43897e9c1d54f6fb4b890a93ee1b for a working example.
The result after fixing is of the left, and the simple call to plt.colorbar() is on the right:
I wonder if this the right thing to do? And, perhaps we need to support other projections, too?
PS:
I also forced to use axes_class=matplotlib.axes._axes.Axes in divider.append_axes to get the proper axis for colorbar. Otherwise the returned ax would have same projection as the input ax, which is polar.
Hi jklymak, there is nothing wrong with shrink. But still the problem is how to figure out the shrink, which I think axes_grid1.axes_size can do. Honestly, it should be not too hard to do without axes_grid1.axes_size.
I think fig.colorbar internally uses GridSpec, so perhaps just a different way to do it. But I still think axes_grid1.axes_size should handle different projections properly.
Very true. I generated a lot of map plots (which are usually rectangular) with Cartopy, and the axes_grid1 doesn't work, probably due to the lack of support of "map_projection." I ended up tweaking 'shrink' argument. By the way, you can actually set projection for a bunch of plots at once using f, axarr = plt.subplots(2, 2, subplot_kw=dict(projection='polar')). For me the issue is, I want different projections in different panels, so add_subplot seems to be the only way out... (and again shrink)
I'm not a huge fan of axes_grid1, so I think it'd be nice to see any functionality like this in fig.colorbar.
The problem, as it often is, is interactivity. Yes, I think its possible to make the colorbar match the size of the parent axes using the aspect ratio of the plot as it currently is set. But if the user changes the aspect ratio of the plot (by adjusting the x or y-lims or zooming) then does the colorbar change size? That sort of interactivity isn't impossible, but starts to make the code pretty complicated. Personally, I'd rather someone composing a figure just eyeball a good-looking colorbar shrink and aspect, rather than introduce a slew of hooks where the axis for the colorbar gets adjusted on the fly. But if someone wants to work on it, I'm sure a PR would be welcomed.
@tacaswell Yes, I think so. Its been a while (;-)) so I'd have to check and see what it does already. Part of the impetus for #9855 was for the constrained layout to know how big the axes wanted to be. The question will be (once we get there) is what the default should be (shrink is proportional to original or modified Axes position) and how to implement choosing.
Activity
jklymak commented on Nov 6, 2017
Whats wrong with:
If you want to get fancy, you can figure out the shrink...
I'm actually surprised that there is no way to set the projection except via add_subplot.
liangwang0734 commented on Nov 6, 2017
Hi jklymak, there is nothing wrong with shrink. But still the problem is how to figure out the shrink, which I think axes_grid1.axes_size can do. Honestly, it should be not too hard to do without axes_grid1.axes_size.
I think fig.colorbar internally uses GridSpec, so perhaps just a different way to do it. But I still think axes_grid1.axes_size should handle different projections properly.
fischcheng commented on Nov 25, 2017
Very true. I generated a lot of map plots (which are usually rectangular) with Cartopy, and the axes_grid1 doesn't work, probably due to the lack of support of "map_projection." I ended up tweaking 'shrink' argument. By the way, you can actually set projection for a bunch of plots at once using f, axarr = plt.subplots(2, 2, subplot_kw=dict(projection='polar')). For me the issue is, I want different projections in different panels, so add_subplot seems to be the only way out... (and again shrink)
jklymak commented on Nov 25, 2017
I'm not a huge fan of axes_grid1, so I think it'd be nice to see any functionality like this in fig.colorbar.
The problem, as it often is, is interactivity. Yes, I think its possible to make the colorbar match the size of the parent axes using the aspect ratio of the plot as it currently is set. But if the user changes the aspect ratio of the plot (by adjusting the x or y-lims or zooming) then does the colorbar change size? That sort of interactivity isn't impossible, but starts to make the code pretty complicated. Personally, I'd rather someone composing a figure just eyeball a good-looking colorbar shrink and aspect, rather than introduce a slew of hooks where the axis for the colorbar gets adjusted on the fly. But if someone wants to work on it, I'm sure a PR would be welcomed.
tacaswell commented on Nov 25, 2017
@jklymak This seems like the sort of problem that the constraints based layout should be able to address.
jklymak commented on Nov 25, 2017
@tacaswell Yes, I think so. Its been a while (;-)) so I'd have to check and see what it does already. Part of the impetus for #9855 was for the constrained layout to know how big the axes wanted to be. The question will be (once we get there) is what the default should be (shrink is proportional to original or modified Axes position) and how to implement choosing.
anntzer commented on Dec 29, 2022
The gist link above is dead, so let's just move the full discussion to #3286, as I think(?) all the relevant info is over there.