how to set octave x-axis limitation and interval - plot

I want to plot a graph in octave in which the x-axis maximum value is 2048, and the they start with 0 and increment by 100.
The y data is a vector of 2049 numbers.
here is my code :
ydata = load ("data.txt");
x = linspace(1,2048,2048);
plot(x,ydata(:,1));
this figures the x-axis with maximum value of 2500.

To add to Silver's answer, you might also want to set the XTick property of the axes:
ydata = rand(2048,1);
plot(ydata(:,1))
xlim([0 2048])
set(gca,'XTick',0:100:2048)
This produces the following, which I think is what you're after (note the axis labels are a bit on top of each other but that's because you wanted them every 100 - changing the aspect ratio of the figure will help):

I think what you are looking for is xlim
xlim([0 2048]);
That will limit the x-axes in the plot between 0 and 2048.
See the documentation here for more info.

Related

Reducing number of datapoints when plotting in loglog scale in Gnuplot

I have a large dataset which I need to plot in loglog scale in Gnuplot, like this:
set log xy
plot 'A_1D_l0.25_L1024_r0.dat' u 1:($2-512)
LogLogPlot of my datapoints
Text file with the datapoints
Datapoints on the x axis are equally spaced, but because of the logscale they get very dense on the right part of the graph, and as a result the output file (I finally export it in .tex) gets very large.
In linear scale, I would simply use the option every to reduce the number of points which get plotted. Is there a similar option for loglogscale, such that the plotted points appear equally spaced?
I am aware of a similar question which was raised a few years ago, but in my opinion the solution is unsatisfactory: plotted points are not equally spaced along the x-axis. I think this is a really unsophisticated problem which deserves a clearer solution.
As I understand it, you don't want to plot the actual data points; you just want to plot a line through them. But you want to keep the appearance of points rather than a line. Is that right?
set log xy
plot 'A_1D_l0.25_L1024_r0.dat' u 1:($2-512) with lines dashtype '.' lw 2
Amended answer
If it is important to present outliers/errors in the data set then you must not use every or any other technique that simply discards or skips most of the data points. In that case I would prefer the plot with points that you show in the original question, perhaps modified to represent each point as a dot rather than a cross. I will simulate this by modifying a single point in your 500000 point data set (first figure below). But I would also suggest that the presence of outliers is even more apparent if you plot with lines (second figure below).
Showing error bounds is another alternative for noisy data, but the options depend on what you have to work with in your data set. If you want to pursue that, please ask a separate question.
If you really want to reduce the number of data to be plotted, you might consider the following script.
s = 0.1 ### sampling interval in log scale
### (try 0.05 for more detail)
c = log10(0.01) ### a parameter used in sampler(x)
### which should be initialized by
### smaller value than any x in log scale
sampler(x) = (x>0 && log10(x)>=c) ? (c=ceil(log10(x)/s+0.5)*s, x) : NaN
set log xy
set grid xtics
plot 'A_1D_l0.25_L1024_r0.dat' using (sampler($1)):($2-512) with points pt 7 lt 1 notitle , \
'A_1D_l0.25_L1024_r0.dat' using 1:($2-512) with lines lt 1 notitle
This script samples the data in increments of roughly 0.1 on x-axis in log scale. It makes use of the property that points whose x value is evaluated as NaN in using are not drawn.

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)

When plotting a curve in R, a piece of the curve gets cut off, not sure why

I am trying to plot this formula. As x approaches 0 from the right, y should be approaching infinity, and so my curve should be going upwards close to y-axis. Instead it gets cut off at y=23 or so.
my_formula = function(x){7.9*x^(-0.5)-1.3}
curve(my_formula,col="red",from=0 ,to=13, xlim=c(0,13),ylim=c(0,50),axes=T, xlab=NA, ylab=NA)
I tried to play with from= parameter, and actually got what I needed when I
put from=-4.8 but I have no idea why this works. in fact x doesn't get less than 0, and from/to should represent the range of x values, Do they? If someone could explain it to me, this would be amazing! Thank you!
By default, curve only chooses 101 x-values within the (from, to) range, set by the default value of the n argument. In your case this means there aren't many values that are close enough to 0 to show the full behaviour of the function. Increasing the number of values that are plotted with something like n=500 helps:
curve(my_formula,col="red",from=0 ,to=13,
xlim=c(0,13),ylim=c(0,50),axes=T, xlab=NA, ylab=NA,
n=500)
This is due mainly to the fact that my_formula(0) is Inf:
So plotting from=0, to=13 in curve means your first 2 values are by default (with 101 points as #Marius notes):
# x
seq(0, 13, length.out=101)[1:2]
#[1] 0.00 0.13
# y
my_formula(seq(0, 13, length.out=101)[1:2])
#[1] Inf 20.61066
And R will not plot infinite values to join the lines from the first point to the second one.
If you get as close to 0 on your x axis as is possible on your system, you can make this work a-okay. For instance:
curve(my_formula, col="red", xlim=c(0 + .Machine$double.eps, 13), ylim=c(0,50))

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 to control xtics in gnuplot

As the title says, I want to control the range and the amount of xtics. I use gnuplot for plotting data files in which the horizontal axes is usually time (t) taking values in the interval [0, t_max]. OK, now let's suppose the following scenario:
The maximum value of time is 4086 and this particular value is not known beforehand. However, it can be found using
stats "data.out" u 1
set xrange [0:STATS_max]
My question is how can I round up the maximum value of t to the closest hundred (4100 in this case)? Also, is there a way to tell gnuplot to print only 5 ticks at the horizontal axes regardless of its range (rounding up the maximum value to the closest hundred, or decade it will always be divided by 5)?
Many thanks in advance!
To round up to the closest hundred, just use the ceil function:
set xrange[0: 100*ceil(STATS_max/100.0)]
Regarding the xtics, you can only set the start, increment, end, and explicit tic position of the tics, but not the number of ticks. But since you set the xrange manually, you can use this information to calculate the tic frequency:
x_lim = 100*ceil(STATS_max/100.0)
set xrange[0: x_lim]
set xtics x_lim/4.0

Resources