After a few years of finding solutions to all my coding-problems on this site, this is my first post with (as far as I can tell) a new question!
I want to create several bar-charts from one data-set and save them as individual images. I want the image-size to scale automatically so that any given object (e.g. a 1x1 square) appears the same size on every image.
The following code produces two such images in which each 1x1 element is about 60x60 pixel, so far so good:
import matplotlib.pyplot as plt
def barchart(bars,size,title):
hspace,vspace = (max(size)+1,len(size))
fig = plt.figure(figsize=(hspace,vspace+1))
fig.add_axes([0.2,0.2,0.6,0.6])
plt.title(title)
plt.axis('scaled')
x_pos = xrange(vspace)
plt.xlim(0,hspace)
plt.ylim(-1,vspace)
plt.barh(x_pos, size, height=1, align='center', alpha=0.5)
plt.yticks(x_pos, bars)
plt.savefig(title+'.png',bbox_inches='tight')
plt.clf()
barchart(["1x1","A","B","C"],[1,3,5,2],"many short bars")
barchart(["1x1","A"],[1,17],"few long bars")
But I would like to do this with a different aspect-ratio, so that e.g. each 1x1 element appears as 60x30 pixel on the image. Is there a replacement for .axis('scaled')
which does this? I have tried to scale the width in figsize
, xlim
and both, as well as in .add_axes()
and several key-words in .axis()
. They all seem to affect the final scale and aspect ratio of the images in different ways.
The exact pixel-size does not matter, whether it is 60x30 or 66x33 or otherwise, as long as it is consistent throughout all images.