90

我已经开始使用gnuplot-py绘制Tomcat日志的项目,特别是将特定请求与内存分配和垃圾收集相关联。用于 Python 绘图的 gnuplot-py 与Matplotlib的集体智慧是什么。有没有我没听说过的更好的图形库?

我的一般考虑是:

  • 虽然 gnuplot 有大量文档,但 gnuplot-py 没有。Matplotlib 的文档社区有多好?
  • 有没有 gnuplot 可以做但 gnuplot-py 不能做的事情?
  • Matplotlib 是否有更好的 Python 支持?
  • 是否有大型节目阻止错误?烦恼?
  • 目前 gnuplot 正在绘制 100,000 个点,我计划将其扩展到数百万个。我应该期待问题吗?Matplotlib 处理得如何?
  • gnuplot vs Matplotlib 的易用性、周转时间?
  • 将现有的 gnuplot-py 代码移植到 Matplotlib 有多容易?

您将如何处理这项任务?

4

8 回答 8

54
  • 你可以自己查看matplotlib 的文档。我觉得挺全面的。
  • 我对 gnuplot-py 的经验很少,所以我不能说它是否可以做到所有 gnuplot 可以。
  • Matplotlib 是专门为 Python 编写和设计的,因此它非常适合 Python 习语等。
  • Matplotlib 是一个成熟的项目。NASA 用它来做一些事情。
  • 我在 Matplotlib 中绘制了数千万个点,它仍然看起来很漂亮,反应很快。
  • 除了使用 Matplotlib 的面向对象方式之外,还有 pylab 接口,它使绘图变得像在 MATLAB 中一样简单——也就是说,非常简单。
  • 至于从 gnuplot-py 移植到 matplotlib,我不知道。
于 2009-05-26T17:12:05.803 回答
49

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()
于 2014-05-27T07:21:57.490 回答
24

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

matplotlib 的一个显着优势是您可以将它与 Python GUI(至少是wxPythonPyQt )集成并创建具有漂亮绘图的 GUI 应用程序。

于 2009-05-26T17:09:47.540 回答
18

在使用 GNUplot(使用我自己的 Python 包装器)很长时间(并且真的不喜欢 80 年代的输出)之后,我才开始研究 matplotlib。我必须说我非常喜欢它,输出看起来非常好,文档质量高且内容广泛(尽管这也适用于 GNUplot)。我花了很长时间在 matplotlib 文档中寻找的一件事是如何写入图像文件而不是屏幕!幸运的是,这个页面解释得很好:http ://www.dalkescientific.com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html

于 2009-12-25T10:01:31.897 回答
8

两者我都玩过,我更喜欢 Matplotlib 在 Python 集成、选项和图形/绘图质量方面。

于 2009-05-26T17:15:51.020 回答
8

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

500.000 points scatterplot
gnuplot:      5.171 s
matplotlib: 230.693 s

我只运行了一次,结果看起来并不相同,但我认为这个想法很明确:gnuplot 在性能上获胜。

*我直接使用了 gnuplot,因为 gnuplotpy 演示对我来说不能开箱即用。Matplotlib 在 Python 集成中获胜。

于 2015-01-10T21:26:25.227 回答
4

Gnuplot 可以做什么 Gnuplot-Py 也可以。因为 Gnuplot 可以由管道(pgnuplot)驱动。Gnuplot-Py 只是它的一个薄层。所以你不需要担心它。

为什么我更喜欢 gnuplot 可能是多种输出格式(PDF、PS 和 LaTex),这在论文中非常有用,而且默认输出看起来更科学:)

于 2013-03-02T14:29:39.157 回答
4

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

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

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

于 2020-02-24T21:40:15.057 回答