Julia – Save Plot as PNG or JPEG

To save a plot to local file storage, use the method savefig("filename") or png("filename").

Using savefig(“filename”) to save the plot locally

In this example, we will save the plot to as a PNG file.

using Plots

#data to plot
globaltemperatures = [14.4, 14.5, 14.8, 15.2, 15.5, 15.8];
numindustries = [17, 400, 5000, 15000, 20000, 45000];

#use GR module
gr();

#plot
plot(numindustries, globaltemperatures, label="line")
#add points
scatter!(numindustries, globaltemperatures, label="points")

#save plot
savefig("C:\\plot.png")

The plot is saved to the local storage at the location specified in the arguments to savefig() function.

ADVERTISEMENT
Julia plot saved to local storage

And the plot is

Export or save Julia Plot to local file storage

Using png() method to save the file as .png

If you want to save the file with .png extension, then you can use png() method with the path for the file without the extension.

using Plots

#data to plot
globaltemperatures = [14.4, 14.5, 14.8, 15.2, 15.5, 15.8];
numindustries = [17, 400, 5000, 15000, 20000, 45000];

#use GR module
gr();

#plot
plot(numindustries, globaltemperatures, label="line")
#add points
scatter!(numindustries, globaltemperatures, label="points")

#save plot as PNG
png("C:\\plot2")

The file will be saved as PNG at the specified location.

Julia export plot as PNG

Conclusion

In this Julia Tutorial, we learned how to save a plot as PNG or JPEG to filesystem.