I am trying to add a horizontal bar at the bottom of each pie chart in a figure. I am using subplots to achieve this, but I don't know how to customise the subplots with the horizontal bars.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(28, 11)
countery=0
for y in range(1,15):
counterx=0
for x in range(1,12):
axes[countery,counterx].pie([70,20])
axes[countery 1,counterx].barh('a',40,height=1)
axes[countery 1,counterx].barh('a',60,left=40,height=1)
axes[countery 1,counterx].axis('off')
counterx=counterx 1
countery=countery 2
plt.show()
I would like to change the size of the horizontal bar so it doesn't take all the horizontal space. and make it look smaller overall. I would like it to look something like this for each:
I've tried changing wspace
and hspace
in
plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=5)
but no luck. Any help with this?
CodePudding user response:
Tidied the loop a bit (removed the counters - you've used a for
loop like a while
loop - we can use the iterated variables directly). Other than that, the below should work. First, using figsize
make the output taller than wide, as we expect each subplot to be taller than it is wide as per your goal image. Then set the ratios of the heights of each subplot by whether it is even (a pie) or odd (a bar). Fiddle with the 4
and 1
ratios in hr
to get what looks right for you, and let me know if there's anything you don't understand.
import matplotlib.pyplot as plt
h = 14
w = 11
hr = [4 if i % 2 == 0 else 1 for i in range(h*2)]
fig, axes = plt.subplots(28, 11, figsize=(6, 12), gridspec_kw={'height_ratios':hr})
for y in range(0,h*2,2):
for x in range(w):
axes[y,x].pie([70,20])
axes[y 1,x].barh('a',40,height=1)
axes[y 1,x].barh('a',60,left=40,height=1)
axes[y 1,x].axis('off')
plt.show()
Produces
CodePudding user response: