我想创建一个直方图并将其保存到文件中,而不在屏幕上显示它。我现在的代码默认情况下会显示图形,我找不到任何方法来禁止显示图形。我已经尝试了pyplot.hist(nrs),但仍然存在同样的问题。
import math, time, matplotlib.pyplot as plt, pylab;
import numpy as np;
nrs = [1.0, 2.0, 1.0, 3.0, 4.0]
freq,bins = np.histogram(nrs)
fig = plt.figure(figsize=(5,4), dpi=100);
freq = np.append(freq, [0.0])
graph = fig.add_subplot(111);
x = graph.bar(bins, freq)
fig.savefig( "test.png")
plt.show()
或fig.show()
吗? - Srivatsanpython script.py
运行即可。这应该可以工作。如果没有调用plt.show()
,您唯一可能得到一个图形的方法是在 IPython 的交互模式下。 - rthAgg
)。请参见https://dev59.com/1HE85IYBdhLWcg3wYicB#3054314获取示例(那里有稍微不同的问题,但解决方案相同)。 - tacaswellplt.ioff()
将关闭自动绘图。 - Imanol Luengo