I tried to draw barchart using matplotlib . The number of elements to build can vary. I cannot set figure.set_size_inches(w,h) or set_figwidth(w) with constants (e.g. 6., 8. or 8., 12, etc.), since I cannot say in advance what should be the value of w or h. I want the width of figure to increase as the number of items to be plotted increases . Can someone tell me how can I do this?
import pylab def create_barchart(map): xvaluenames = map.keys() xvaluenames.sort() yvalues = map.values() max_yvalue = get_max_yvalue(yvalues) xdata = range(len(xvaluenames)) ydata = [map[x] for x in xvaluenames] splitxdata = [x.split('-',1) for x in xvaluenames] xlabels = [x[0] for x in splitxdata] figure = pylab.figure() ax = figure.add_subplot(1,1,1) figsize = figure.get_size_inches() print 'figure size1=',figsize,'width=',figsize[0],'height=',figsize[1] barwidth = .25 ystep = max_yvalue/5 pylab.grid(True) if xdata and ydata: ax.bar(xdata, ydata, width=barwidth,align='center',color='orange') ax.set_xlabel('xvalues',color='green') ax.set_ylabel('yvalues',color='green') ax.set_xticks(xdata) ax.set_xlim([min(xdata) - 0.5, max(xdata) + 0.5]) ax.set_xticklabels(xlabels) ax.set_yticks(range(0,max_yvalue+ystep,ystep)) ax.set_ylim(0,max(ydata)+ystep) figure.autofmt_xdate(rotation=30) figure.savefig('mybarplot',format="png") print 'figure size2=',figure.get_size_inches() pylab.show() def get_max_yvalue(yvals): return max(yvals) if yvals else 0
if I try with a small set of elements, I get
if __name__=='__main__': datamap = dict(mark=39,jim=40, simon=20,dan=33) print datamap create_barchart(datamap)

but if i use a larger set
datamap = dict(mark=39,jim=40, simon=20,dan=33) additional_values= dict(jon=34,ray=23,bert=45,kevin=35,ned=31,bran=11,tywin=56,tyrion=30,jaime=36,griffin=25,viserys=25) datamap.update(additional_values) create_barchart(datamap)

It looks awful. I wonder if there is a way to increase the width of the shape according to the number of elements that need to be built, keeping the width of the bars the same