April 27th, 2010 | 14 Comments
Plotting data like measurement results is probably the most used method of plotting in gnuplot. It works basically like the plotting of functions. But in this case we need a data file and some commands to manipulate the data. First, we will start with the basic plotting of simple data and thereafter look at the plotting of data with errors.
Simple data
At first we will have a look at a data file. This can be a text file containing the datapoints as columns.
# plotting_data1.dat # X Y 1 2 2 3 3 2 4 1
You can plot these by writing
set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1.5 # --- blue plot 'plotting-data1.dat' with linespoints
Here we also set the point type (pt) and the point size (ps) to use. For the available point styles you can have a look at the ps_symbols file.
The resulting plot is presented in Fig. 1.
Fig. 1 Plot of the data from plotting_data1.dat (code to produce this figure)
If you have data points that aren’t continuous you can simply tell gnuplot this by inserting one blank line between the data.
# plotting_data2.dat # X Y 1 2 2 3 3 2 4 1
Fig. 2 Plot of the data from plotting_data2.dat as a non-continuous line (code to produce this figure)
If you want to use another color for the second data and still want to have it in the same file, you can insert a second blank line. You then have to index the data block starting by 0.
# plotting_data3.dat # First data block (index 0) # X Y 1 2 2 3 # Second index block (index 1) # X Y 3 2 4 1
set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1.5 # --- blue set style line 2 lc rgb '#dd181f' lt 1 lw 2 pt 5 ps 1.5 # --- red plot 'plotting-data3.dat' index 0 with linespoints ls 1, \ '' index 1 with linespoints ls 2
As you can see, we have added another color and point type and plotted the two datasets by using index and separated the plots by a comma. To reuse the last filename we can just type ''. The result is shown in Fig. 3.
Fig. 3 Plot of the data from plotting_data3.dat in two different styles (code to produce this figure)
Data with errors
Another common task is to plot data with errorbars. Therefore we use the battery.dat file from gnuplots demo files that contains data about the dependence of the power of the battery on the resistance.
Here we want not only to plot the data, but also show the error for the y value (the data is stored in the format: x, y, xerror, yerror).
set xrange [-2:52] set yrange [0:0.12] set format y '%.0s' plot 'battery.dat' using 1:2:4 w yerrorbars ls 1, \ '' using 1:2 w lines ls 1
The power values are stored in Watt in the data file, but only has values lower than 1. That’s why we want to use mW as unit. Therefore we set the format option to tell gnuplot to use “mantissa to base of current logscale”, see gnuplot’s documentation. Then in the plot command using tells gnuplot which columns from the data file it should use. Since we want to plot the y errors and the data we need three columns in the first line of the plot command. Using the yerrorbars plotting style it is not possible to combine the points by a line. Therefore we add a second line to the plot command to combine the points with a line. This will give us the resulting Fig. 4.
Fig. 4 Plot of the data from battery.dat with y errors (code to produce this figure)
We can avoid the set format command in the last plot by directly manipulating the input data:
set yrange [0:120] plot 'battery.dat' using 1:($2*1000):($4*1000) w yerrorbars ls 1
For achieving this we have to set brackets around the expression and reference the column data with $column_number.
In the last plot we will add theoretical data and a legend to the graph:
# Legend set key at 50,112 # Therotecial curve P(x) = 1.53**2 * x/(5.67+x)**2 * 1000 plot 'battery.dat' using 1:($2*1000):($4*1000) \ title 'Power' w yerrorbars ls 2, \ P(x) title 'Theory' w lines ls 1
Generally the legend is enabled by the set key command. In addition to that, its position can be specified by set key top left etc. You can also set it directly to one point as we have done it here in order to have enough space between the key and the tics. The title keyword within the plot command specifies the text to be displayed in the legend.
Fig. 5 Plot of the data from battery.dat with y errors and a theoretical curve (code to produce this figure)
Now you should be able to plot your own data with gnuplot. You may also want to look at how to plot functions, or dealing with gnuplot’s different output terminals.
Nice site on gnuplot, always very helpful to see any program in action.
Minor: resitance should be resistance
Thanks for the resistance hint ;)
[...] give me some more data for gnuplot practise. I’m going to use the excellent tutorial I found here to make some nice plots of this [...]
Hi,
since this part of the blog is regarding data-sets i was wondering if you could come up with a solution for the follwing:
When plotting from datasets (large entries; >2000) and using the “with filledcurves”-option to overlap them- the overlapped area is full of artefacts from the lines in the background.
It seems like the the stripes forming the filled area have very small gaps but since the amount of data is very dense, it creates some sort of transparency for the background.
Any idea? Would be really awesome.
Btw: very good site on gnuplot!!
Matthias
I can’t reproduce this. In 3D plots I know this problem, but not in 2D plots.
Can you give me more details or sent me your data?
Nice tutorial on gnuplot.
I would like to know if there is a way to automatically change color while plotting multiple columns in a data file.
> plot “filename” w lp
generates a bunch of curves, one for each col, all with the same color. I would like the color to cycle through instead of staying at the default color in the default linestyle (red, in my case)
Separately, it would be nice if you could add a section on plotting data that is saved row-wise instead of column wise. For instance,
>plot “filename” matrix w lp
generates bunch of curves, one for each row of data.
Interesting that
> plot “filename” w lp
creates a bunch of lines in your case. For me it always plots only one line using the first two columns. But you can get the desired result by applying:
> plot for [n=2:10] “filename” u 1:n w lp
This will create 9 curves with different colors, using the first column in your data file as x-axis and the nths column as y-axis.
I noticed that, in contrast to gnuplot 4.4 (Windows 32 bit), gnuplot 4.6 (3/2012) pads automatically the x and y axis ranges by ~10%. Is there a set parameter to eliminate the padding for the xrange so that the rightmost point is the xrange?
I have tested this with the battery.dat data from above by plotting it with
but didn’t get any difference between Gnuplot 4.4 and 4.6.
Can you post an example were a difference occurs?
how can i plot the data for his please help me out , for a text file .txt
x=-0012 y=-0003 z= +0068
@ anvesh kumar -
gnuplot expects and prefers data with just numbers, no text.
You can tell it to ignore rows by inserting a preceding #, like BASH.
The best approach would be to have the program logging your data to skip logging the text. Use colrm if you know your data is always logged exactly the same. otherwise it’s a trivial job for sed.
http://www.grymoire.com/Unix/Sed.html
Great website! thx!
THANKS! fortunately there’re people like you who cares about ignorant people like me! XD
I want to know when and how to use set table command?