<h2>Matplotlib=易用性,Gnuplot=(稍好一点)性能</h2>
<hr/>
<p>我知道这封信是旧的,但我路过,想把我的两分钱。我的结论是:如果数据集不是很大,那么应该使用Matplotlib。它更容易,看起来也更好。但是,如果您真正需要性能,可以使用Gnuplot。我已经添加了一些代码来在您的机器上测试它,并亲自看看它是否有真正的区别(这不是一个真正的性能基准,但应该给出一个初步的想法)。</p>
<p>下图表示到的所需时间(秒):</p>
<ul>
<li>绘制随机散布图</li>
<li>将图形保存为png文件</li>
</ul>
<p><img src="https://i.stack.imgur.com/zfCmR.png" alt="Gnuplot VS Matplotlib"/></p>
<p>配置:</p>
<ul>
<li>格努普洛特:5.2.2</li>
<li>gnuplot py:1.8</li>
<li>matplotlib:2.1.2版</li>
</ul>
<p>我记得当运行在旧版本库的旧计算机上时,性能差距要大得多(对于大型散点图,相差约30秒)。</p>
<p>此外,正如评论中提到的,你可以得到同等质量的情节。但你必须花更多的汗水才能使用Gnuplot。</p>
<hr/>
<p><a href="https://gist.github.com/7hibault/5d7b9a6fa29b61653974e381248073b0" rel="noreferrer">Here's the code to generate the graph</a>如果你想在你的机器上试用一下:</p>
<pre><code># -*- 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()
</code></pre>