I am trying to make a chart of data, which depends on time. I have this data:
01/1851 NaN 0.812 0.862 0.735 2.19
02/1851 NaN 1.739 1.733 1.695 1.875
03/1851 NaN 1.472 1.376 1.458 3.31
04/1851 NaN 1.847 1.775 1.828 6.06
05/1851 4.48 2.038 1.762 2.125 7.695
06/1851 10.06 0.347 0.276 0.383 5.48
07/1851 6.67 0.402 0.323 0.395 9.76
08/1851 3.21 1.57 1.469 1.421 8.62
09/1851 1.78 2.64 2.616 2.589 10.075
10/1851 3.7 1.269 1.314 1.137 4.295
11/1851 7.62 6.206 6.236 6.058 8.87
I need to make a chart of all this columns in one chart. Problem is, that date is monthly, and some values are NaN (not a number) which should not be ploted, but it says "all points y value undefined!" when i do this
set xdata time
gnuplot> set timefmt "%m/%y"
gnuplot> set xrange ["01/1851":"12/2014"]
gnuplot> set format x "%m/%y"
gnuplot> set timefmt "%m/%y"
gnuplot> plot "milesovka.txt" u 2:3
Can somebody help me? I tried this even ix excel, but i doesn't take dates before 1900.
Why do you specify u 2:3? You then wouldn't have a date on the x-axis.
This seems to work fine:
set xdata time
set timefmt "%m/%Y"
set format x "%m/%Y"
plot for [i=2:6] "milesovka.txt" u 1:i ti "Column ".i w lp
Related
I want to indicate on graph y-axis logarithmic grids like SNR plot that enabled dash or dot y-tics. Matlab and similar programs does this, but need to use Gnuplot.
### sum up for certain conditions
reset session
set key right box
set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#ffffff" behind
set datafile separator comma
set grid
set output 'logscaled.png'
set logscale y 10
set ytics
show ytics
set y2tics
show ytics
set xlabel "SNR"
set ylabel "BER"
plot "snr-ber.csv" title 'Random' w l
### end of code
SNR
BER
-5.00
2.8301e-01
-4.00
2.6931e-01
-3.00
2.3582e-01
-2.00
2.2382e-01
-1.00
1.511e-01
0.00
1.4879e-01
1.00
1.2080e-01
2.00
1.1439e-01
3.00
8.5206e-02
4.00
6.6113e-02
5.00
4.7682e-02
6.00
3.3031e-02
7.00
2.2605e-02
8.00
5.61010e-03
9.00
3.6660e-03
10.00
8.5350e-04
11.00
2.7550e-04
12.00
4.8500e-05
13.00
1.6000e-06
14.00
0.0000e+00
set grid x y my will enable grid lines for the major tics along both the x and y axes, and minor tics along the y axis.
I'm using Gnuplot with the 2,2 multiplot environment.
One of my datasets looks like this:
# Avg1. Min1. Max1. Avg2. Min2. Max2.
25 0.049 0.002 0.108 0.051 0.004 0.102
50 0.034 0.005 0.070 0.036 0.004 0.086
100 0.028 0.012 0.044 0.026 0.012 0.054
And I'm using the following script to plot the first graph (I think once I get the first one right I can just repeat the code):
#!/usr/bin/env gnuplot
set term post eps color solid enh
set multiplot layout 2,2 rowsfirst
set grid ytics
set offsets 0.5, 0.5
unset key
set ylabel offset 1,0
set xtics ("25" 1, "50" 2, "100" 3)
### First plot
set tmargin at screen 0.95
set bmargin at screen 0.65
set lmargin at screen 0.10
set rmargin at screen 0.45
set ylabel 'Y-Label Here'
plot 'data.dat' u :2:3:4 w yerrorlines ti 'Title1', \
'' u :5:6:7 w yerrorlines ti 'Title2'
### three other graphs
unset multiplot
And I have three more plots like this. The problem is that my X-axis only shows 25 and 50 (shown below).
I don't know how to fix this. Can anyone help please?
I've tried using 1:2:3:4 instead, but it shows the intermediate X-tics, which I don't want to show.
PlotExample
If you don't specify an explicit column for the x-value, then gnuplot uses the row index, which starts at zero:
set xtics ("25" 0, "50" 1, "100" 2)
plot 'data.dat' u 0:2:3:4 w yerrorlines ti 'Title1'
You can also directly use the values in the first column as xticlabels:
plot 'data.dat' u :2:3:4:xtic(1) w yerrorlines ti 'Title1'
I'm trying to plot a histogram with the xtics as time information. The gp script I'm using is this one:
set style data histogram
set style histogram cluster gap 1
set boxwidth 1
set datafile separator "\t"
set yrange[0:3000]
set xlabel "xaxis"
set ylabel "yaxis"
set xdata time
set timefmt "%Y-%m-%d %H"
set terminal png size 1200,800
set output outputfile
plot inputfile u 2:1 title "Count"
An example of my inputfile(that I pass by parameter) is this one:
2012-07-22 00:00:00 159.361111111
2012-07-22 01:00:00 207.019166667
2012-07-22 02:00:00 191.749722222
2012-07-22 03:00:00 147.647777778
2012-07-22 04:00:00 107.751388889
2012-07-22 05:00:00 95.9566666667
2012-07-22 06:00:00 110.405277778
2012-07-22 07:00:00 151.689166667
2012-07-22 08:00:00 244.787777778
2012-07-22 09:00:00 481.601388889
However I'm receiving the error "plot_timeflow.gp", line 16: Too many columns in using specification and when I try to set the plot command to plot inputfile u 2:xticlabels(1) title "Count" I have the following error: "plot_timeflow.gp", line 16: Need full using spec for x time data.
Does someone have any ideia, what is the problem??
Thanks in advance.
Strange indeed. But in any case, for your data the boxes plotting style is better, because with histograms, every box has an own xtic. With boxes, the x-axis is treated as a normal time axis:
set boxwidth 0.8 relative
set datafile separator "\t"
set yrange[0:3000]
set xlabel "xaxis"
set ylabel "yaxis"
set xdata time
set timefmt "%Y-%m-%d %H"
set style fill solid
plot 'data.txt' using 1:2 with boxes title "Count"
That gives:
I'd like to plot hours of the day on the x axis, but print the date when the day rolls over from one day to the next. So the x axis might look like this:
11/02 04:00 08:00 12:00 16:00 20:00 11/03 04:00 08:00 ...
Is there a sane way to do this in gnuplot?
FWIW, my file currently looks something like this:
set xdata time
set timefmt "%Y-%m-%d|%H:%M:%S"
plot '-' using 1:2 with lines linewidth 1 linecolor rgb "#FF0000"
2013-11-02|00:00:48 123.0
2013-11-02|00:00:55 124.0
2013-11-02|00:01:06 121.0
2013-11-02:00:01:17 123.0
...
2013-11-04|23:59:41 241.0
2013-11-04|23:59:52 241.0
That is a tough one, made me think a bit. Here is how you can do it:
You need to set the format of the x-axis to %H:%M and then replace the 00:00 with dates, similarly like done in this answer.
The main work is then to extract the timestamps for the mid nights. I use the stats command for this (needs at least version 4.6.0), but because it doesn't support time data you must fiddle around a bit with strptime and similar:
fmt = "%Y-%m-%d|%H:%M:%S"
stats 'file.txt' using (strptime(fmt, stringcolumn(1))) nooutput
t = int(STATS_min)
t_start = t - tm_hour(t)*60*60 - tm_min(t)*60 - tm_sec(t)
num_days = 2 + (int(STATS_max) - t)/(24*60*60)
set xdata time
set timefmt fmt
set xtics 4*60*60
set for [i=1:num_days] xtics add (strftime('%m/%d', t_start+(i-1)*24*60*60) t_start+(i-1)*24*60*60)
set format x '%H:%M'
plot 'file.txt' using 1:2 with lines
I increment num_days by 2 to account for the possible automatic extension of the x-range to the next tics.
The result with your data is (with 4.6.4):
Here is a version with xticlabels:
$data <<EOD
2013-11-02|00:00:48 123.0
2013-11-02|00:00:55 124.0
2013-11-02|00:01:06 121.0
2013-11-02|00:01:17 123.0
2013-11-04|23:59:41 241.0
2013-11-04|23:59:52 241.0
EOD
set xdata time
set timefmt "%Y-%m-%d|%H:%M:%S"
plot $data using 1:2 w l # plot to get GPVAL_X_MIN GPVAL_X_MAX
t = GPVAL_X_MIN
t_start = t - tm_hour(t)*60*60 - tm_min(t)*60 - tm_sec(t)
num_days = 1 + (GPVAL_X_MAX-GPVAL_X_MIN)/(24*60*60)
replot [i=0:6*num_days:1] '+' us (t=t_start+i*24*60*60/6, strftime('%Y-%m-%d|%H:%M:%S', t)):(NaN):xtic(strftime(int(i)%6?'%H:%M':'%m-%d',t)) t ""
So the format is selected with int(i)%6?'%H:%M':'%m-%d'.
This was inspired by gnuplot: set link and x2tics in interactive mode.
I'm having this file as data.dat:
Xstep Y1 Y2 Y3 Y4
332 1.22 0.00 0.00 1.43
336 5.95 12.03 6.11 10.41
340 81.05 81.82 81.92 81.05
394 11.76 6.16 10.46 5.87
398 0.00 0.00 1.51 1.25
1036 0.03 0.00 0.00 0.00
I can plot this data as histogram with this script, hist-v1.gplot (using set style data histogram):
set xlabel "X values"
set ylabel "Occurence"
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set term png
set output 'hist-v1.png'
set boxwidth 0.9
# attempt to set xtics so they are positioned numerically on x axis:
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)
# ti col reads the first entry of the column, uses it as title name
plot 'data.dat' using 2:xtic(1) ti col, '' u 3 ti col, '' u 4 ti col, '' u 5 ti col
And by calling:
gnuplot hist-v1.gplot && eog hist-v1.png
this image is generated:
However, you can notice that the X axis is not scaled numerically - it understands the X values as categories (i.e. it is a category axis).
I can get a more numerical X axis with the following script, hist-v2.gplot (using with boxes):
set xlabel "X values"
set ylabel "Occurence"
# in this case, histogram commands have no effect
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set term png
set output 'hist-v2.png'
set boxwidth 0.9
set xr [330:400]
# here, setting xtics makes them positioned numerically on x axis:
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)
# 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"!
plot 'data.dat' using 1:2 ti col smooth frequency with boxes, '' u 1:3 ti col smooth frequency with boxes
And by calling:
gnuplot hist-v2.gplot && eog hist-v2.png
this image is generated:
image hist-v2.png http://img266.imageshack.us/img266/6717/histv2.png
Unfortunately, the bars 'overlap' here, so it is hard to read the graph.
Is there a way to keep the numerical scale X axis as in hist-v2.png, but keep the 'bars' side by side with as in hist-v1.png? This thread, "Re: Histogram with x axis date error" says you cannot:
But it will be hard to pull the x-coordinate date out of the data file, ...
but then, it refers to a different problem...
Thanks,
Cheers!
Ok, after reading the gnuplot help for a bit, it seems that histogram style will ''always'' interpret x axis as sequential entries/categories - so indeed, there seems to be no way to get a numerical axis with a histogram style.
However, it turns out that $ can refer to a column, and those can be used to actually 'reposition' the bars in the second (frequency with boxes style) example; so with this code as hist-v2b.gplot:
set xlabel "X values"
set ylabel "Occurence"
set style fill solid border -1
set term png
set output 'hist-v2.png'
set boxwidth 0.9
set xr [330:400]
# here, setting xtics makes them positioned numerically on x axis:
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)
# 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"!
plot 'data.dat' using ($1-0.5):2 ti col smooth frequency with boxes, '' u ($1-0.25):3 ti col smooth frequency with boxes, '' u ($1+0.25):4 ti col smooth frequency with boxes, '' u ($1+0.5):5 ti col smooth frequency with boxes
And by calling:
gnuplot hist-v2b.gplot && eog hist-v2b.png
this image is generated:
image hist-v2b.png http://img823.imageshack.us/img823/805/histv2b.png
... which is pretty much what I wanted in the first place.
Just a small note - I originally wanted to use the script with inline data; for a setup like this, it would have to be written as
plot '-' using ($1-0.5):2 ti col smooth frequency with boxes, '-' u ($1-0.25):3 ti col smooth frequency with boxes
Xstep Y1 Y2 Y3 Y4
332 1.22 0.00 0.00 1.43
336 5.95 12.03 6.11 10.41
340 81.05 81.82 81.92 81.05
394 11.76 6.16 10.46 5.87
398 0.00 0.00 1.51 1.25
1036 0.03 0.00 0.00 0.00
end
Xstep Y1 Y2 Y3 Y4
332 1.22 0.00 0.00 1.43
336 5.95 12.03 6.11 10.41
340 81.05 81.82 81.92 81.05
394 11.76 6.16 10.46 5.87
398 0.00 0.00 1.51 1.25
1036 0.03 0.00 0.00 0.00
end
... that is, the data would have to be entered multiple times, as it comes in from stdin - this problem is discussed in gnuplot - do multiple plots from data file with built-in commands.
Cheers!
PS: As there is quite a bit of space on the diagram, it would be nice if we could somehow specify separate x-axis ranges; that is discussed in:
Gnuplot tricks
Gnuplot tricks: Broken axis revisited
Setting the box width properly is very important when you plot a histogram using "boxes" plot style. In one of my blog article I have talked about it. If any interest,click here!