Is there a quick way to plot the Maclaurin/Taylor series using Gnuplot. For eg:
http://en.wikipedia.org/wiki/Power_series
I'm trying to plot each term of the series NOT the series f(x) vs x.
So, for sin(x) I'd like to plot on the x-axis, x, -x^3/3!, x^5/5! etc and the sum itself all on one axis without having to type each term in the sequence manually.
You can plot functions in gnuplot. In this case, you would have to define your series, for example:
gnuplot> a0 = 1
gnuplot> a1 = 0.5
gnuplot> a2 = 0.1
gnuplot> f(x) = a0 + a1*x**2 + a2*x**3
gnuplot> plot f(x)
This will give you:
Edit
Based on the comment to this answer, I'm proposing this (which should work for gnuplot 4.4 and higher):
First, define your factorial:
gnuplot> fac(n) = (n==0) ? 1 : n * fac(n-1)
Second, iterate over as many terms as you like (in this case 10). We're only interested in the uneven exponents, hence the upper bound is 20. Furthermore, every other "term" has to be multiplied by -1, thus two commands, and an increment of 4:
plot for [a=3:21:4] -1*x**a/fac(a), for [a=1:21:4] x**a/fac(a)
This will give you a plot like this:
Related
Normally the xrange and yrange are adjusted to fit the largest and smallest value in the datafile, when plotting according to
plot "my_file.dat" u 1:2
Suppose I want to apply a cutoff to the y-values, so that I only include the points whose y-value is larger than, say 1. If I do
plot "my_file.dat" u 1:($2>1?$2:NaN)
I indeed select only those points. The problem, however, is that the xrange is still adjusted to the range of column 1 of the entire datafile, rather than to only those points which are actually plotted. How can I fix this? My gnuplot version is 5.2.
You also have to filter the undesired x-values in a similar way:
plot 'my_file.dat' u ($2 > 1 ? $1 : NaN):($2 > 1 ? $2: NaN)
If you want to draw a line between these points, a different way of filtering is necessary: see Plotting lines with missing datapoints for multidimensional data
So I have some data files in format
x y ymin ymax
That I'm plotting with yerrorbars.
Now how would I best add a median of the y values to the plot running over the whole range of x?
UPDATE
I'm also plotting the x axis in logscale which seems to prevent using STATS.
Suppose that your data looks like this:
1 8 6 9
2 6 5 7
3 5 4 8
4 6 5 8
We can use the stats command to find the median. The use is similar to the plot command. Here, we only need to do analysis of the second column, so we will only specify the second column:
stats datafile u 2 nooutput
The nooutput option tells the command not to print the results. If we wish to see the full analysis, we simply omit that specification. By default, the stats command stores its results in variables of the form STATS_*. We can use a different prefix if desired. See help stats for more details.
At this point, we have a variable STATS_median that stores the median of the y values (which is 6 for the sample data). We can now add the median to the graph in one of two ways. First we can simply add a plot specification to the existing plot command:
plot datafile u 1:2:3:4 with yerrorbars, STATS_median
or we can add a line with the set arrow command and then plot just the yerrorbars:
set arrow 1 from graph 0, first STATS_median to graph 1, first STATS_median nohead
plot datafile u 1:2:3:4 with yerrorbars
Here we give the x coordinate in graph units which range from 0 (the left side) to 1 (the right side) and the y coordinate in the first coordinate system which corresponds to the y1 axis. Specifying nohead says to not draw an arrow head. The 1 immediately following set arrow tags this arrow as arrow 1 so that we can change or remove it easily later.
Other options are available. See help arrow for more details.
I've had a look at trying to plot some data with Gnuplot and super impose a function on it however I have found no information after a couple of hours research.
Say you have typical datapoints:
x y
0 1
1 5
2 6
3 6
...
and now you want to super impose a extrapolation taking y down to 0 for some gradient (eg. a gradient of 1 over 3 x steps, then your straight line function is (0 - 6)/(3)*x + 6 = -2*x + 6)
I am under the assumption you can just plot this via a parametric function,
eg:
y(x) = 2*x + 6
plot 'datafile.dat' using 1:2, \
y(x) [3:6]
Is this the right approach? I've tried but it doesn't plot correctly.
Also my data is set xdata time and I'm doing a multiplot, (however I only need this on one of them) which complicates things. (I've also tried it with set parametric)
Any ideas?
you can generate a piece-wise function like this:
y(x) = (x>3 && x<6) ? 2*x + 6 : 0/1
plot [0:6] 'datafile.dat' using 1:2, y(x)
but it will draw a vertical line at x=3.
the best attempt maybe to store the function in a file:
set table 'func.dat'
plot [3:6] y(x)
unset table
and then plot both data and function:
plot [0:6] 'datafile.dat' using 1:2, 'func.dat' w l
My data file is as-
2 3 4 1 5 2 0 3 4 5 3 2 0 3 4 0 5 4 3 2 3 4 4 0 5 3 2 3 4 5 1 3 4
My requirement is to plot normal PDF in gnuplot.
I could do it by calculating f(x)
f(x) = \frac{1}{\sqrt{2\pi\sigma^2} } e^{ -\frac{(x-\mu)^2}{2\sigma^2} }
for each x using shell script.
Then I plot it in gnuplot using the command-
plot 'ifile.txt' using 1:2 with lines
But whether is it possible to plot directly in gnuplot?
gnuplot provides a number of processing options under the smooth keyword (try typing help smooth for more info). For your specific case, I would recommend a fit though.
First, note that your data points are in a row, you need to convert it to columns for gnuplot to use it. You can do it with awk:
awk '{for (i=1;i<=NF;i++) print $i}' datafile
which can be invoked from within gnuplot:
plot "< awk '{for (i=1;i<=NF;i++) print $i}' datafile" ...
Now assume that datafile has the right format for simplicity.
You can use the smooth frequency option to see how many occurrences of each value you have:
plot "datafile" u 1:(1.) smooth frequency w lp pt 7
To get the normalized distribution, you divide by the number of values. This can be done automatically within gnuplot with stats:
stats "datafile"
This will store the number of values in variable STATS_records, which in you case has value 33:
gnuplot> print STATS_records
33.0
So the normalized distribution (the probability of getting a value at x) is:
plot "datafile" u 1:(1./STATS_records) smooth frequency w lp pt 7
As you can see, your distribution doesn't really look like a normal distribution, but anyway, let's go on. Create a Gaussian for fitting and fit to your data, and plot it. You need to fit to the probability, rather than to the data itself. To do so, we plot to a table to extract the data generated by smooth frequency:
# Non-normalized Gaussian
f(x)= A * exp(-(x-x0)**2/2./sigma**2)
# Save probability data to table
set table "probability"
plot "datafile" u 1:(1./STATS_records) smooth frequency not
unset table
# Fit the Gaussian to the data, exclude points from table with grep
fit f(x) "< grep -v 'u' probability" via x0, sigma, A
# Normalize the gaussian
g(x) = 1./sqrt(2.*pi*sigma**2) * f(x) / A
# Plot
plot "datafile" u 1:(1./STATS_records) smooth frequency w lp pt 7, g(x)
set table generates some points which you should exclude, that's why I used grep to filter the file. Also, the Gaussian needs to be normalized after the fitting is done with a variable amplitude. If you want to retrieve the fitting parameters:
gnuplot> print x0, sigma
3.40584703189268 1.76237558717934
Finally note that if the spacing between data points is not homogeneous, e.g. instead of x = 0, 1, 2, 3 ... you have values at x = 0, 0.1, 0.5, 3, 3.2 ... then you'll need to use a different way to do this, for example defining bins of regular size to group data points.
This is my first time trying to use gnuplot, and I can't find any instructions on how to accomplish this. The closest I found was this:
http://gnuplot.sourceforge.net/docs_4.2/node259.html
plot 'file.dat' using 1:2:3:4 with vectors head filled lt 2
but I can't find any explanation about "file.dat".
So can somebody give a simple example of how to draw a simple 2d vector arrow? Thanks.
gnuplot has a very good help/documentation build in. Just type help plot or help vector to learn more on how to plot vectors in gnuplot.
The 2D vectors style draws a vector from (x,y) to (x+xdelta,y+ydelta).
A small arrowhead is drawn at the end of each vector.
4 columns: x y xdelta ydelta
This means, your input file should have 4 columns, where the first two columns define the starting (x,y)-position of the vector/arrow and the last two its (x,y) direction:
# file.dat
0 0 .5 .5
0 1 -.5 .5
1 1 1 0
Now typing the following command
plot "file.dat" using 1:2:3:4 with vectors filled head lw 3
gives the following output:
Drawing vectors with the set arrow command
Consider using the set arrow command, if you only need to draw a few vectors/arrows (e.g. to highlight some points in the plot).
set arrow 1 from pi/2,1 to pi/2,0
set arrow 2 from pi*3/2,-1 to pi*3/2,0
plot[0:2*pi] sin(x)
You can create 'file.dat' in a spreadsheet save it as text and put it in the path of gnuplot by using the cd command to point gnuplot to its location. If that does not agree with you, look at the examples using '+' and '++' and '-' in the gnuplot manual. These are a "virtual data file." Note that the first two are for one and two column data points i.e. (x) or (x,y). You will have to use $1 and $2 as variables for calculating dx and dy. It is obligatory to set the xrange and yrange variables and the isosamples for density for this to work.
Something like....
set isosamples 30
set samples 30
set xrange [-10:10]
set yrange [-10:10]
plot '++' using 1:2:(0.1*sin($1)):(0.1*cos($2)) with vectors