0
votes

I am attempting to display two subplots in the same Figure object, a pie chart and a table:

close enough aspect ratio, achieved by figsize and wspace trial/error:

enter image description here

The desired pie:table width ratio is 1:2. The issue is, I can't seem to ensure that the pie subplot remains square (and thus the pie remains circular), nor can I keep the second table subplot from overlapping with the first, without just trying figsize and wspace combinations until it works.

To try to make sure the first subplot would be square, I set up an equation, 1x + 2x +(3x/2)wspace = 12. 1x is the width of the first subplot, 2x is the width of the second subplot, and 3x/2wspace should be the wspace between them (3x/2 being the average width of the two subplots). This should sum up to 12 if I specify a figsize width of 12. From here I can figure out x, which should be equal to the width of the first subplot, and then set it as my figsize height. That gives me a height of roughly 3.478 when using a wspace of 0.3.

Here is my code currently:

rows = ['Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5','Category 6', 'Category 7']
cols = ['+100', '+200', '+300', '+400', '+500'] 
vals = [['$18,990', '$18,990', '$18,990', '$18,990', '$18,990'],
       ['$24,211', '$24,211', '$24,211', '$24,211', '$24,211'],
       ['$19,999', '$19,996', '$19,989', '$19,979', '$19,966'],
       ['$26,214', '$26,214', '$26,214', '$26,214', '$26,214'],
       ['$16,674', '$16,674', '$16,674', '$16,674', '$16,674'],
       ['$21,440', '$21,440', '$21,440', '$21,440', '$21,440'],
       ['$19,342', '$19,341', '$19,340', '$19,338', '$19,336']]
bbox = [0, 0, 1, 1]
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(12,3.478), gridspec_kw={'width_ratios':[1,2]})
fig.tight_layout()
ax1.pie([1,0])
ax2.axis('off')
table = ax2.table(cellText=vals, rowLabels=rows, colLabels=cols,bbox=bbox)
table.auto_set_font_size(False)
table.set_fontsize(12)
plt.subplots_adjust(wspace=.3)

This produces the following:

squished:

enter image description here

I'm guessing the margins figure into the figsize, so my equation above does not actually work. I've tried specifying no wspace as well, but that produces the following overlapped and squished image:

no wspace specified, overlapping and squished:

enter image description here

I've tried ax2.set_outscale_on(False) as well but that didn't seem to change anything.

The first picture at the top of my post came through much trial and error, landing on a fig height of 3.478 and a wspace of .5. I'm sure it's not perfectly square, but it looks good enough to me. Is there an easier way to force the subplot to a square aspect ratio? A secondary concern is ensuring neither overlap by default, without having to mess with wspace, but I can deal with manually adjusting wspace if I need to. Thanks for any advice!

1

1 Answers

0
votes

To have both subplots keep a square aspect you can do

fig, (ax1, ax2) = plt.subplots(ncols=2)
ax1.set_aspect('equal', adjustable='box')
ax2.set_aspect('equal', adjustable='box')

If you only want one to be square then you can just set the aspect for ax1 to be 'equal' and ax2 to 'auto'. You can also increase wspace a little more to ensure they don't overlap.