If I have a file of several data points. How can I plot a function of them? For example, suppose I have a file of to columns x
and y
and I want to plot sin(y)
as a function of 1/x
.
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.
GnuPlot scripts are stored in Excel along with the generated graphs and can be instantly edited and regenerated without leaving the Excel environment. Data can be read from Excel sheets by a cell range ("A1:C400") or using an Excel named range.
There are two ways to save your work in gnuplot: you can save the gnuplot commands used to generate a plot, so that you can regenerate the plot at a later time. Or you can export the graph to a file in a standard graphics file format, so that you can print it or include it in web pages, documents, or presentations.
splot is the command for drawing 3-d plots (well, actually projections on a 2-d surface, but you knew that). It can create a plot from functions or a data file in a manner very similar to the plot command. See plot (p. ) for features common to the plot (p. ) command; only differences are discussed in detail here.
Now everything is ready to make the data plot: by typing only gnuplot will produce a graph in your output destination. The default settings will use the first two columns of your data file, respectively x and y. To specify the columns to be plotted use the using specifier which means "plot the file using column 2 as X and column 4 as Y".
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.
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.
In the case your data set is a tridimensional file just use splot ad add the z-column There are also different style (see gnuplot documentation or Selecting a plotting style for further infos) for plotting points. As said before, the default style is point which will plot the same as if you do not type with point.
Try this:
plot "-" u (1/$1):(sin($2)) w l
2.00000 0.16104
3.00000 0.15604
4.00000 0.40055
5.00000 0.09972
e
Or when not using data files make use of the parametric
mode, which is described here
With this data file Data.csv
:
0.00000 0.33371
1.00000 0.13034
2.00000 0.16104
3.00000 0.15604
4.00000 0.40055
5.00000 0.09972
6.00000 0.25204
7.00000 -0.34172
8.00000 -0.04733
9.00000 -0.27211
10.00000 0.10229
11.00000 -0.30608
12.00000 0.11006
13.00000 -0.44390
14.00000 -0.16963
15.00000 0.03208
16.00000 0.32431
17.00000 0.15424
18.00000 0.16468
19.00000 0.24410
20.00000 0.34961
I can run this script:
f(x) = kappa*x**2
fit f(x) "Data.csv" u (log($1)):(sin($2)) via kappa
plot "Data.csv" u 1:(sin($2)), f(x) w l
Which gives me this plot:
Granted, the fitting does not make any sense at all^^.
Are you maybe missing some brackets in your fit?!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With