Saving a plot in python creates only an empty pdf document -
i using example code exists @ official matplotlib page http://matplotlib.org/examples/api/barchart_demo.html
#!/usr/bin/env python # bar plot errorbars import numpy np import matplotlib.pyplot plt n = 5 menmeans = (20, 35, 30, 35, 27) menstd = (2, 3, 4, 1, 2) ind = np.arange(n) # x locations groups width = 0.35 # width of bars fig, ax = plt.subplots() rects1 = ax.bar(ind, menmeans, width, color='r', yerr=menstd) womenmeans = (25, 32, 34, 20, 25) womenstd = (3, 5, 2, 3, 3) rects2 = ax.bar(ind+width, womenmeans, width, color='y', yerr=womenstd) # add ax.set_ylabel('scores') ax.set_title('scores group , gender') ax.set_xticks(ind+width) ax.set_xticklabels( ('g1', 'g2', 'g3', 'g4', 'g5') ) ax.legend( (rects1[0], rects2[0]), ('men', 'women') ) def autolabel(rects): # attach text labels rect in rects: height = rect.get_height() ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height), ha='center', va='bottom') autolabel(rects1) autolabel(rects2) plt.show()
this plot, shown absolutely correctly. however, if want save it, using following command @ bottom of code, not save plot.
plt.savefig("result.pdf")
is make wrong?
move plt.savefig("result.pdf")
line before plt.show()
should save plot correctly pdf file.
you might using interactive backend. after plt.show()
, window containing plot pops up. if close window, plot gone (similar plt.close()
call). therefore, plt.savefig()
has nothing save if after closing plot window.
Comments
Post a Comment