Matplotlib = 使用简便,Gnuplot = (稍微更好的)性能
我知道这篇帖子已经老旧并且有答案了,但我路过这里,想要分享我的看法。这是我的结论:如果你的数据集不算太大,你应该使用Matplotlib。它更容易使用,看起来更好看。然而,如果你真的需要性能,可以使用Gnuplot。我添加了一些代码,可以在你的机器上测试一下,看看是否会有真正的差别(这不是真正的性能基准测试,但应该可以给出一个初步想法)。
以下图表表示所需的时间(以秒为单位):
配置:
- gnuplot: 5.2.2
- gnuplot-py: 1.8
- matplotlib: 2.1.2
我记得在旧电脑和旧版本的库上运行时,性能差距要大得多(对于大型散点图相差约30秒)。
此外,如评论中所述,你可以获得与Gnuplot相当的绘图质量。但你需要付出更多的努力来使用Gnuplot实现。
这里是生成图表的代码,如果你想在自己的机器上试一试:
from timeit import default_timer as timer
import matplotlib.pyplot as plt
import Gnuplot, Gnuplot.funcutils
import numpy as np
import sys
import os
def mPlotAndSave(x, y):
plt.scatter(x, y)
plt.savefig('mtmp.png')
plt.clf()
def gPlotAndSave(data, g):
g("set output 'gtmp.png'")
g.plot(data)
g("clear")
def cleanup():
try:
os.remove('gtmp.png')
except OSError:
pass
try:
os.remove('mtmp.png')
except OSError:
pass
begin = 2
end = 500000
step = 10000
numberOfPoints = range(begin, end, step)
n = len(numberOfPoints)
gnuplotTime = []
matplotlibTime = []
progressBarWidth = 30
g = Gnuplot.Gnuplot()
g("set terminal png size 640,480")
plt.clf()
for idx, val in enumerate(numberOfPoints):
sys.stdout.write('\r')
progress = (idx+1)*progressBarWidth/n
bar = "▕" + "▇"*progress + "▁"*(progressBarWidth-progress) + "▏" + str(idx) + "/" + str(n-1)
sys.stdout.write(bar)
sys.stdout.flush()
x = np.random.randint(sys.maxint, size=val)
y = np.random.randint(sys.maxint, size=val)
gdata = zip(x,y)
start = timer()
mPlotAndSave(x, y)
end = timer()
matplotlibTime.append(end - start)
start = timer()
gPlotAndSave(gdata, g)
end = timer()
gnuplotTime.append(end - start)
cleanup()
del g
sys.stdout.write('\n')
plt.plot(numberOfPoints, gnuplotTime, label="gnuplot")
plt.plot(numberOfPoints, matplotlibTime, label="matplotlib")
plt.legend(loc='upper right')
plt.xlabel('Number of points in the scatter graph')
plt.ylabel('Execution time (s)')
plt.savefig('execution.png')
plt.show()