Sunday 15 September 2013

python - Matplotlib, Setting position of chart in image -



python - Matplotlib, Setting position of chart in image -

i'm making tool automate couple of graphs using matplotlib, each problem encountered, after research end-up finding solution, here think i'm stuck.

1 - want have, in generated png file, kind of offset before graph starts on left, can insert text objects. how can done ?

2 - want able insert text beneath graph, useful.

mainly want able define position of plot in image file, here illustration of have, , of i want.

edit :

def plot_bar(): fig = plt.figure(figsize=(600/96, 360/96), dpi = 96) # setting padding between ticks' labels , axes mpl.rcparams['xtick.major.pad']='10' ax = plt.gca() ax.spines['top'].set_visible(false) ax.spines['right'].set_visible(false) ax.spines['left'].set_visible(false) ax.spines['bottom'].set_visible(false) ax.axes.get_yaxis().set_visible(false) mpl.rcparams['axes.facecolor'] = 'white' mpl.rcparams["figure.facecolor"]= 'white' mpl.rcparams["savefig.facecolor"]= 'white' values1 = ( 10 , 25 , 30 , 40 ) values2 = ( 40 , 3 , 10 , 6 ) n = 4 ind = np.arange(n) width = 0.7 p1 = plt.bar(ind, values1, width, color='#00a79a', ec='#00a79a') p2 = plt.bar(ind, values2, width, color='#8fdcd6', bottom = values1, ec='#8fdcd6') plt.xticks(ind+width/2., ('fuel\n\n dollars \n\n dollars', 'tires', 'roadcalls', 'maintenance') ) plt.yticks(np.arange(0,81,10)) # removing tick lines axes tic in ax.xaxis.get_major_ticks(): tic.tick1on = tic.tick2on = false tic in ax.yaxis.get_major_ticks(): tic.tick1on = tic.tick2on = false plt.savefig("fichier.png")

you can utilize subplots_adjust add together spacing. tight_layout extend box fit text missing plot before ("dollars").

as rest of plot, should reasonably easy add together labels. example, for loop below adds totals on top of each bar (similar this demo).

for bar in p2: height = bar.get_height() + p1[bar.get_x()].get_height() ax.text(bar.get_x()+bar.get_width()/2., 1.05*height, '%d'%int(height), ha='center', va='bottom') plt.tight_layout() plt.subplots_adjust(left=0.3, bottom=0.3)

python matplotlib figure

No comments:

Post a Comment