I have the following data:
t4.8k 1.84 1.86 1.83
t5.8k 1.82 1.84 1.8
t7.10k 1.79 1.8 1.77
t8.8k 1.8 1.84 1.76
I need to plot this in GNU plot using yerror bars.
Column1 - dataset name. This is the xaxis scale.
Column2 - Y-Mean
Column3 - Y-Max
Column4 - Y-Min
Here is the plotting code that I use:
plot "chameleonConfidence.dat" using xtic(1):2:4:3 title "Ratio of Time Taken" with yerrorbars
But this gives me the following error
Warning: empty x range [4.94066e-324:4.94066e-324], adjusting to [4.94066e-324:4.94066e-324]
"chameleonConfidence.gplot", line 15: x_min should not equal x_max!
Can someone help me with this?
This works for me.
set xrange [-1:4]
TITLE="Ratio of Time Taken"
plot "chameleonConfidence.dat" using ($0):2:4:3:xticlabels(1) with yerrorbars title TITLE
the xtic function doesn't replace the x-values in your file -- it is an automagic extra field you can add to the using specification to add the xticlabels on your axis. Basically, you just need the line number ($0) to be the x value and then you are all set (moving xtic(1) to the end of your using spec)
Related
I am trying to use gnuplot for the first time.
I am completely new to gnuplot. Please forgive any basic mistakes.
I am trying to plot a stock chart.
My data looks like the following:
Date Open High Low Close
21/04/2017 31.81 32.09 31.67 31.95
20/04/2017 31.55 32.02 31.45 31.91
19/04/2017 31.3 31.71 30.99 31.57
18/04/2017 31.78 31.84 31.06 31.3
17/04/2017 31.3 31.97 31.21 31.8
13/04/2017 31.26 31.48 31.16 31.19
12/04/2017 31.13 31.38 30.98 31.24
11/04/2017 31.37 31.66 30.86 31.2
I am using the following settings to plot the lines. I got them from another website.
set xdata time
set timefmt "%d/%m/%Y"
set xrange ["21/04/2015":"21/04/2017"]
set format x "%d/%m/%Y"
plot [0:100] 'chart.dat' using 0:2:3:4:5 notitle with financebars
However, the x-axis just has 01/01/1970. See attached pic.
Any assistance would be greatly appreciated. Thanks.
The plot command plot [0:100] ... using 0:2:3:4:5 takes column 0 for x, column 0 corresponds to the line number instead of the time column.
This command should work:
plot 'chart.dat' using 1:2:3:4:5 notitle with financebars
I want to know how to create the plot of a stepwise function in Gnuplot. The function I want to plot includes the operations cost for several distance range and multiple products. For instance, if the distance is 0-300 Km for product 1 the cost is 1.05 $/Km and for product 2, it is 0.86 $/Km. When the distance increases, the cost for each product decrease.
I have defined one function for each product and plot them functions together:
gnuplot> f(x)=x<=300 ? 1.05 : x<=650 ? 0.65 : x<=1300 ? 0.46 : x<=1950 ? 0.4 : x<=3250 ? 0.31 : 0.22
gnuplot> x<=300 ? 0.86 : x<=650 ? 0.53 : x<=1300 ? 0.38: x<=1950 ? 0.32 : x<=3250 ? 0.24 : 0.19
gnuplot> plot [0:5000][0:3] f(x), g(x)
There is one problem: I can not remove the vertical lines. Any idea?
Thanks for your help
There are basically two approaches you can take. The best approach is to use a datafile, but you can use functions, although it will be more difficult.
Datafile Approach
You are probably going to have trouble doing this as a function, because you are going to get those vertical lines. A datafile gives you a little better control, and even allows you to mark the end points of the pieces of the piecewise function with the typical open/closed dots. Set up your data file with this format:
x y # left point of piece 1
x y # right point of piece 1
# one single blank line
x y # left point of piece 2
x y # right point of piece 2
# one single blank line
...
With your function f, we can do this like
0 1.05
300 1.05
300 0.65
650 0.65
650 0.46
1300 0.46
1300 0.4
1950 0.4
1950 0.31
3250 0.31
3250 0.22
6000 0.22
then plot datafile with lines gives
We can get even fancier with†
plot datafile w lines,\
last=0,\
"" u 1:(oldlast=last,last=$1,$1==oldlast?$2:1/0) w points pt 6 lt 1,\
last=0,\
"" u 1:(oldlast=last,last=$1,$1==oldlast?1/0:$2) w points pt 7 lt 1
to produce
Here we first plot the same curve as before. Then we initialize the variable last to be 0 (the value of the first x coordinate)‡, and plot the open dots.
To do this we evaluate (oldlast=last,last=$1,$1==oldlast?$2:1/0) which first stores the value of last as oldlast and then stores the value of the first column (the x coordinate) as last to use on the next point. Finally we check to see if the x-coordinate is the same as the value of oldlast (the value of the x-coordinate from the last point). If it is, we use the 2nd column value, otherwise we use the unplottable 1/0. This will cause points to be plotted only if the are the first point in the two point blocks. We plot these with points using pointstyle 6 (an open point) and linetype 1 (the same as used in the lines).
We do the same thing again, but this time plot the second points with filled dots (pointtype 7).
We can either add the points for the function g to the same file, separating it from the others by two blank lines and then use indexes to refer to them, or create a separate datafile for g. We can then add similar plot commands to the current command. For example, if we use the same file with function f followed by function g, we can do:
plot datafile i 0 w lines,\
last=0,\
"" i 0 u 1:(oldlast=last,last=$1,$1==oldlast?$2:1/0) w points pt 6 lt 1,\
last=0,\
"" i 0 u 1:(oldlast=last,last=$1,$1==oldlast?1/0:$2) w points pt 7 lt 1,\
datafile i 1 w lines,\
last=0,\
"" i 1 u 1:(oldlast=last,last=$1,$1==oldlast?$2:1/0) w points pt 6 lt 1,\
last=0,\
"" i 1 u 1:(oldlast=last,last=$1,$1==oldlast?1/0:$2) w points pt 7 lt 1
Function Approach
As far as getting only one jump, your functions have a lot of redundant conditions. Redefine f (and similarly for g) as
f(x)=x<=300 ? 1.05 : x<=650 ? 0.65 : x<=1300 ? 0.46 : x<=1950 ? 0.4 : x<=3250 ? 0.31 : 0.22
and then plot it. Make sure that the samples are set high enough, otherwise you may end up collecting multiple jumps together or get undesirable slanted lines. With
set xrange[0:6000]
set yrange[0:2]
set samples 1000
plot f(x)
we get
However, this will still get the vertical connecting lines. This is going to be very hard to avoid with a function. The best way that I can think of to avoid this is to inject a very small non-plottable value just before the breaks. For f(x), we can do this with
f(x)=x<=290 ? 1.05 : x<=300? (1/0) : x<=640? 0.65 : x<=650 ? (1/0) : x<=1290 ? 0.46 : x<=1300 ? (1/0) : x<=1940? 0.4 : x<=1950 ? (1/0) : x<=3240 ? 0.31: x<=3250? (1/0) : 0.22
Here, we have inject a non-plottable value of 1/0 for a region of length 10 just before the breaks. Smaller lengths can be used as well. If we set the samples high enough to be sure that the sampling hits each of these breaks (in this case a sample of 1000 like before is good enough), it will avoid connecting the points.
With samples set too small (for example 100), we might still get the connecting lines
Thus if we use a gap with a size smaller than 10, we may need to use higher sampling to avoid the connecting lines. Larger gaps may work with smaller sampling.
Depending on the sampling, the gaps might be larger than specified as well if the sampling is too low. For example, setting the gaps to a size of 100 with
f(x)=x<=200 ? 1.05 : x<=300? (1/0) : x<=550? 0.65 : x<=650 ? (1/0) : x<=1200 ? 0.46 : x<=1300 ? (1/0) : x<=1850? 0.4 : x<=1950 ? (1/0) : x<=3150 ? 0.31: x<=3250? (1/0) : 0.22
and a sampling of 10, we get
where the gaps have a size of 222.22 (I have added labels to make it easy to compute the gap sizeΔ), but with a sampling of 1000, we get
where the gaps have size 101.1, very close to the value of 100 specified in the function.
To use functions to do this, therefore, use this model and set the gap size to a value small enough that it will appear non-existent on the final graph (notice that on the graph from 0 to 6000, we can barely see the gap size of 10), and then set the samples reasonably high.
With the function approach, I don't know of any way to add the filled and open dots if those are desired.
† Gnuplot version 5.1 (the current development version) supports a pointtype variable option which can simplify this to
plot last=0,\
datafile u 1:2:(oldlast=last,last=$1,$1==oldlast?6:7) w linespoints pt var lt 1
Here we just plot all points, but use the same test as before to select between pointtype 7 or 6. As we can do both point types at once, we can just use the linespoints style instead of doing two separate plots.
‡ Initializing last to a value less than the first x-coordinate will cause that first point to be filled.
Δ To draw these labels, in the first case (with set xrange[0:1000] and set samples 10), I used
plot f(x),\
"+" u 1:(f($1)+0.1):(abs($1-250)<150||abs($1-600)<160?sprintf("%0.2f",$1):"") w labels
and in the case of set samples 1000
plot f(x),\
"+" u 1:(f($1)+0.1):(abs($1-250)<51||abs($1-600)<51?sprintf("%0.2f",$1):"") w labels
It takes a little playing around with the bounds on the abs functions here to get the desired labels to appear. Examining the output using set table can be helpful for getting them right.
Your ternary statements are trying to do too much. When you are writing
f0(x)=(x_low < x <= x_high) ? y : 0
you should be writing
f0(x)=((x_low < x) && ( x <= x_high)) ? y : 0
So your function f(x) should look like
f(x)=(x<=300) ? 1.05 : (x<=650) ? 0.65 : (x<=1300) ? 0.46 : (x<=1950) ? 0.4 : (x<=3250) ? 0.31 : (x<3250) ? 0.22 : 0
As for the plotting style if you want it to be discontinuous, use separate functions for the steps like and plot them individually. Your first function would be split up like:
f1(x)= (x<=300) ? 1.05 : 1/0
f2(x)=(x>300) && (x<=650) ? 0.65 : 1/0
...
If you just want steps without interpolation, use steps
plot [0:6000][0:3] f(x) w steps, g(x) w steps
Creating labels in gnulot is quite simple
set label "<value>" at <x,y> ...
But I want to read the value of a label in gnuplot from a file.
The lines in the file look this way:
...
400 300 8 0.200214 1.00193 7.42157 8.623714 86.06 13.94 1.26
800 600 1 0.2055 0.1938 34.9172 35.3165 98.86 1.14 1.00
800 600 2 0.2066 1.5514 21.1664 22.9244 92.33 7.67 1.54
800 600 4 0.2027 1.6316 14.9445 16.7788 89.06 10.94 2.10
800 600 8 0.242 1.8385 12.7261 14.8066 85.94 14.06 2.38
1024 768 1 0.2212 0.2217 55.1782 55.6211 99.20 0.80 1.00
...
I just need the values from the 10th column as labels.
Is it possible to realize something like this:
set label from <inputfile> <column_of_inputfile> <row_of_inputfile> ...
?
Thanks for any help.
This solution worked for me:
set title "800x600"
set xlabel "Nodes [#]\n"
set ylabel "Speedup" offset 2
set xrange [-0.55:3.55]
set yrange [0:5]
set style data histograms # plot boxes
set boxwidth 0.75 # have a gap between the boxes
plot 'inputfile.csv' every ::9::12 using 10:xtic(3) title "800x600" lc rgb "grey",\
'' every ::9::12 using :10:10 with labels center offset 0,1 tc rgb "black"
The first plot line does in detail:
every ::9::12 => plot line 9-12 of the inputfile.
using 10:xtic(3) => the value of column number 10 specifies the height of the data histograms. The value of column 3 is used for labeling the x axis.
The second plot line plots the content of column 10 of the lines 9-12 as label in center orientation above the data histograms.
The image shows the result.
I've had a look through questions but still can't get this working.
My data set is like this:
[date] , [%cpu] , [mem]
23:00:39 , 21.9 , 2.1
23:00:44 , 21.8 , 2.1
23:00:49 , 21.8 , 2.1
23:00:54 , 21.8 , 2.1
23:00:59 , 21.7 , 2.1
My Gnuplot statements (just started using for this data) is:
set timefmt "%H:%m:%s"
set xdata time
set datafile sep ','
plot '/tmp/info' using 2 title 'cpu' with lines, '/tmp/info' using 3 title 'memory%' with lines
I get the following error:
Need full using spec for x time data
I've tried autoscale x , but I'm a bit lost, any help would be appreciated.
Time data requires that you always specify all columns to be used. (Note also the corrected timefmt):
set timefmt "%H:%M:%S"
set xdata time
set datafile sep ','
set style data lines
plot '/tmp/info' using 1:2 title 'cpu', '' using 1:3 title 'memory%'
The reason for this is, that the timefmt may also contain spaces, so that the data used for the time axis may come from two columns. Consider the following modified data file:
23:00:39 06/08/13 21.9 2.1
23:00:44 06/08/13 21.8 2.1
23:00:49 06/08/13 21.8 2.1
23:00:54 06/08/13 21.8 2.1
23:00:59 06/08/13 21.7 2.1
The plotting commands for this format are:
set timefmt "%H:%M:%S %d/%m/%Y"
set xdata time
set format x "%H:%M:%S"
set style data lines
plot 'mydata.dat' using 1:3 t 'cpu', '' using 1:4 t 'memory%'
To avoid confusion, it is always required that for time data all columns used by the plotting style (here with lines) are given explicitely with the using statement.
Your plot command look like this instead:
plot '/tmp/info' using 1:2 title 'cpu' with lines, '/tmp/info' using 1:3 title 'memory%' with lines
I had a similar issue with gnuplot where I had a data file like:
05:07:00 0.0769 0.0769 0.0000 0.0000 0.0000 0.0000 0.0000
05:08:00 0.2308 0.2308 0.0000 0.0000 0.0000 0.0000 0.0000
I was trying to use, but it was just not working
set xdata time
set timefmt "HH:MM:SS";
plot "wed" using 0:2 t 'wtf" w lines
The fix was a couple of things, primarily using the %'s in the timefmt string and only using one letter. Also using the set format x for the output was key (and using the first column as 1 - though I had tried that also earlier).
This is the minimal output script that is working for me:
set xdata time
set timefmt "%H:%M:%S"
set format x "%H:%M"
plot "wed" using 1:2 t 'my title' w lines
I have the following dat file, named ls.dat:
# Gnuplot script file for "ls"
# Version Removed Added Modified
8.1 0 0 0
8.4 0 0 4
8.5 2 5 9
8.6 2 7 51
8.7 2 7 51
8.8 2 7 51
8.9 2 7 51
8.10 2 7 51
8.11 2 8 112
8.12 2 8 112
8.13 2 17 175
8.17 6 33 213
I am trying to plot with this:
plot "ls.dat" using 1:2 title 'Removed' with lines,\
"ls.dat" using 1:3 title 'Added' with lines,\
"ls.dat" using 1:4 title 'Modified' with lines
This produces the following graph:
What I am expecting is three line plots which should all go up, but at different rates. Can anyone see what is going on here? I'm sure it must be something very silly.
I think your problem is your version numbers. Try making 8.1 --> 8.01, and so forth. That should put the points in the right order.
Alternatively, you could plot using X, where X is the column number you want, instead of using 1:X. That will plot those values on the y axis and integers on the x axis. Try:
plot "ls.dat" using 2 title 'Removed' with lines, \
"ls.dat" using 3 title 'Added' with lines, \
"ls.dat" using 4 title 'Modified' with lines
andyras is completely correct. One minor addition, try this (for example)
plot 'ls.dat' using 4:xtic(1)
This will keep your datafile in the correct order, but also preserve your version tic labels on the x-axis.
In addition to the answers above the command below will also work. I post it because it makes more sense to me. In each case it is 'using x-value-column: y-value-column'
plot 'ls.dat' using 1:2, 'ls.dat' using 1:3, 'ls.dat' using 1:4
note that the command above assumes that you have a file named ls.dat with tab separated columns of data where column 1 is x, column 2 is y1, column 3 is y2 and column 4 is y3.
Edit for .csv file types....
Note if you have a .csv file then if you use the gnuplot command
set datafile separator comma
you can then use the plot command above for data files where the numbers are separated by commas.
Whatever your separator is in your ls.dat, you can specify it to gnuplot:
set datafile separator "\t"