93

I've started on a project graphing Tomcat logs using gnuplot-py, specifically correlating particular requests with memory allocation and garbage collection. What is the collective wisdom on gnuplot-py vs Matplotlib for Python graphing. Are there better graphing libraries out there I haven't heard of?

My general considerations are:

  • While gnuplot has large amounts of documentation, gnuplot-py doesn't. How good is documentation community for Matplotlib?
  • Are there things which gnuplot can do, but gnuplot-py can't?
  • Does Matplotlib have better Python support?
  • Are there are big show stopping bugs in either? Annoyances?
  • Currently gnuplot is graphing 100,000's of points, I'm planning on scaling this up to millions. Should I expect problems? How well does Matplotlib handle this?
  • Ease of use, turnaround time for gnuplot vs Matplotlib?
  • How easy would it be to port existing gnuplot-py code to Matplotlib?

How would you approach this task?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Ethan Heilman
  • 16,347
  • 11
  • 61
  • 88

8 Answers8

54
  • 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.
Cimbali
  • 11,012
  • 1
  • 39
  • 68
Autoplectic
  • 7,566
  • 30
  • 30
  • 3
    The only plus I can say for gnuplot is that matplotlib does not have 3D plotting capabilities. Besides that, I've used both an prefer matplotlib by far. – physicsmichael May 26 '09 at 23:24
  • 1
    @vgm64: current SVN has 3d added back in. i haven't tested any of it myself, so i can't say how nice it is. for 3d plotting i use mayavi2: http://code.enthought.com/projects/mayavi/ . – Autoplectic May 27 '09 at 01:36
  • 10
    matplotlib now has a [3D toolkit](http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/index.html#mplot3d) – Kit Aug 09 '10 at 03:20
  • 11
    "Matplotlib is written in and designed specifically for Python" - I have to disagree. The matplotlib API is so far from 'typical python' that it hurts. If anything it mimics matlab semantics. – Ole Sep 25 '13 at 14:34
  • 7
    Biased. you had "very little experience with gnuplot-py". Information given is all about matplotlib. Expression about matplotlib is also too subjective. – squid Jul 01 '15 at 16:25
50

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()
7hibault
  • 2,371
  • 3
  • 23
  • 33
  • 9
    Moreover, I would to add that in terms of quality of plot, they are equivalent **if** someone does not just go with the default styles. Moreover, *gnuplot* can be called easily without having to run *Python*, so it is language independent! – Atcold Aug 12 '14 at 21:21
26

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.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
18

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

Wim
  • 11,091
  • 41
  • 58
  • 12
    I have to disagree about the 80s-looking output of *gnuplot* (which is spelled *gnuplot* and not *GPUplot*). If you use some custom styles (you have to define them only once), you end up with beautiful plot. Just check out how others have been using this amazing piece of software ([reference](http://www.gnuplotting.org/)). – Atcold Aug 12 '14 at 21:16
10

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.

Mark
  • 18,730
  • 7
  • 107
  • 130
8

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

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
5

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

joseph.smeng
  • 81
  • 1
  • 4
5

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.

  • Although I create Python programs with GUI I haven't used matplotlib, so I cannot compare. Instead, I use a "library" of gnuplot scripts, i.e. textfiles, executed via the Python program to plot the data. I'm not sure whether I could use similar templates in matplotlib or whether everytime I do a little change on a plot layout I would have to recompile the program again and again and ask all users to update the Python program instead of just updating a small textfile. Maybe you can comment on this? – theozh Jan 19 '21 at 07:35
  • @theozh: it's off-topic here, but I guess you are are looking for an analogue of `load`, Python has `reload` for modules and `eval` for string snippets. Remember calling gnuplot scripts from python is similar to dynamic imports. – Friedrich -- Слава Україні Jan 19 '21 at 13:31