r/learnpython • • 4d ago

Remove padding of polar subplots

I have a figure that is composed of many subplots. Some of them are just simple plots with a normal projection, but some are made with a polar projection. I have it set up such that there are no labels on the outside of the polar plots, but when I look at the final figure, I still get tiny plots compared to the other ones because there is a lot of padding kept on the margins of the polar subplots. I have already tried passing the pad=0 option while plotting, as well as using plt.subplots_adjust() or plt.tight_layout(), but I still get comparatively smaller radar plots. I could fix this manually using an external editor, but I would like to be able to solve it using Python on its own.

7 Upvotes

7 comments sorted by

1

u/TheRNGuy 4d ago

You need to look in browser web dev tool.

It could come from completely different thing (look in layout and in css tabs)

You can also change things in real time there to find problem faster.

1

u/_between3-20 4d ago

What? Why browser web dev? Is that a recommendation just to figure things out? Because my plots are not intended for any online or browser-based thing. They are for a printer report. Does matplotlib include css?

1

u/baubleglue 4d ago

I think, the comment assumed you are using Jupyter notebook. It should help to see a code sample and context.

1

u/kimiyah_Fremiot 4d ago

Are your subplot cells rectangular by chance? Polar plots stay strictly circular, so they shrink to match the smaller dimension of the grid cell and leave whitespace on the longer side regardless of your padding settings

1

u/_between3-20 3d ago

They are rectangular, but there is still a lot of vertical spacing (the shorter side of the rectangle) between them, which I would like to get rid of.

1

u/Asly97 4d ago

pad and tight_layout won't touch it because that space isn't padding. A polar axes is locked to a circle, so it can only be as big as the shorter side of its grid cell. If your cells are wider than they are tall, the circle fits the height and the leftover width just sits empty, while the normal plots stretch to fill the whole rectangle. Next to each other the radar plots look tiny.

In a quick test with a 2x3 grid (polar in the middle column) this made them all the same size:

fig = plt.figure(figsize=(9, 6), layout="compressed")
gs = fig.add_gridspec(2, 3)
# ...add your subplots as usual, then on each normal (non-polar) axes:
ax.set_box_aspect(1)

set_box_aspect(1) makes the normal plots square too, so they match the circles, and layout="compressed" (matplotlib 3.6 or newer) closes up the gaps around fixed-aspect axes, which tight_layout doesn't do.

If you want to keep the normal plots wide, the other route is giving the polar ones square cells, e.g. put them in their own row, or play with height_ratios / width_ratios in add_gridspec until the polar cells come out roughly square. Then each circle fills its cell.

There's also ax.set_aspect("auto") on the polar axes, which fills the cell but squashes the circle into an oval, so probably not what you want in a printed report.