23

I'm trying to draw a graph in Python, using Gnuplot. I have a hard time finding any guide/tutorials how to start.

What I'm wondering: what files/programs are necessary?(I'm using Ubuntu), Where do I begin?

If anyone could recommend a good tutorial, that would be very appreciated!

Thank you!

Fredrik Nylund
  • 231
  • 1
  • 2
  • 4
  • 1
    Hello, I had the same problem in that past. I found matplotlib way easier to use. Have a look : http://matplotlib.sourceforge.net/ I think it's quite amazing, and you get plenty of examples there. Download button is on the right handside – BuZz Nov 10 '11 at 09:12

4 Answers4

14

You could try gnuplot.py. It is an interface to gnuplot I used in the past. In the website you have some indications and there are some example scripts in the distribution.

In fact it is very easy to run directly gnuplot from python. The gnuplot.py source code will give you valuable hints. See also here and here for other alternatives.

As other recommends the alternative is to use matplotlib. Matplotlib is great and I use it as my main visualization library. The downside is that working with a high number of data it can become slow. gnuplot in this case is a good option.

Community
  • 1
  • 1
joaquin
  • 82,968
  • 29
  • 138
  • 152
12

Your approach depends on what you already have and what you want to work with. To plot a graph with gnuplot you need two things:

  1. A gnuplot script, that describes how the resulting plot should look like (title, axis description, legend...)
  2. A data file, which holds the data you want to plot

If you already have lets say the gnuplot script file and you simply want to write new data files using python, than this approach is sound in my option. Simply export data to the specified format you used in your data files before and run gnuplot from within python with something like

import os
import subprocess
p = subprocess.Popen("gnuplot <scriptname>", shell = True)
os.waitpid(p.pid, 0)

Don't forget that you maybe have to change the path the data file in your gnuplot script if you write out new data files. So something like this:

plot "<path>" ...

If you don't yet have a gnuplot script you want to use you can definitely write one and use that from this point on, but using python there are also other alternatives.

You could take a look at matplotlib which is a plotting library that is very similar in the way Matlab uses the plot command. It is very well documented and there are lots of tutorials and examples online you can learn from and work with.

Woltan
  • 13,723
  • 15
  • 78
  • 104
0

As a gnuplot fan, I use this gnuplot wrapper https://github.com/mzechmeister/python/wiki/gplot.py.

Here is a demo snippet

from gplot import *

gplot.term('wxt')
gplot.title('"gplot.py"').grid()
gplot.xlabel('"time"')
gplot([1,2,0,4,3.5], 'w l, sin(x), "<seq 10" us 1:(cos($1))')

0

about 10 years later, let me point the attention to autogpy or Autognuplotpy.

Autogpy aims at a full generation of gnuplot scripts (and suitably dumped data) from python.

For instance, the python code

import autogpy
import numpy as np

xx = np.linspace(0,6,100)
yy = np.sin(xx)
zz = np.cos(xx)

with autogpy.AutogpyFigure("test_figure") as figure: 

    # gnuplot-like syntax
    figure.plot(r'with lines t "sin"',xx,yy)
    
    # matplotlib-like syntax
    figure.plot(xx,zz,u='1:2',w='lines',label='cos')

generates the gnuplot script

set terminal epslatex size 9.9cm,8.cm color colortext standalone      'phv,12 '  linewidth 2
set output 'fig.latex.nice/plot_out.tex'

p "fig__0__.dat" with lines t "sin",\
"fig__1__.dat" u 1:2 with lines t "cos" 

and dumps readable data.

Supports latex, tiks and png terminal, but can be easily expanded to more.

Disclaimer: I am the author.

Acorbe
  • 8,367
  • 5
  • 37
  • 66
  • When I try and use this, I cannot actually see any plots? @Acorbe – Noah P Oct 28 '20 at 11:31
  • @NoahP, thanks for your post. have you allowed imagemagick to perform conversions ` sudo sed -i '/PDF/s/none/read|write/' /etc/ImageMagick-6/policy.xml `? if this does not solve the problem, can you please post your issue on github? with some more detail? https://github.com/acorbe/autogpy thanks – Acorbe Oct 28 '20 at 12:29