Fredrik Nylund
Fredrik Nylund

Reputation: 221

Short guide how to use gnuplot with python?

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!

Upvotes: 22

Views: 62475

Answers (4)

Acorbe
Acorbe

Reputation: 8391

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.

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

Upvotes: 0

joaquin
joaquin

Reputation: 85615

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.

Upvotes: 14

Woltan
Woltan

Reputation: 14023

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.

Upvotes: 12

Related Questions