gnuplot vs Matplotlib [closed]
Asked Answered
I

8

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?

Imam answered 26/5, 2009 at 16:49 Comment(1)
Another gnuplot wrapper is gplot.py which also works in jupyter.Townscape
A
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.
Arteaga answered 26/5, 2009 at 17:12 Comment(5)
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.Freyah
@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: code.enthought.com/projects/mayavi .Arteaga
matplotlib now has a 3D toolkitQuintanilla
"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.Oligarch
Biased. you had "very little experience with gnuplot-py". Information given is all about matplotlib. Expression about matplotlib is also too subjective.Divest
B
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()
Briscoe answered 27/5, 2014 at 7:21 Comment(1)
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!Functionalism
R
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.

Rodas answered 26/5, 2009 at 17:9 Comment(0)
R
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

Rafat answered 25/12, 2009 at 10:1 Comment(1)
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).Functionalism
L
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.

Liriodendron answered 10/1, 2015 at 21:26 Comment(0)
H
8

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

Herstein answered 26/5, 2009 at 17:15 Comment(0)
H
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 :)

Heterodyne answered 2/3, 2013 at 14:29 Comment(0)
I
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.

Illuminati answered 24/2, 2020 at 21:40 Comment(2)
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?Ossuary
@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.Townscape

© 2022 - 2024 — McMap. All rights reserved.