Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short guide how to use gnuplot with python?

Tags:

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!

like image 776
Fredrik Nylund Avatar asked Nov 10 '11 09:11

Fredrik Nylund


People also ask

How do I start gnuplot?

To start gnuplot under MS Windows, double-click on the gnuplot icon. The gnuplot window will pop up with menus and buttons along the top, the opening message and the gnuplot> prompt inside the window. To start gnuplot under OS/2, open the folder where gnuplot is located, and double click on the gnuplot icon.

How use gnuplot data file?

To plot functions simply type: plot [function] at the gnuplot> prompt. Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes) on the plot or splot command line. Data files should have the data arranged in columns of numbers.


2 Answers

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.

like image 60
joaquin Avatar answered Sep 20 '22 00:09

joaquin


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.

like image 45
Woltan Avatar answered Sep 19 '22 00:09

Woltan