I have 3d data that makes a surface when plotted in 3d:
x1 y1 f(x1, y1)
x1 y2 f(x1, y2)
.. .. ..
x1 yn f(x1, yn)
x2 y1 f(x1, y1)
x2 y2 f(x1, y2)
.. .. ..
x2 yn f(x1, yn)
.
.
I can use gnuplot to get level curve for x=constant by using :
plot "datafile.txt" u 2:3 index 0 //or index 1 etc
How can I get plot for y=constant ?
Thanks for reply in advance...
1st possibility: plot the whole file, and make all unwanted datapoints invalid (NaN or 1/0)
yfix = 5 # the constant y value
plot datafile using (($2 == yfix) ? $1 : NaN):3
2nd possibility: the every specifier
n = 3 # number of the wanted sample in y-direction
plot datafile every ::n::n using 1:3
It takes as option (line increment):(block increment):(first line):(first block):(last line):(last block)
So the command above plots only the n'th line from each block, resp. dataset.
(note: blocks are separated by a single blank line, datasets by an additional second blank line. The latter are specified with index, the former addressed via every)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I tried to do my homework and i'm not sure if I did it right. I'm not entirely sure how linspace works and was hoping for some input on my code! I'll attach the assignment it's based on. [enter link description here][1]
[enter image description here][2]
clc
clear all
figure(1);
figure(1);
x = 0:0.1:10
y1 = exp(-0.5*x).*sin(2*x)
y2 = exp(-0.5*x).*cos(2*x)
plot(x,y1,'-b',x,y2,'--r')
legend('y1','y2');
grid on
xlabel('\bfx axis');
ylabel('\bfy axis');
title('Sin and Cos Functions')
figure(2);
subplot(2,2,1)
plot(x,y1,'-b');
legend('y1','Location','northeast');
grid on
xlabel('\bfx')
ylabel('\bfy1')
title('Subplot of x and y1')
figure(3)
subplot(2,2,2)
plot(x,y2,'--r');
legend('y2','Location','northeast');
grid on
xlabel('\bfx')
ylabel('\bfy2')
title('Subplot of x and y2')
figure(4)
x = linspace(-2,8,200);
fun=(x.^2-6*x+5)./(x-3)
plot(x,fun,'b-','LineWidth',2)
axis([-2 8 -10 10]);
hold on;
title('Plot of f(x)')
xlabel('\bfx')
ylabel('\bfy')
(50 Points) Plot the function y(x) = (e^-0.5x)(Sin2x) for 100 values of x between 0 and 10. Use solid blue line for this function. Then plot the function y(x) = (e^-0.5x)(Cos2x)on the same axis. Use dashed red line for this function. Be sure to include a legend, title, axis labels and grid on the plots. Re-graph the two functions using subplot.
(50 Points) Plot the function Plot the function f(x)= ((X^2)-6x-5)/(x-3) using 200 points over the range −2 ≤ x ≤ 8. Note that there is an asymptote at x = 3, so the function will tent to infinity near to that point. In order to see the rest of the plot properly, you will need to limit the y-axis to the range -10 to 10.
linspace takes 3 arguments: a starting value, an ending value, and n = the number of points you want to generate in between those two numbers. It outputs an array of n evenly spaced numbers between your starting and ending values. For example linspace(0, 10, 5) returns an array of 5 evenly spaced numbers starting with 0 and ending with 10.
Here's the code for figure 1
x = linspace(0, 10, 100);
y1 = exp(-0.5*x).*sin(2*x);
y2 = exp(-0.5*x).*cos(2*x);
figure(1)
plot(x, y1, '-b', x, y2, '--r')
title('This is the title')
legend('This is Y1', 'This is Y2')
xlabel('These are the X values')
ylabel('These are the Y values')
grid on
Figure 2. Both subplots can be under one figure handle (i.e. figure(2)).
figure(2)
subplot(2, 1, 1)
plot(x, y1, '-b')
title('This is the first subplot')
xlabel('X axis')
ylabel('Y axis')
grid on
subplot(2, 1, 2)
plot(x, y2, '--r')
title('This is the second subplot')
xlabel('Another X axis')
ylabel('Another Y axis')
grid on
Figure 3. Your code looks right. Here's a similar solution:
x2 = linspace(-2, 8, 200);
y3 = ((x2.^2)-6.*x2-5)./(x2-3);
figure(3)
plot(x2, y3, '-g')
title('The third figure')
grid on
ylim([-10 10])
xlabel('Another axis')
ylabel('The last axis')
I have a set of x, y, t in a text file sect.txt. First line represents x1, y1 , t1 and second is x2, y2, t1... the 4th line(3rd line is blank) is x3, y3, t2, followed by x4, y4, t3. I want to plot a line which has coordinates x1, y1 and x2, y2, with thickness t1. Similary next line has coords x3, y3 and x4, y4 with thickness t2 and so on, for n times. With the code below, I am able to plot the 2d lines in gnuplot, but unable to incorporate thickness. Could anyone make any suggestions?
0.0000000000000000 5.0000000000000000 5
1084.0000000000000 5.0000000000000000 5
542.00000000000000 10.000000000000000 1
542.00000000000000 981.00000000000000 1
0.0000000000000000 990.50000000000000 2
1084.0000000000000 990.50000000000000 2
392.00000000000000 1017.5000000000000 4.5
692.00000000000000 1017.5000000000000 4.5
392.00000000000000 1682.5000000000000 3
692.00000000000000 1682.5000000000000 3
542.00000000000000 1035.0000000000000 2
542.00000000000000 1665.0000000000000 2
code
set xlabel "X Axis (Major) "
set ylabel"Y Axis (Minor) "
coord="sect.txt"
set timestamp
set autoscale keepfix
set view equal xy
plot coord using 1:2 with linespoints
As far as I known, lw does not support the variable keyword as does line color lc. However, one could do this with a small "trick". The idea is to first determine the number of lines in the input file and then do first an auxiliary plot (per line via the every keyword) which loads the line widths into an array. This array is in turn used in the "real" plot:
fName = 'test.dat'
stat fName nooutput
#number of lines
M = int(STATS_records/2)
array lineWidths[M]
plot \
for [i=0:M-1] fName every :::i::i u 1:(lineWidths[i+1]=$3,1/0) t '', \
for [i=0:M-1] fName every :::i::i u 1:2 w l lw lineWidths[i+1] t sprintf("line %d", i+1)
For your input, this yields:
I want to plot a right triangle with hypothenuse parallel to the slope of a curve in loglog scale. However, gnuplot behaves funky:
set terminal postscript eps enhanced "Helvetica" 22
a1 = 64459.7;
a2 = -1.90748;
func(x) = a1*x**a2
X1 = 1e+4;
Y1 = 1e-4;
X2 = 2e+4;
Y2 = Y1 + func(X1) - func(X2)
set logscale xy
set format x "10^{%T}"
set format y "10^{%T}"
set key top right
set output 'temp.eps'
set object 1 poly from X1,Y1 to X2,Y1 to X1,Y2 to X1,Y1 fs empty border 1
set xrange [0.8e+4:1e+5]
plot func(x) title sprintf("a line with slope %1.2f",a2)
unset object 1
and outputs
Perhaps in log-log scale the command set object behaves differently when in log-log scale, otherwise i have no idea...
the right answer is:
Y2 = Y1 * 10**(a2 * log10(X1 / X2) );
p.s. moderators, feel free to delete the question
I've made a 3d surface plot in gnuplot, and I'm wondering if it's possible to plot an unrelated 2d line graph on the 'wall' of the y,z axis.
So in essence, I take my x,y line plot but paste it on to the y,z axis.
Similar to a contour plot on the x,y, under a 3d surface, but different.
This may not be precisely what you are looking for, but it is a way to plot a function on a 'wall' of your splot box:
#!/usr/bin/env gnuplot
set term png
set output 'test.png'
# this is the function you want on the wall
myfun(x,y) = y**2
# set the ranges manually
xmin = -10
xmax = 10
ymin = -10
ymax = 10
x_range = xmax - xmin
y_range = ymax - ymin
scaler = 0.001
x1 = xmin - x_range*scaler
x2 = xmin + x_range*scaler
x3 = xmax - x_range*scaler
x4 = xmax + x_range*scaler
y1 = ymin - y_range*scaler
y2 = ymin + y_range*scaler
y3 = ymax - y_range*scaler
y4 = ymax + y_range*scaler
xminwall(x,y) = (x > x1 && x < x2) ? myfun(x,y) : 1/0
xmaxwall(x,y) = (x > x3 && x < x4) ? myfun(x,y) : 1/0
yminwall(x,y) = (y > x1 && y < x2) ? myfun(x,y) : 1/0
ymaxwall(x,y) = (y > x3 && y < x4) ? myfun(x,y) : 1/0
splot sin(x), xminwall(x,y)
Here is the result:
What the script does is plots the function you want to plot on the wall (myfun(x,y)) and does a splot of it, restricted to values that are very close to the wall. The surface mesh has a certain number of gridpoints, and one of those points is always at the x and y limits.
In this example, if I had just done splot sin(x), y**2, I would have gotten two overlapping surfaces.
I made four functions (xminwall, etc.) so you can plot on the wall at the x/y range minimum/maximum walls. Note that mufun(x,y) has to be a function of the correct x and y in order for the plot to show up properly on the wall, otherwise you might get a straight line.
I'm intersecting a line in 2D and I calculate the X,Y coordinates of the intersection point. What I need is the Z of the intersection point given the X,Y,Z of the line points, and the X,Y of the intersection. From what I understand of equations it should be a one-liner but I don't know enough math to get there.
Your question is rather vague, but I will try to answer.
So take following equation:
Let's note it as Fx(X) = Fy(Y) = Fz(Z) and take a part of it:
Fx(X) = Fz(Z)
Then you said you know x, y and z for two points, put it to x1, x2, z1, z2 accordingly. Then put x of intersection to x. Now you have a linear equation with one variable z. Here it is:
z = (x - x1) / (x2 - x1) * (z2 - z1) + z1