Gnuplot: setting custom labels on x2-axis - plot

I have a data files including multiple float-value columns such as following and I want to plot some columns in gnuplot.
1.08 1.6 4.83
1.53 2.5 5.95
2.11 3.2 6.1
2.60 4.1 7.0
I want to have x2-axis by following:
set xtics nomirror
set x2tics
set autoscale xfix
set autoscale x2fix
set ylabel 'Y_H
set xlabel 'Y_O'
set x2label 'Y_C'
p 'datafile' u 1:2 w l ls 1 t '',\
'' u 1:(NaN):x2ticlabels(3) axes x2y1 w l ls 1 t ''
This way displays every single tic and It's label on the x2 axis. I would like to set custom labels for x2-axis such as 4,5,6,7 and their tics to write. How can I have custom labels and tics on x2-axis?

x2tics behave the same way as xtics so all functions available for xtics are also applicable to x2tics.
Set a range by:
set x2range [1:5]
This command will give you a range of 1 to 5 on the x2 axis.
If you want to set custom labels then:
set x2tics ("one" 1, "two" 2, "three" 3, "four" 4, "five" 5)
This command will give you the words inside double quotes at x2=1, x2=2 and so on.
In your case you can get numerical values on x2 axis by using set x2range [4.83:7] and changing second plot to 1:(NaN):3

Related

Hierarchial Axis in GNUPLOT

I am trying to plot a figure using GNUPLOT, but I want to have a hierarchical x-axis, it should be six tick points in x-axis with first level of label in x-axis being 32,64,128,32,64 and 128. Here the first 32, 64 and 128 should be grouped as stencil and the second 32, 64 and 128 should be grouped as diamond in second level of label . Current i am doing this as stencil-32, stencil-64, stencil-128, diamond-32, diamond-64 and diamond-128. Is there a way to change it into hierarchical axis lableling?
If I correctly understand what you mean with hierachical axis the following code could be one possible way of many others.
You could simply increase the bottom margin and add some labels. But if you want to have some axis, using multiplot might be an option. You need to make sure that the margins are identical (especially the left margin).
Code:
### hierarchical axis
reset session
$Data <<EOD
1 stencil 32 0.3
2 stencil 64 0.4
3 stencil 128 0.5
4 diamond 32 0.6
5 diamond 64 0.7
6 diamond 128 0.8
EOD
myBottomMargin = 0.12
myColor(col) = strcol(col) eq "stencil" ? 0xff0000 : 0x0000ff
set multiplot
set origin 0, myBottomMargin
set size 1, 1-myBottomMargin
set lmargin screen 0.1
set style fill solid 1.0
set boxwidth 0.8
set yrange [0:1]
plot $Data u 0:4:(myColor(2)):xtic(3) w boxes lc rgb var notitle
set origin 0, 0
set size 1, myBottomMargin
set border 1 # only bottom border
set xtics nomirror
set xrange [0.5:2.5]
set mxtics 2
set xtics 1 add ("stencil" 1, "diamond" 2) scale 0,1
set yrange [0:1]
unset ytics
unset key
plot NaN # dummy plot just for the extra axis
unset multiplot
### end of code
Result:

gnuplot error:"Tic label does not evaluate as string!" in bar chart

I'm working on Gnuplot 4.6 patch 4.
I want to create the bar chart with palette. I've followed this but I got error:
Tic label does not evaluate as string!
Yes, the different with the reference that I mentioned above are the xtics. My graph uses string in xtics.
Here is my sample data:
off 100.0
gpl 60.0
docs 99
vim 9.4
box 95
goo 60
ama 99.9
eba 99.98
and my plot file is:
set term pos eps font 20
set style data histogram
set style histogram rowstacked
set boxwidth 0.75
set format y "%.0f%%"
set style fill solid border -1
set xtics font "Times-Roman, 15" rotate by 45 right
#set xtics center offset 0,-1
set ytics font "Times-Roman, 15"
set palette defined ( 0 "#FFFFFF",\
1 "#FFCCCC",\
2 "#FF9999 ",\
3 "#FF6666",\
4 "#FF3333",\
5 "#FF0000",\
6 "#CC0000",\
7 "#C00000",\
8 "#B00000",\
9 "#990000",\
10 "#A00000")
set ylabel "Percentages"
#set xlabel "Services"
set yrange [70:100]
set output 'a2.eps'
plot 'a2.dat' \
using ($2):xtic(1):($2<=10 ? 0 : $2<=20 ? 1 : 2) t '' with boxes palette
Thanks for help!
The xtic label part should always be the last in a using statement. Also, when using the boxes plotting style you must specify x, y and color values:
plot 'a2.dat' \
using 0:2:($2<=10 ? 0 : $2<=20 ? 1 : 2):xtic(1) t '' with boxes palette
Note, that with boxes overwrites the histogram style you set earlier. Also, the values given in the palette aren't absolute values, but those color values are scaled to accomodate your effictive color range of [0:2].

