我正在尝试运行一个小程序来保存我的 3D 散点图,而不是在 GUI 中打开它。问题是两者兼而有之!这是我正在谈论的一段代码:
from matplotlib import pyplot
from scipy import math
from mpl_toolkits.mplot3d import Axes3D
fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xPosition, yPosition, zPosition, c = velocity, s = mass)
ax.set_xlim3d(-plotSize, plotSize)
ax.set_ylim3d(-plotSize, plotSize)
ax.set_zlim3d(-plotSize, plotSize)
pyplot.savefig('plot.png')
我非常想知道如何在不在图形用户界面中打开情节的情况下获取我的情节的保存图像。
最佳答案
您应该使用 pylab.ioff() 作为 Saullo Castro 的亮点,并且每次您想要保存图形时使用 pylab.savefig('file.png')。当您不需要图形时,只需执行 pylab.close() 即可关闭当前图形(并释放内存)。
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
pyplot.ioff()
fig = pyplot.figure()
# HERE your code to add things in the figure
pyplot.savefig('file.png')
pyplot.close()
关于Python:保存绘图而不是在 GUI 中打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19740698/