Trying to setting a range for abline in R - r

When I use the abline in R ,
plot(NULL,xlim=c(0,50),ylim=c(-30,0))
abline(h=-15,col='black')
how do I set the range of x axis to a certain range?
(like a horizontal line at y=-15,but end with x =50) (not using xlim)

You can use the lines() function, taking an x and y coordinate of start/end point. This example has starting point (0|-15) and end point (50|-15):
lines(x=c(0,50), y=c(-15,-15))

Related

How to plot data over a non-rectangular region in Octave?

I have three arrays of equal size: x, y, z. I want to plot z over x, y. Problems is, those x and y do not represent a rectangular region, such as what would be in case of using meshgrid function.
I know I can use something like scatter, but that would graphically only give me the points themselves. What I want is the filled, smoothed picture. So as opposed to this created by scatter:
I would like something like this:
Any suggestion how this can be done? I have a feeling the data must be smoothed out somehow via interpolation or something else prior to plotting which itself should be simple.
You can use griddata() to interpolate your x,y data on a regular grid and then you can use imagesc() to plot the result.
Here is a minimal example with a basic circle:
% INPUT
x = cos(0:0.1:2*pi);
y = sin(0:0.1:2*pi);
z = (0:0.1:2*pi);
% Create a regular grid that have the same boundary as your x,y data
[xx,yy] = meshgrid(linspace(-1,1,100),linspace(-1,1,100));
% Grid interpolation
zz = griddata (x, y, z, xx, yy);
% Plot
imagesc(zz)
colormap ([jet(); 1 1 1]); % I add a last [1 1 1] triplet to set the NaN color to white.
Noticed that this will only works if you keep the default interpolation method (which is a linear interpolation). The other method (cubic and nearest) will extend the domain of definition by analytic continuation.
I realized that the best approach would be some slight modification to what obchardon is proposing:
instead of the lines
imagesc(zz)
colormap ([jet(); 1 1 1]);
do this:
surf(xx, yy, zz);
shading interp;
colormap("jet");
This eliminates the problem with the black background. Then all it takes is just to rotate the camera with a mouse so that the 3d surface looked like 2d from above.

How to plot 3D in octave

Hi I'm starting to use Octave and need help on how to plot x²+y² = 1.
I know that the figure is a cylinder.
I tried:
x= -10:0.1:10;
y = -10:0.1:10;
t = x²+y²;
but it won't work.
For this particular situation, you can just use the cylinder function.
cylinder([a,b])
will plot a cylinder whose radius at z==0 will be equal to a, and will vary continuously and smoothly until its radius at z==1 reaches b. In you case, you need to set a and b to 1, which is what happens by default when you call cylinder().
Now this will plot the cylinder with only z values in [0,1]. If you want to customize that range, you can just get the output from the function like this:
[xx yy zz]=cylinder([1,1]);
And now you can use this to obtain the plot that you want. For example,
surf(xx,yy,zz.*10);hold on; surf(xx,yy,-zz.*10);
will produce this:

How to create exponential graph

How can I make an x-axis that doubles for every increment? I want equal distances between 0, 128, 256, 512, 1024 and 2048. How can I do that?
I'm trying to plot points from a benchmark where I measured time and doubled the memory size every increment.
You can cheat and plot with a linear axis, like from 1 up to as many numbers as you desire, then change the labels when you're done. You can use the 'xtick' property to set what horizontal tick values on your graph remain and the 'xticklabel' property to change the labels to your desired values.
labels = [0 128 256 512 1024 2048]; % Provide your labels here
x = 1 : numel(labels);
y = rand(1, numel(x)); % Insert your data here
plot(x, y, 'b.'); % Plot your data
set(gca, 'xtick', x); % Change the x-axis so only the right amount of ticks remain
set(gca, 'xticklabel', labels) % Change the labels to the desired ones
I get the following graph. Note that the data I'm plotting is completely random as I don't have your data but I want to demonstrate what the changed plot looks like:
For more properties that you can change on your graph, see the Axes Properties page on the Octave docs.
With apologies to Rayryeng, since I'm essentially proposing the same method at heart, but I felt it was missing important info, such as how to convert the axis itself to equally spaced intervals in the first place, without messing with the data. So here's a complete solution for example data X vs Y, producing the equivalent of semilogx for base 2.
Y = 1 : 10;
X = 2 .^ Y;
XTicks = log2(X);
XTickLabels = {};
for XTick = XTicks
XTickLabels{end+1} = sprintf('2^{%d}', XTick);
end
plot (log2 (X), Y);
set(gca, 'xtick', XTicks, 'xticklabel', XTickLabels);
Note that if you plan to 'superimpose' another plot on top of this, you'll have to take into account that the actual values in the X axis are essentially "1, 2, 3, ... 10", so either "log-ify" the new plot's X-axis values too, before superimposing via hold on, or plot onto another, independent set of axes entirely and place them in the same position.
Note: I have assumed that you're after a base-2 logarithmic x-axis. If you do actually want the 0-128 interval to be the same as the 128-256 interval, then modify as per Rayrengs answer --- or even better, use a more appropriate graph, like a bar graph! (i.e. with the 'powers-of-two' used purely as descriptive labels for each column)

