Skip to content
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

Wedged polar plot's position/scaling leaves too much white space. #10264

Closed
ImportanceOfBeingErnest opened this issue Jan 18, 2018 · 10 comments
Closed
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action topic: polar

Comments

@ImportanceOfBeingErnest
Copy link
Member

ImportanceOfBeingErnest commented Jan 18, 2018

Coming from this stackoverflow question, some strange (to me) behaviour is observed when plotting partial or wedged polar plots. This new feature, which can be used by setting .set_thetamin and .set_thetamax on a polar plot, allows to plot only part of a polar plot without the need to use floating axes - which is of course a great feature.

The problem arises when either using those partial polar plots as subplots or when trying to plot them tightly in a figure. It seems like their position is always set to be in the middle of some kind of subplot with aspect ratio of 1. Especially when using a half circle polar plot, this causes a lot of unnecessary white space around the half-circle.

Consider this code:

import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(1, 2, subplot_kw={'projection': 'polar'},
                         figsize=(8, 4))

for i, theta_max in enumerate([2*np.pi, np.pi]):

    # define theta vector with varying end point and some data to plot
    theta = np.linspace(0, theta_max, 181)
    axes[i].set_thetamin(0)
    axes[i].set_thetamax(theta_max*180/np.pi)

    axes[i].set_xlabel('Magnitude', fontsize=15)
    axes[i].set_ylabel('Angles', fontsize=15)

plt.savefig("polarwedges2.svg", facecolor="#e8f4f2")

which produces the top plot in the below image. I have manually marked the region which is occupied by the polar plot, calling it "Incompressible BBox", because it occupies much more space than desired, also making the xlabel appearing totally off. The picture below is what I would expect this subplot to behave like.

image

The "imcompressibility" of that box also prevents to have such a half-circle polar plot tight in a figure.
In the following code a simple figure with such a plot is attempted.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1, figsize=(6,3.4), subplot_kw={'projection': 'polar'})

# set 'thetamin' and 'thetamax' according to data
ax.set_thetamin(0)
ax.set_thetamax(180)

ax.set_xlabel('Magnitude', fontsize=15)
ax.set_ylabel('Angles', fontsize=15)

plt.savefig("polarwedge3.svg", facecolor="#e8f4f2")

The plot appears much smaller than the figure would allow for and leaves a lot of white space around.

image

The only workaround I can currently think of is to make the original axes much larger than the figure (ax.set_position( [0.1, -0.45, 0.8, 2])) and shift the xlabel manually by some amount towards the top, as shown in the lower picture. Workaround code.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1, figsize=(6,3.4), subplot_kw={'projection': 'polar'})

# set 'thetamin' and 'thetamax' according to data
ax.set_thetamin(0)
ax.set_thetamax(180)

ax.set_xlabel('Magnitude', fontsize=15, labelpad=-60)
ax.set_ylabel('Angles', fontsize=15)

ax.set_position( [0.1, -0.45, 0.8, 2])
plt.savefig("polarwedge4.svg", facecolor="#e8f4f2")

I guess this behaviour is in general undesired. One would rather have either the same behaviour as for fixed aspect plots (like imshow, i.e. what is called "compressible" above) or even better, some API to set the position to useful values according to the usual subplot mechanism.

@QuLogic
Copy link
Member

QuLogic commented Jan 18, 2018

This is expected (by me because I wrote it) behaviour, though not really desired. Polar plots set themselves to have equal aspect by default. This is only done in __init__ though, so wedges are simply zoomed as much as possible to fit in that square box.

Since the space for the Axes is provided by the Figure, I never really knew how to get it to resize itself, and still don't really know. If anyone knows, I'd like to find out, because it's something that seems desirable in other places, like Cartopy for instance.

@tacaswell tacaswell added this to the v3.0 milestone Jan 18, 2018
@WeatherGod
Copy link
Member

WeatherGod commented Jan 18, 2018 via email

@QuLogic
Copy link
Member

QuLogic commented Jan 18, 2018

Well, it's not resizing the Figure that's needed, "just" resizing the space needed for the Axes. But I think that's still managed by the Figure itself, no?

@ImportanceOfBeingErnest
Copy link
Member Author

I have to admit that my understanding of the internals is rather limited. But since normal image subplots are handled exactly as desired (i.e. their axes size is as large as the image itself by default) I would have thought that there is a mechanism that provides this behaviour and which can be used here as well.

@jklymak
Copy link
Member

jklymak commented Jan 18, 2018

First there is a constrained layout manager PR #9082.

Since the space for the Axes is provided by the Figure, I never really knew how to get it to resize itself, and still don't really know. If anyone knows, I'd like to find out, because it's something that seems desirable in other places, like Cartopy for instance.

The space that the axes is being drawn is set by subplots, where it returns a position for the axes, called the "original" position. If you set the x/ylim of this axis, and then set the aspect then the there is an "active" position that tells us where the new axis is.

Bizarrely, that new position doesn't calculated until draw time, which is not of too much use if you call get_position(original=False). (please see #9855 which fixes this, but hasn't been reviewed yet).

I don't know the internals of the polar projection. if I was just drawing half a circle on a Cartesian projection, I'd set the ylim = [0.5 1.], xlim=[0., 1.] and then the "active" axis would be the right aspect ratio.

I'd need to play w/ the above, but it seems maybe polar projection should have some awareness of the extent it wants to draw over and modify its transform accordingly. It may need to modify the "active" axis position as well, but I don't know how polar does that at the moment.

@QuLogic
Copy link
Member

QuLogic commented Jan 18, 2018

xlim and ylim are in data coordinates, so that wouldn't work (your xlim is 0 to 57 degrees). After looking around, I do think some fiddling with the "active" position could probably work, but it would require some tweaking of internals that assume square equal aspect ratio.

@jklymak
Copy link
Member

jklymak commented Jan 18, 2018

Yeah, and I don't understand the Transforms. Whats happening now is that set_aspect('equal') is defining a cartesian box in the middle of the axis with relative limits 0-1, 0-1 that I presume the transform now maps into.

I think that aspect should be changed from 1 to 0.5 (in the present case, or whatever fraction of the circle is being made) and the y part of the transform multiplied by 2.0.

If I set_aspect in PolarAxes, I get half way there with the x-axis stretched. But I don't understand the Transforms well enough to then get the y-direction to stretch and be offset down.

@QuLogic
Copy link
Member

QuLogic commented Jan 18, 2018

Yes, I've found something similar. Partially, the problem is in the wedge bbox here because it tries extra hard to get equal aspect ratio. I can set aspect ratio in draw where the wedge is updated, but even if that adjustment is ignored, there's still some other bit wrong as clipping is not quite right.

@karlos124c
Copy link

excellent

@github-actions
Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Apr 30, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2023
@rcomer rcomer added the status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. label May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action topic: polar
Projects
None yet
Development

No branches or pull requests

8 participants