Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using savefig to increase dots per inch (DPI) in matplotlib.pyplot

This answer, suggests using plt.savefig in order to increase the DPI. I am a relative newbie, and not sure how to use .savefig.

savefig's call signature requires fname to reference the file (or object?) which needs more DPI:

enter image description here

In the following code, what should I use for fname? Should I create an object and then reference that?

#previous code generates two dataframes now converted to two lists...

Max_Vals = DFMAX1.tolist()
Min_Vals = DFMIN1.tolist()

fig = plt.figure()

plt.plot(Max_Vals, 'g-')
plt.plot(Min_Vals, 'b-')

fig.set_size_inches(30.,18.)
plt.show()

enter image description here

When I run savefig without fname:

plt.savefig(dpi=300)

I get an error:

enter image description here

Grateful for any help.

like image 563
ZakS Avatar asked Jan 03 '23 11:01

ZakS


1 Answers

The point of plt.savefig() is it allows you to export the graph to a file. If you're just using plt.show() you're only showing the image, at which point to copy it elsewhere you have to use print-screen or similar.

Try running:

#previous code generates two dataframes now converted to two lists...

Max_Vals = DFMAX1.tolist()
Min_Vals = DFMIN1.tolist()

fig = plt.figure()

plt.plot(Max_Vals, 'g-')
plt.plot(Min_Vals, 'b-')

fig.set_size_inches(30.,18.)
plt.savefig('100dpi.png', dpi=100)
plt.savefig('200dpi.png', dpi=200)

At this point, two image files will be saved in your working folder (likely the same folder as your script) - one with 100dpi, the second with 200dpi.

like image 142
asongtoruin Avatar answered Jan 13 '23 08:01

asongtoruin