I want to plot two functions in different ranges in Gnuplot. As an example, I want to plot f(x) for xrange [0:0.5] and g(x) for xrange [0.5:1], both in a same graph.
What should I do for this?
You have at least two different solutions :
1) create a "heavyside" function :
f(x) = ... define your first function
g(x) = ... define your second function
h(x) = (x<0.5)?f(x):g(x)
plot h(x)
2) if you need some control on the color of each function, you could do
plot (x<0.5?f(x):1/0) lc 1, (x>0.5?g(x):1/0) lc 2
Related
I have a list of positive and negative values and a single temperature. I am trying to plot the Maxwell-Boltzmann Distribution using the equation for particles moving in only one direction.
m_e = 9.11E-28 # electron mass [g]
k = 1.38E-16 # boltzmann constant [erg*K^-1]
v = range(1e10, -1e10, step=-1e8) # velocity [cm/s]
T_M = 1e6 # temperature of Maxwellian [K]
function Maxwellian(v_Max, T_Max)
normal = (m_e/(2*pi*k*T_Max))^1.5
exp_term = exp(-((m_e).*v_Max.*v_Max)/(3*k*T_Max))
return normal*exp_term
end
# Initially comparing chosen distribution f_s to Maxwellian F_s
plot(v, Maxwellian.(v, T_M), label= L"F_s" * " (Maxwellian)")
xlabel!("velocity (cm/s)")
ylabel!("probability density")
However, when, plotting this, my whole function is 0:
I tested out if I wrote my function correctly by replacing return normal*exp_term with return exp_term (i.e. ignoring any normalization constants) and this seems to produce the distinct of the bell curve:
Yet, without the normalization constant, this will not preserve the area under the curve. I was wondering what may I be doing incorrectly with setting up my Maxwellian function and the constant in front of the exponential.
If you print the normalization term on its own:
julia> (m_e/(2*pi*k*T_M))^1.5
1.0769341115495682e-27
you can see that it is 10 orders of magnitude smaller than the Y-axis scale used for the plot. You can set the Y-axis limits during the plots with ylims argument, or after the plot with:
julia> ylims!(-1e-28, 2e-27)
which changes the plot to:
I would like to draw a curve logW= a+b*logP, where a = 12, b = -0.8
R = -0.4 of this curve. I would like to use data and look how this curve fits to data. Can I do it in gnuplot?
log(f(x)) = a+b*log(P)
a = 12, b = -0.8
fit f(x) 'data.txt' u 1:2 w p via a,b
plot f(x) 'data.txt' u 1:2
Does it sounds ok?
You are close, but there are little problems everywhere. Let's try to clean it up.
1) You must define f(x) itself, not log(f(x)). It is not clear from your statement, but I assume 'P' is the independent variable x?
f(P) = exp(a + b*log(P))
2) gnuplot commands are separated by semicolons, not commas
a = 12; b = -0.8
3) 'fit' is not a plotting style, so "with points" makes no sense as part of a fit command
fit f(x) 'data.txt' using 1:2 via a,b
4) The 'plot' command has two separate pieces: the data and the curve fit to it
plot 'data.txt' using 1:2 with points, f(x) with lines
I'm trying to plot a piece wise defined function give by,
equation
Implemented in gnuplot, like this,
h(x)=0
g(x)=(4/3)*(1-x**3)
plot h(x)*(x<0) + g(x)*(x>0)*(x<1) + h(x)*(x>1)
The problem is that the line that goes from h(x) to g(x) is not a vertical line, is a inclined one. I really need a vertical one.
How to fix it?
plot
You have to increase the sampling rate, for example by
set samples 1000
Also note that you might have to change the definition of the function g(x) to
g(x)=(4./3.)*(1-x**3)
because you otherwise evaluate (4/3), which is equal to 1 rather than 1.333...
You mustn't have (mathematically speaking) a connecting line between the noncontinous parts, because that implies that there are actual function values there.
You should do this instead:
plot sample [:0] h(x) lc 1 title "f(x)", \
[0:1] g(x) lc 1 notitle, \
[1:] h(x) lc 1 notitle
I would like to plot lines with colors representing the intensity of some measurment in the form of a vector. With scatter you can plot points with the color given by a color vector but in plot I would have to give an RGB vector. I would like to know how can I map my vector to an RGB vector? Or is there some other method?
Thanks a lot, and tell me if I didn't give some information
Depending of the number of lines you have to plot, one solution would be to use the built-in colormaps. Crude example:
intensityVector = [0.1 0.5 1]; % some intensities
% Data you want to plot
x=1:0.1:16;
data1 = zeros(1,length(x));
data2 = sin(x);
data3 = cos(x);
% Let's get an appropriate colormap
cm = colormap(hot);
close; % because colormap call opens a figure
% In order for the line to be differentiable, let's add an offset
offset=10;
figure()
hold on
plot(data1,'Color',cm(1*offset,:),'LineWidth',3);
plot(data2,'Color',cm(2*offset,:),'LineWidth',3);
plot(data3,'Color',cm(3*offset,:),'LineWidth',3);
legend({'data1','data2','data3'});
Is there any way to specify a yrange using the graph position specifier?
I want to plot a function inside a specific region of the canvas. I have absolute values for the xrange, but for the yrange I want it to go from, for example, y>graph 0.7 and y<graph 0.8, i.e, I want the y values of the function vary from 70% till 80% of the values of the actual yrange. More specifically, I want something like this:
plot (x<x_min || x>x_max) || (y<graph 0.7 || y>graph 0.8) ? 1/0 : the_function(x)
Well, I tried to do this, but it didn't work: gnuplot returns an error. I also searched for a solution, but had no success.
Do you have any idea of how to do this?
You cannot directly use graph for coordinates used inside plot. This works only for labels, objects and arrows. Gnuplot evaluates the function on the whole x-range and then determines the automatic y-range. So you cannot have access to an automatically computed y-range at the time the function is evaluated.
Here is how you can achieve that with other means.
First you plot the complete function using the unknown terminal. This determines the y-range which would have been used for the complete function. The values are stored in the variables GPVAL_Y_MIN and GPVAL_Y_MAX. Now you can replot the function using these values as constraints:
set terminal unknown
f(x) = sin(x)
set xrange [0:10]
plot f(x)
set terminal pngcairo size 600,300 lw 2
unset key
set output 'test.png'
set samples 1000
graph_y = GPVAL_Y_MAX - GPVAL_Y_MIN
y0 = GPVAL_Y_MIN
set yrange[y0 : y0 + graph_y]
plot (y = f(x), (y < y0 + 0.7*graph_y || y > y0 + 0.8*graph_y) ? 1/0 : y)
1/0 only works with data points. You can use the special data file "+" for this purpose, which in the end is the same as plotting a function, where gnuplot is sampling the function at discrete intervals:
set xrange [-2*pi:2*pi]
plot "+" u ($1):($1 > -pi && $1 < pi ? sin($1) : 1/0) w l