gnuplot 与 Matplotlib

发布于 2024-07-20 21:27:02 字数 1431 浏览 10 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

再浓的妆也掩不了殇 2024-07-27 21:27:02
  • 您可以自行查看 matplotlib 的文档。 我觉得很全面。
  • 我对 gnuplot-py 的经验很少,所以我不能说它是否可以完成 gnuplot 的所有功能。
  • Matplotlib 是专门为 Python 编写和设计的,因此它非常适合 Python 习惯用法等。
  • Matplotlib 是一个成熟的项目。 美国国家航空航天局 (NASA) 将其用于某些用途。
  • 我已经在 Matplotlib 中绘制了数千万个点,它看起来仍然很漂亮并且响应速度很快。
  • 除了使用 Matplotlib 的面向对象方式之外,还有 pylab 接口,它使得绘图像在 MATLAB 中一样简单——也就是说,非常简单。
  • 至于从 gnuplot-py 移植到 matplotlib,我不知道。
  • You can check matplotlib's documentation yourself. I find it quite comprehensive.
  • I have very little experience with gnuplot-py, so I can not say whether it can do all gnuplot can.
  • Matplotlib is written in and designed specifically for Python, so it fits very nicely with Python idioms and such.
  • Matplotlib is a mature project. NASA uses it for some stuff.
  • I've plotted tens of millions of points in Matplotlib, and it still looked beautiful and responded quickly.
  • Beyond the object-oriented way of using Matplotlib is the pylab interface, which makes plotting as easy as it is in MATLAB -- that is, very easy.
  • As for porting from gnuplot-py to matplotlib, I have no idea.
最佳男配角 2024-07-27 21:27:02

Matplotlib = 易用性,Gnuplot = (稍微好一点)性能


我知道这篇文章很旧并且已经回答了,但我路过并想投入我的两分钱。 我的结论是:如果你的数据集不太大,你应该使用 Matplotlib。 它更容易并且看起来更好。 但是,如果您确实需要性能,则可以使用 Gnuplot。 我添加了一些代码来在您的计算机上对其进行测试,并亲自看看它是否有真正的区别(这不是真正的性能基准,但应该给出第一个想法)。

下图表示执行以下操作所需的时间(以秒为单位):

  • 绘制随机散点图
  • 将图形保存到 png 文件

Gnuplot VS Matplotlib

配置:

  • 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()

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

Gnuplot VS Matplotlib

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.


Here's the code to generate the graph if you want to give it a try on your machine:

# -*- 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()
云淡风轻 2024-07-27 21:27:02

matplotlib 有非常好的文档,而且似乎相当稳定。 它产生的情节很漂亮——“出版质量”是肯定的。 由于有良好的文档和大量在线示例代码,它很容易学习和使用,而且我认为将 gnuplot 代码翻译成它不会有太多麻烦。 毕竟,科学家正在使用 matplotlib 来绘制数据和准备报告 - 因此它包含了人们需要的一切。

matplotlib 的一个显着优势是您可以将其与 Python GUI 集成 (wxPythonPyQt,位于至少)并创建具有漂亮绘图的 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 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.

把昨日还给我 2024-07-27 21:27:02

在使用 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

深海里的那抹蓝 2024-07-27 21:27:02

关于性能和绘制大量点:我使用 gnuplot* 和 matplotlib 将其与从文本文件加载并保存到 png 的 500.000 点散点图进行了比较。

500.000 points scatterplot
gnuplot:      5.171 s
matplotlib: 230.693 s

我只运行了一次,结果看起来并不相同,但我认为这个想法很明确: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.

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.

盛夏已如深秋| 2024-07-27 21:27:02

我都用过这两种语言,而且在 Python 集成、选项和图形/绘图质量方面我更喜欢 Matplotlib。

I have played with both, and I like Matplotlib much better in terms of Python integration, options, and quality of graphs/plots.

孤独难免 2024-07-27 21:27:02

gnuplot 的一些专家(使用多年后我仍然不喜欢 matlibplot):

  • 简单地使用 sin(x) 绘制函数(不需要定义数组并考虑范围)
  • 直接绘制文件(无需导入数组)
  • 绘制管道数据(即时执行 shell 命令 ")
  • 复制到剪贴板按钮
  • 更快地绘图
  • 更快的编码
  • 关键字 更容易记住

gplot. py 是 python 和 jupyter 的另一个包装器 gnuplot 包装器。

Some pro's of gnuplot (I still don't like matlibplot after years of usage):

  • plot function simply with sin(x) (no need to define arrays and think about ranges)
  • plot files directly (no need to import into an array)
  • plot piped-data (execute shell commands on the fly "<echo 1 2 3")
  • copy-to-clipboard button
  • faster plotting
  • faster coding
  • keywords easier to remember

gplot.py is another wrapper gnuplot wrapper for python and jupyter.

鹿童谣 2024-07-27 21:27:02

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文