Creating a 2-D plot using three parameters in IDL

I am able to create a 2-D plot using two parameters in IDL, i.e., star formation rate (y-axis) vs. time (x-axis).
But I would like to include the redshift (another variable) corresponding to each data point, say, as the top x-axis. It didn't work when I tried adding the third variable to PLOT procedure, and I have not been able to find any discussion on how to accomplish this online. Any help is appreciated.
First run PLOT.PRO with the NODATA keyword set and XAXIS=4 and YAXIS=4 to suppress each axis. Then you can use the AXIS.PRO program to define each axis. Then you can use OPLOT.PRO to draw the points of Z vs. X and Z vs. Y, where Z = star formation rate, X = time, and Y = redshift. Look up details on the [XYZ]AXIS keywords to determine which axis to draw at each time. You can even color each axis using the COLOR keyword with the AXIS.PRO program.
The only trick is that you will have to scale the Y data points to the X-axis scale prior to plotting because you will explicitly define the [XYZ]RANGE when calling PLOT.PRO (well you could do the converse and scale it to Y and redefine X, it's your choice). You need to do this scaling because OPLOT.PRO and, say, PLOTS.PRO use the original [XYZ]RANGE defined when calling PLOT.PRO to convert device coordinates to data coordinates.
Does that make sense?
first call PLOT, TIME, SFR with XSTYLE=9 to force exact range and suppress the top x-axis
then use the AXIS procedure to create the top x-axis
be careful with the ticks of that axis, which you want to correspond to a REDSHIFT that you compute from the TIME variable
example with a bottom x-axis in velocity and a top y-axis in frequency:
> plot, vel, spec, xsty=9, xtick_get=xtick, xtit='Velocity (km/s)', ytit='Antenna Temperature (K)'
> axis, !x.crange[0], !y.crange[1], xaxis=1, xtickv=((ref_freq - ref_freq/299792.458*xtick)), xtickformat='(F8.3)', xticks=n_elements(xtick)-1, xrange=(ref_freq - ref_freq/299792.458*minmax(!x.crange)), chars=1.5
You could always set the color to be the third dimension (ie. color or size).

How can i plot a single point using maxima/wxmaxima?

Im writing a basic script to find the min distance between f(x):=log(x)-x and the origin. I would like to be able to plot the point closest to the origin over the top of a plot of f(x), but i can not figure out how to plot a single point.
Here is what i have written. Any ideas?
f(x):=log(x)-x;
d(x):=sqrt(x^2+f(x)^2)$
find_root(diff(d(x),x),x,0.01,5)$
a:%;
f(a);
print("min distance from f(x) to (0,0)")$
d(a);
print("passes second derivative test if next value greater than zero")$
g(x):=''(diff(d(x),x,2))$
g(a);
wxplot2d([f(x)], [x,.01,5], [y,-6,0])$
Use the discrete option as a second curve, and then use points in the style option.
Replacing your last line with
wxplot2d([f(x), [discrete, [a], [f(a)]]], [x,.01,5], [y,-6,0],
[style, lines, points],
[legend, "log(x)-x", "closest point to origin"],
[point_type, circle],
[gnuplot_preamble, "set key bottom"])$
gives you this:

Resources