How can I print a plot in matplotlib either from the plot window or with a command?


Question

is there a way to print a plot from matplotlib,either with a command or from the plot window itself ? I know i could save it and then print,but I am looking for something more automated.Thanks.

1
3
9/14/2011 4:32:59 PM

Accepted Answer

You could save the figure as a pdf, then use subprocess to print the pdf. On *nix, lpr could be used:

import matplotlib.pyplot as plt
import numpy as np
import subprocess
import shlex

n=20
x=np.linspace(0,np.pi,n)
y=np.sin(x)
plt.plot(x,y)
fname='/tmp/test.pdf'
plt.savefig(fname)
proc=subprocess.Popen(shlex.split('lpr {f}'.format(f=fname)))
3
9/14/2011 6:00:39 PM

From the plot window itself, there is a "save" toggle button, in the shape of a disk on the right side of the row of buttons.

From the command line or script, you can use:

    pylab.savefig("directory/filename.pdf")

Look here for some of the details:

http://matplotlib.sourceforge.net/faq/howto_faq.html#plotting-howto


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon