# -*- 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()
Matplotlib = ease of use, Gnuplot = (slightly better) performance
I know this post is old and answered but I was passing by and wanted to put my two cents. Here is my conclusion: if you have a not-so-big data set, you should use Matplotlib. It's easier and looks better. However, if you really need performance, you could use Gnuplot. I've added some code to test it out on your machine and see for yourself if it makes a real difference (this is not a real performance benchmark but should give a first idea).
The following graph represents the required time (in seconds) to:
Plot a random scatter graph
Save the graph to a png file
Configuration:
gnuplot: 5.2.2
gnuplot-py: 1.8
matplotlib: 2.1.2
I remember the performance gap being much wider when running on an older computer with older versions of the libraries (~30 seconds difference for a large scatter plot).
Moreover, as mentionned in the comments, you can get equivalent quality of plots. But you will have to put more sweat into that to do it with 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()
matplotlib has pretty good documentation, and seems to be quite stable. The plots it produces are beautiful - "publication quality" for sure. Due to the good documentation and the amount of example code available online, it's easy to learn and use, and I don't think you'll have much trouble translating gnuplot code to it. After all, matplotlib is being used by scientists to plot data and prepare reports - so it includes everything one needs.
One marked advantage of matplotlib is that you can integrate it with Python GUIs (wxPython and PyQt, at least) and create GUI application with nice plots.
After using GNUplot (with my own Python wrapper) for a long time (and really not liking the 80s-looking output), I just started having a look at matplotlib. I must say I like it very much, the output looks really nice and the docs are high quality and extensive (although that also goes for GNUplot). The one thing I spent ages looking for in the matplotlib docs is how to write to an image file rather than to the screen! Luckily this page explains it pretty well: http://www.dalkescientific.com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html
About performance and plotting a great number of points: I compared this for a scatterplot of 500.000 points loaded from a text file and saved to a png, using gnuplot* and matplotlib.
500.000 points scatterplot
gnuplot: 5.171 s
matplotlib: 230.693 s
I ran it only once and the results don't look identical, but I think the idea is clear: gnuplot wins at performance.
*I used gnuplot directly since the gnuplotpy demo doesn't work out-of-the-box for me. Matplotlib wins at Python integration.
What Gnuplot can do Gnuplot-Py can do too. Because Gnuplot can be driven by pipe(pgnuplot).
Gnuplot-Py is just a thin layer for it. So you don't need worry about it.
Why I prefer gnuplot maybe the many output format(PDF, PS and LaTex), which is very useful in papers, and the default output looks more scientific-style :)
发布评论
评论(8)
Matplotlib = 易用性,Gnuplot = (稍微好一点)性能
我知道这篇文章很旧并且已经回答了,但我路过并想投入我的两分钱。 我的结论是:如果你的数据集不太大,你应该使用 Matplotlib。 它更容易并且看起来更好。 但是,如果您确实需要性能,则可以使用 Gnuplot。 我添加了一些代码来在您的计算机上对其进行测试,并亲自看看它是否有真正的区别(这不是真正的性能基准,但应该给出第一个想法)。
下图表示执行以下操作所需的时间(以秒为单位):
配置:
我记得在具有旧版本库的旧计算机上运行时性能差距要大得多(对于大散点图大约有 30 秒的差异) 。
此外,正如评论中提到的,您可以获得同等质量的绘图。 但是你必须付出更多的努力才能使用 Gnuplot 来完成它。
这里是生成图表的代码(如果您想在自己的计算机上尝试一下):
Matplotlib = ease of use, Gnuplot = (slightly better) performance
I know this post is old and answered but I was passing by and wanted to put my two cents. Here is my conclusion: if you have a not-so-big data set, you should use Matplotlib. It's easier and looks better. However, if you really need performance, you could use Gnuplot. I've added some code to test it out on your machine and see for yourself if it makes a real difference (this is not a real performance benchmark but should give a first idea).
The following graph represents the required time (in seconds) to:
Configuration:
I remember the performance gap being much wider when running on an older computer with older versions of the libraries (~30 seconds difference for a large scatter plot).
Moreover, as mentionned in the comments, you can get equivalent quality of plots. But you will have to put more sweat into that to do it with Gnuplot.
Here's the code to generate the graph if you want to give it a try on your machine:
matplotlib
有非常好的文档,而且似乎相当稳定。 它产生的情节很漂亮——“出版质量”是肯定的。 由于有良好的文档和大量在线示例代码,它很容易学习和使用,而且我认为将gnuplot
代码翻译成它不会有太多麻烦。 毕竟,科学家正在使用 matplotlib 来绘制数据和准备报告 - 因此它包含了人们需要的一切。matplotlib 的一个显着优势是您可以将其与 Python GUI 集成 (wxPython 和 PyQt,位于至少)并创建具有漂亮绘图的 GUI 应用程序。
matplotlib
has pretty good documentation, and seems to be quite stable. The plots it produces are beautiful - "publication quality" for sure. Due to the good documentation and the amount of example code available online, it's easy to learn and use, and I don't think you'll have much trouble translatinggnuplot
code to it. After all, matplotlib is being used by scientists to plot data and prepare reports - so it includes everything one needs.One marked advantage of matplotlib is that you can integrate it with Python GUIs (wxPython and PyQt, at least) and create GUI application with nice plots.
在使用 GNUplot(带有我自己的 Python 包装器)很长一段时间(并且真的不喜欢 80 年代的输出)之后,我刚刚开始了解 matplotlib。 我必须说我非常喜欢它,输出看起来非常好,文档质量高且内容广泛(尽管 GNUplot 也是如此)。 我花了很长时间在 matplotlib 文档中寻找的一件事是如何写入图像文件而不是屏幕! 幸运的是,这个页面解释得很好: http://www .dalkescientific.com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html
After using GNUplot (with my own Python wrapper) for a long time (and really not liking the 80s-looking output), I just started having a look at matplotlib. I must say I like it very much, the output looks really nice and the docs are high quality and extensive (although that also goes for GNUplot). The one thing I spent ages looking for in the matplotlib docs is how to write to an image file rather than to the screen! Luckily this page explains it pretty well: http://www.dalkescientific.com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html
关于性能和绘制大量点:我使用 gnuplot* 和 matplotlib 将其与从文本文件加载并保存到 png 的 500.000 点散点图进行了比较。
我只运行了一次,结果看起来并不相同,但我认为这个想法很明确:gnuplot 在性能上获胜。
*我直接使用了 gnuplot,因为 gnuplotpy 演示对我来说不是开箱即用的。 Matplotlib 在 Python 集成方面获胜。
About performance and plotting a great number of points: I compared this for a scatterplot of 500.000 points loaded from a text file and saved to a png, using gnuplot* and matplotlib.
I ran it only once and the results don't look identical, but I think the idea is clear: gnuplot wins at performance.
*I used gnuplot directly since the gnuplotpy demo doesn't work out-of-the-box for me. Matplotlib wins at Python integration.
我都用过这两种语言,而且在 Python 集成、选项和图形/绘图质量方面我更喜欢 Matplotlib。
I have played with both, and I like Matplotlib much better in terms of Python integration, options, and quality of graphs/plots.
gnuplot
的一些专家(使用多年后我仍然不喜欢 matlibplot):sin(x)
绘制函数(不需要定义数组并考虑范围)")
gplot. py 是 python 和 jupyter 的另一个包装器 gnuplot 包装器。
Some pro's of
gnuplot
(I still don't like matlibplot after years of usage):sin(x)
(no need to define arrays and think about ranges)"<echo 1 2 3"
)gplot.py is another wrapper gnuplot wrapper for python and jupyter.
Gnuplot 能做的事 Gnuplot-Py 也能做。 因为Gnuplot可以通过pipe(pgnuplot)来驱动。
Gnuplot-Py 只是它的一个薄层。 所以你不需要担心它。
为什么我更喜欢 gnuplot 也许是它的多种输出格式(PDF、PS 和 LaTex),这在论文中非常有用,而且默认输出看起来更科学风格:)
What Gnuplot can do Gnuplot-Py can do too. Because Gnuplot can be driven by pipe(pgnuplot).
Gnuplot-Py is just a thin layer for it. So you don't need worry about it.
Why I prefer gnuplot maybe the many output format(PDF, PS and LaTex), which is very useful in papers, and the default output looks more scientific-style :)