gnuplot will plot a data file with vectors.
I would like to set something like isosamples=40 and
have gradient vectors plotted for a 2D function.
I know I could write a python program to generate
the data file for the vectors but
I would prefer to do the entire operation within gnuplot.
Any advice?
Would this be a worthy improvement to gnuplot if not yet implemented?
You can use the vectors style and do the computations in gnuplot using the special filename ++.
Suppose that I wish to graph a direction field for the differential equation y'=y*sin(x). I could do this with the following:
set xrange[-2:2]
set yrange[-2:2]
set samples 30
set isosamples 30
unset key
f(x,y) = y*sin(x)
lf(x,y) = sqrt(1+f(x,y)**2)
lyf(x,y) = f(x,y)/lf(x,y)
plot '++' u 1:2:(0.1/lf($1,$2)):(0.1*lyf($1,$2)) with vectors
All of my calculations are done in gnuplot. I compute the direction of the vectors, and scale them to have a length of 0.1, all in the plot command.
I use the special filename '++' which generates a set of points equally spaced over the x and y plot ranges.
See 'help special-filenames' for more details on that.
Related
is there a way to avoid the drawing of the near asymptote line in the function 1/(2-x), for example, without usage of conditional plotting? The idea is to draw iterated functions based in this one and, since asymptote changes, using conditional plotting isn't a good solution.
You can plot with points at a very high sampling rate:
set yrange [-10:10]
set samples 100000
plot 1/(2-x) with points
If the singularity occurs at different values of x you can use conditional plotting on y:
f(x)=1/(2-x)
set samples 1000
plot (abs(f(x)) < 10 ? f(x) : 1/0) with lines
I have been wondering about this for a while, and it might already be implemented in gnuplot but I haven't been able to find info online.
When you have a data file, it is possible to exchange the axes and assign the "dummy variable", say x, (in gnuplot's help terminology) to the vertical axis:
plot "data" u 1:2 # x goes to horizontal axis, standard
plot "data" u 2:1 # x goes to vertical axis, exchanged axes
However, when you have a function, you need to resort to a parametric function to do this. Imagine you want to plot x = y² (as opposite to y = x²), then (as far as I know) you need to do:
set parametric
plot t**2,t
which works nicely in this case. I think however that a more flexible approach would be desirable, something like
plot x**2 axes y1x1 # this doesn't work!
Is something like the above implemented, or is there an easy way to use y as dummy variable without the need to set parametric?
So here is another ugly, but gnuplot-only variant: Use the special filename '+' to generate a dynamic data set for plotting:
plot '+' using ($1**2):1
The development version contains a new feature, which allows you to use dummy variables instead of column numbers for plotting with '+':
plot sample [y=-10:10] '+' using (y**2):(y)
I guess that's what come closest to your request.
From what I have seen, parametric plots are pretty common in order to achieve your needs.
If you really hate parametric plots and you have no fear for a VERY ugly solutions, I can give you my method...
My trick is to use a data file filled with a sequence of numbers. To fit your example, let's make a file sq with a sequence of reals from -10 to 10 :
seq -10 .5 10 > sq
And then you can do the magic you want using gnuplot :
plot 'sq' u ($1**2):($1)
And if you uses linux you can also put the command directly in the command line :
plot '< seq -10 .5 10' u ($1**2):($1)
I want to add that I'm not proud of this solution and I'd love the "axis y1x1" functionality too.
As far as I know there is no way to simply invert or exchange the axes in gnuplot when plotting a function.
The reason comes from the way functions are plotted in the normal plotting mode. There is a set of points at even intervals along the x axis which are sampled (frequency set by set samples) and the function value computed. This only allows for well-behaved functions; one y-value per x-value.
I have two whitespace separated files that each partially contain the 4D data I need to plot. Call the data points (x,y,z,w). x, y are in both files, but the z and w are split up. I'm not sure how to pull the data properly, and define the proper palette.
I would normally use:
splot "Data1.dat" using 1:2:3 \
"Data2.dat" using 1:2:3
If I were trying to simultaneously plot 2 surfaces. But I'm trying to plot a 4D surface, where the dimension data is split between files.
Ideally, I'd use the HSV color mode, with a fixed saturation, where H is a function of Z, and V is a function of W. I don't understand the palette functions documentation, where each R,G,B is a function of the variable "Gray".
The simplest solution I can think of, would be to make use of the possibility to use command-line tools in gnuplot, something like
splot "<paste Data{1,2}.dat" using 1:2:3:6
should do the trick.
Note: The ordering of x and y should match!
I'm an happy user of the with vectors options in Gnuplot. However, in most case, the vectors need to be rescaled to make a nice figure.
Currently, I am doing this by
plot "data" using 1:2:($3*scale):($4*scale) with vectors
Ideally, I would just like to skip using and writing the scale for vector-length manually once, such that I could use
plot "data" with vectors scale 0.1
Maybe there is an auto-scaling option that I am unaware of?
There's no shortcut for that as far as I know. The best you'll probably do is to use a macro:
set macros
with_vectors = "using 1:2:($3*scale):($4*scale) w vectors"
scale = 0.1
plot "data" #with_vectors
scale = 0.2
plot "data" #with_vectors
I would like to draw a straight line that makes the average of a curve. I am plotting my data like that:
plot 'dataset' u 2:4 w p smooth bezier
My data consists of multiple columns and I would get something like that:
Any ideas of how to do it? I guess it is more an interpolation than an average. It is not relevant the ups and downs of the curve, and it would be much better to have a straight line interpolating the curve...
Using a straight line could be more or less easy to fit using fit however, how could I fit a curve that does not look like a well know curve? Let me show you an example? How could I fit a smooth curve among the main group of points? Please notice that there is some noise on the lower part of the graph that I wouldn't like to represent.
If you want to do some basic statistics on your data, gnuplot has a builtin command stats which may do what you want. Gnuplot offers some internal variables after plotting that contain data about min, max, etc. To see what these are, type show variables all after plotting your data.
Otherwise if you want to fit your data to a line, gnuplot does that as well:
f(x) = a*x + b
fit f(x) 'data.dat' using 2:4 via a,b
plot 'data.dat' using 2:4, f(x)