Using Savefig to Increase Dots per Inch (Dpi) in Matplotlib. Pyplot

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:

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()

When I run savefig without fname:

plt.savefig(dpi=300)

I get an error:

Grateful for any help.

7

1 Answer

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.