matplotlib, savefig: DPI setting is ignored

memstick picture memstick · Apr 12, 2012 · Viewed 24.7k times · Source

I cannot find anyone else with this problem. In matplotlib, you can view your plots using either show() or savefig(). These generate slightly different images; in my case, the savefig() image is uglier and harder to understand. I need to make life easy for my examinator, so..

I found some topics that suggested I set the DPI size to match that of show(). I have tried:

-> Setting savefig.dpi directly with matplotlib.rcParams['savefig.dpi'] = 80.

-> Setting savefig.dpi directly in ~/.matplotlib/matplotlibrc.

-> Moving my rc file to CWD.

-> Finally, using savefig('image.pdf', dpi=80)

I can verify that the attribute is indeed getting set; but it seems that the setting is igored by savefig(). Can anyone help with this?

(Simplified) code:

plt.bar(ind, functimes, yerr=stddev, bottom=0, width=barWidth, align='center', color='b')

ax = plt.gca()
ax.legend(barRcts, barLegend)
plt.title("Function Call Overhead")
plt.xlabel("Function ID")
plt.ylabel("Execution Time [us]")

plt.xticks(ind, funcNames)
figtest.autofmt_xdate()

plt.savefig(out_file, dpi=80, format='pdf')

Answer

pelson picture pelson · Jul 15, 2012

PDF saving uses the DPI setting for image resolution inside the PDF, not for any line/polygon resolution (which is meaningless in a vector format).

If your happy with the output on screen, and don't need to have a scalable graphic, saving as png is probably fine.