On my personal computer I have created many figures with matplotlib. I've always used plt.show() to show the figures and plt.savefig() to save the images, but I've had a lot of trouble saving a figure onto the server.
At work, python is installed on the server, but saving the figures has never worked. I always get the error no display name and no $DISPLAY environment variable
Turns out I was just missing a single line of code. After importing matplotlib type mpl.use('Agg'). This will fix the problem.
Example:
import matplotlib as mpl
mpl.use('Agg') #Not sure why, but this makes saving to a server possible.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,50,.2)
y = np.sin(x)
plt.plot(x,y)
plt.title("This works!!! I'm so excited")
plt.savefig(path to save image + image_name.png)
plt.close()
Results in:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.