Matplotlib = 易用性,Gnuplot =(稍好)性能
我知道这篇文章很旧并且已经回答,但我路过并想投入我的两分钱。这是我的结论:如果你有一个不太大的数据集,你应该使用 Matplotlib。它更容易,看起来更好。但是,如果你真的需要性能,你可以使用 Gnuplot。我已经添加了一些代码来在您的机器上对其进行测试,并亲自查看它是否会产生真正的影响(这不是真正的性能基准,但应该给出第一个想法)。
下图表示执行以下操作所需的时间(以秒为单位):
![Gnuplot VS Matplotlib](https://i.stack.imgur.com/zfCmR.png)
配置:
- gnuplot:5.2.2
- gnuplot-py:1.8
- matplotlib:2.1.2
我记得在具有旧版本库的旧计算机上运行时,性能差距要大得多(大散点图的差异约为 30 秒)。
此外,正如评论中提到的,您可以获得同等质量的情节。但是你必须付出更多的努力才能使用 Gnuplot 来做到这一点。
如果你想在你的机器上试一试,这里是生成图表的代码:
# -*- coding: utf-8 -*-
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
# Init Gnuplot
g = Gnuplot.Gnuplot()
g("set terminal png size 640,480")
# Init matplotlib to avoid a peak in the beginning
plt.clf()
for idx, val in enumerate(numberOfPoints):
# Print a nice progress bar (crucial)
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()
# Generate random data
x = np.random.randint(sys.maxint, size=val)
y = np.random.randint(sys.maxint, size=val)
gdata = zip(x,y)
# Generate string call to a matplotlib plot and save, call it and save execution time
start = timer()
mPlotAndSave(x, y)
end = timer()
matplotlibTime.append(end - start)
# Generate string call to a gnuplot plot and save, call it and save execution time
start = timer()
gPlotAndSave(gdata, g)
end = timer()
gnuplotTime.append(end - start)
# Clean up the files
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()