Gnuplot: xtic showing every given number of data elements

Let me just say that i am new to gnuplot...
Trying to plot from file :
"Dataset " "Min_sup (%)" Itemsets "Rules " "Exec_time (s)"
Giorno_1 0.1 16392 260337 15.23
Giorno_1 0.2 9719 155963 11.96
Giorno_1 5.0 275 2495 6.43
Giorno_2 0.1 15023 212058 14.14
Giorno_2 0.2 8503 115766 14.62
Giorno_2 0.4 2962 43710 12.90
Giorno_2 0.8 1603 17514 10.53
Giorno_2 1.0 1223 14701 9.96
I want to plot using as x axis the the "Min_sup" column AND the "Dataset" column. Here's the problem: as you see the "Dataset" column has value that repeat and i want to show them only one time in the graph.
So basically i am searching for a way to select when to plot the the x2tics.
The gnuplot script i am using is:
set style data histograms
set grid
set terminal png nocrop enhanced font verdana 12 size 1024,768
set output "graph.png"
set xtics norangelimit
set xtics border in scale 1,0.5 nomirror rotate by -45 offset character 0, 0
set x2tics ("dataset1" "Giorno_1","dataset2" 2,"dataset3" 3,"dataset4" 4,"dataset5" 5,"dataset6" 6)
set x2tics scale 10
set xlabel "Minimum support in %"
set ylabel "# of "
set style fill transparent solid 0.6 noborder
set datafile separator " "
plot 'prova.dat' using 4:xtic(2):x2ticlabels(1) title col , \
'prova.dat' using 3:xtic(2):x2ticlabels(1) title col
It is a bit tricky to do that. Basically, you must save the "Dataset" value of the current and the previous row and compare them. If they are equal, don't set a new x2ticlabel, other use it as new label.
The values are stored with the using command:
s1 = ''
s2 = ''
plot 'prova.dat' using (s2 = s1, s1 = stringcolumn(1), $4):...
Here, s2 is assigned the value of s1 and, therefore, hold the value of the previous row. Then, s1 is assigned the string-value of the first column. With $4, the fourth column is used. This has nothing to do with the comparision, but is the 'normal' part of the using statement (using 4 and using ($4) is almost the same).
The check is done in the x2ticlabel function:
x2tic(s1 eq s2 ? 1/0 : s1)
This uses the value of the variable s1 as x2ticlabel if s1 and s2 are equal. Otherwise it uses 1/0. With this you'll get some warnings of the kind Tic label does not evaluate as string!, but that is what you want.
Finally, I removed some default settings from your script:
set terminal png nocrop enhanced font verdana 12 size 1024,768
set output "graph.png"
set style data histograms
set grid
set xtics nomirror rotate by -45
set x2tics left scale 10
set xlabel "Minimum support in %"
set ylabel "# of "
set style fill transparent solid 0.6 noborder
s1=''; s2='';
plot 'prova.dat' using (s2 = s1, s1 = stringcolumn(1), $4):xtic(2):x2tic(s1 eq s2 ? 1/0 : s1) title col , \
'prova.dat' using 3 title col
Gives (with 4.6.3):

Gnuplot plot 2D matrix with image, want to draw borders for each cell

