How to control xtics in gnuplot - plot

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

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 set octave x-axis limitation and interval

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.

Increasing the yRange by a fixed amount

I want my legend to be inside the plot and therefore, I want to increase the yRange by a fixed amount. However, that fixed amount should be %20 of maximum y amount.
Some graphs I plot are percentages and some of them are just values. Hence, I cannot use the same range for all, but I need to increase the yRange so that legend won't overlap with the plot itself.
How can I do it?
You can use set offsets to achieve this. Use
set offsets 0,0,graph 0.2,0
to expand the upper graph boundary by 20% of the total, automatically calculated height. This might be a bit more then 20% of the maximum y-value because gnuplot first expands to the next major tics. If you want to have exactly 20% of the maximum, you must use
set autoscale yfixmax
set offsets 0,0,graph 0.2,0
If you have Gnuplot version 4.6 then you can leverage stats to get the max/min of the y column in your data file and then use that to augment the yrange. Assuming your y data is in column 1 of your data file:
stats 'datafile' using 1
y_max_augmented = STATS_max + STATS_max * 0.2
set yrange [STATS_min:y_max_augmented] # you may use any other value in place of STATS_min

how histogram in Gnuplot works

I try to reproduce a simple histogram with Gnuplot with the simple macro:
reset
n=9 #number of intervals
width=1 #interval width
hist(x,width)=width*floor(x/width)
set terminal pngcairo size 800,500 enhanced font 'Verdana,14'
set output "test.png"
set boxwidth width
set style fill transparent solid 0.5 border #fillstyle
set xrange [*:*]
set yrange [0:2.]
set xlabel "x"
set ylabel "Freq."
plot "const.dat" u (hist($1,width)) smooth freq w boxes lc rgb "orange" notitle
whit the follow data:
1.1
1.1
1.1
1.1
1.1
1.1
1.1
1.1
Now I like to understand how the hist(x,width) works in the sense:
hist(x,width)=width*floor(x/width)
works with every numbers taking the width=1 and then:
hist(1.1,1)=1*floor(1.1/1)=1
and so on, right?
Now (hist($1,width)) take all the elements in the columns and applay the hist function to everyone.
And I can be able to make the follow plot with the macro above:!
Question:
If I use (hist($1,width)):(1.0) I Don't understand whit the plots change as all the elements stay in one single boxes (from 0.5 to 1.5) ?
In the first case you specify only a single column in the using statement. Since you need at least two (x and y-value), the specified value (your hist(...)) is used as y-value and the row number as x-value. The statement smooth frequency works such, that it takes all points with the same x-value and sums up the corresponding y-values. In your first example you have no equal x-values since the row number is used.
In the second example, you use the hist(...) value as x-value, which is 1 for all rows. The y-value is 1.0. So you get a single box at x=1 and y=8 (the number of rows).

gnuplot: Points overlap in pm3d with a wide xrange

I'm using gnuplot to plot three dimentions of data using pm3d. I'm trying to plot the number of times an event occurs (z value) with respect to the day of the year (x value) and hour of the day (y value).
Using pm3d works great for up to a range of 600 (rought 2 years of data). However, the points begin to overlap each other when a wider range is required.
I believe this is related to the fact that gnuplot isn't stretching the plot to the full size specified in set terminal. I haven't however been able to find a setting that controls this directly.
the script I'm using:
set terminal png size 10000, 1000
set output "%s_plot.png"
set title "%s's"
set ytics 1,1
set xtics 1
set xrange[0:%s]
set yrange[0:23]
set cbrange[0:%s]
set pm3d map
set palette defined (0 "white", 1 "blue", 31 "red")
splot '%s.data'
aspect of the plot for a range of [0:1000] in x:
aspect of the plot for a range of [0,100] in x:
(the images above are just snippets of the whole thing)
What can I do to remedy this? Perhaps the solution is manually setting the points (squares) to have a fixed width.
Thanks.
For the kind of plot that you want, I would replace your last line with:
plot '%s.data' matrix with image

Resources