gnuplot与Matplotlib

2024-04-29 03:14:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的一般考虑是:

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

你将如何处理这项任务?


Tags: 项目内存文档py绘图matplotlib绘制事情
3条回答
  • 你可以自己检查matplotlib's documentation。我觉得很全面。
  • 我对gnuplot py的经验很少,所以我不能说它是否能做到gnuplot所能做到的一切。
  • Matplotlib是专门为Python编写和设计的,因此它非常适合Python习惯用法等。
  • Matplotlib是一个成熟的项目。美国宇航局用它来做一些事情。
  • 我已经在Matplotlib中绘制了数千万个点,它看起来仍然很漂亮,反应也很快。
  • 除了使用Matplotlib的面向对象方法之外,pylab接口还可以使绘图变得像在MATLAB中一样简单——也就是说,非常简单。
  • 至于从gnuplot py移植到matplotlib,我不知道。

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


我知道这封信是旧的,但我路过,想把我的两分钱。我的结论是:如果数据集不是很大,那么应该使用Matplotlib。它更容易,看起来也更好。但是,如果您真正需要性能,可以使用Gnuplot。我已经添加了一些代码来在您的机器上测试它,并亲自看看它是否有真正的区别(这不是一个真正的性能基准,但应该给出一个初步的想法)。

下图表示到的所需时间(秒):

  • 绘制随机散布图
  • 将图形保存为png文件

Gnuplot VS Matplotlib

配置:

  • 格努普洛特:5.2.2
  • gnuplot py:1.8
  • matplotlib:2.1.2版

我记得当运行在旧版本库的旧计算机上时,性能差距要大得多(对于大型散点图,相差约30秒)。

此外,正如评论中提到的,你可以得到同等质量的情节。但你必须花更多的汗水才能使用Gnuplot。


Here's the code to generate the graph如果你想在你的机器上试用一下:

# -*- 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有相当好的文档,并且看起来相当稳定。它制作的情节很美,肯定是“出版质量”。由于良好的文档和大量的在线示例代码,它很容易学习和使用,而且我认为您在将gnuplot代码翻译成它时不会遇到太多问题。毕竟,matplotlib被科学家用来绘制数据和准备报告,所以它包含了人们需要的一切。

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

相关问题 更多 >