I want to plot a 18x18 matrix with gnuplot. Here is my codes:
set size ratio 1
set palette gray negative
set xrange[-0.5:17.5]
set yrange[-0.5:17.5]
set cbrange[-0.2:0.8]
set xtics 0,1,17
set ytics 0,1,17
set xtics offset -0.5,0
set title "Resolusition Matrix for E"
plot "Mat" matrix w image noti
Then I got a fig like this:
Now I would like to add borders to each cell, which will look like this:
Thank you.
For your case you can set one minor tic, which then lies on the border between two pixels, and draw a grid on them:
set size ratio 1
set palette gray negative
set autoscale xfix
set autoscale yfix
set xtics 1
set ytics 1
set title "Resolution Matrix for E"
set tics scale 0,0.001
set mxtics 2
set mytics 2
set grid front mxtics mytics lw 1.5 lt -1 lc rgb 'white'
plot "Mat" matrix w image noti
Note, that set grid front also brings the tics to the front. To avoid that you can scale the tics to 0. For the minor tics you must use a very small number, 0 would omit the grid lines on the minor tics.
The result with 4.6.3 is:
EDIT: In order to control the grid lines and tic labels independently, you can use the unused x2 and y2 to draw the grid (inspired by an answer to How do I draw a vertical line in gnuplot?):
set size ratio 1
set palette gray negative
# grid lines
set x2tics 1 format '' scale 0,0.001
set y2tics 1 format '' scale 0,0.001
set mx2tics 2
set my2tics 2
# labeling
set xtics 5 out nomirror
set ytics 5 out nomirror
set grid front mx2tics my2tics lw 1.5 lt -1 lc rgb 'white'
set xrange[-0.5:39.5]
set yrange[-0.5:39.5]
set x2range[-0.5:39.5]
set y2range[-0.5:39.5]
plot "Mat" matrix w image notitle
With gnuplot version 4.6, this requires setting explicit ranges, so that the x and x2 (unused!) are equal. The information might be extracted with stats from the data file.
Using version 5 allows you to use set link. Instead of all the set *range stuff. You could use:
set autoscale fix
set link x
set link y
Result:

Gnuplot: Plot x2 axis with respect to x1 axis

I seem to be having some difficulty finding the answer to this question online. The title is the basic question, but to be more specific I would like to have two x axes, one at the top of the figure that is dependent on the one at the bottom. However, this is not a simple relationship, i.e. x2!=5*x1 or something like that. The relationship is given by the data file itself. So to be more specific I have a file that looks something like this:
V T P
2.0 15.0 0.586
3.0 17.4 0.798
4.0 25.3 1.023
5.0 28.9 1.124
6.0 30.2 1.456
I would like to make a plot of T with respect to (wrt) P on the x1y1 axes and have T wrt V on the x2y1 axes. So the x1 axis would display the P range and x2 would display the V range in the corresponding places of x1, i.e. 0.586 on x1 axis would have 2.0 on x2 axis at the same place. Is this actually possible in Gnuplot or do I have to have a relationship with the two x axes to do this? Any help would be greatly appreciated. Thank you in advance.
Here is how you can achieve this. I first show you the script and the result, and later explain the steps:
reset
set xtics nomirror
set x2tics
set autoscale xfix
set autoscale x2fix
set xlabel 'P'
set ylabel 'T'
set x2label 'V'
plot 'data.txt' using 3:2 with linespoints ps 2 lw 2 title 'T wrt P', \
'' using 3:2:x2tic(1) axes x2y1 with points ps 2 lw 2 title 'T wrt V'
I first plot T wrt P on x1y1. Afterwards I plot T wrt V on x2y1 and use for this the range and tic positions of P, but use the V values as tic labels for the x2 axis. This gives a linear scale for P and adapts V accordingly.
In order for this to work you must use set autoscale xfix and set autoscale x2fix. This uses the exact ranges and does not expand an axis to the next major tics, which would be done only for the x axis, but not for the x2 axis, which has custom tics.
You could of course also reverse the process and use a linear scale for V and adapt the P tics. In any case, for the custom tics, which are placed with xtic() or x2tic, the numbers are used like they are formatted in the data file.
reset
set xtics nomirror
set x2tics 1
set autoscale xfix
set autoscale x2fix
set xlabel 'P'
set ylabel 'T'
set x2label 'V'
plot 'data.txt' using 1:2:xtic(3) with linespoints ps 2 lw 2 title 'T wrt P', \
'' using 1:2 axes x2y1 with points ps 2 lw 2 title 'T wrt V'
Here, the points are shown for both plot lines, to demonstrate, that they really coincide.
In order to have the one command only generating the xtics, one can use NaN for the y-value. And if only some of the custom tics should be labels, one needs an appropriate check in the x2tic call. Here, I set labels only for all even rows $0 is the current row number, starting from 0):
reset
set xtics nomirror
set x2tics
set autoscale xfix
set autoscale x2fix
set xlabel 'P'
set ylabel 'T'
set x2label 'V'
plot 'data.txt' using 3:2 with linespoints ps 2 lw 2 title 'T wrt P', \
'' using 3:(NaN):x2tic((int($0) % 2) ? '' : stringcolumn(1)) axes x2y1 t ''
With the result:
The best way: Plot your data as usual on the x1 and y1 axes, but place additional labels on the x2-axis with x2tic(column_number):
set x2tics
set xtics nomirror
plot 'data.txt' using 3:2:x2tic(1) w lp
see: Plot y1 in x1 with respect to x2 axis

Resources