Gnuplot histogram with rgb colours from file - colors

I have a file formatted as follows:
A 12.0 255,20,147
B 325.0 255,255,0
C 134456.0 255,255,0
D 13869.0 0,0,0
E 4321.0 255,0,0
F 43676.0 165,42,42
I would like to produce a histogram where $2 is the height of the bar, $1 is the label (i.e.: it is written below the bar, under the x axis) and $3 is the RGB colour of the bar.
I have read this and this but I still can't figure out how to do what I just described.

I found a way to do it: you first have to change the formatting of the rgb colours changing the commas to spaces, so that the new file becomes:
A 12.0 255 20 147
B 325.0 255 255 0
C 134456.0 255 255 0
D 13869.0 0 0 0
E 4321.0 255 0 0
F 43676.0 165 42 42
then you have to instruct gnuplot on how to parse the rgb colours:
rgb(r,g,b) = int(r)*65536 + int(g)*256 + int(b)
finally you can specify that you want column 1 to be the label (xticlabels(1)) and that you want variable colour (lc rgb variable), passing your rgb function the appropriate columns of your file (rgb($3,$4,$5)).
Summing it all up, the following commands worked for me:
set style fill solid
rgb(r,g,b) = int(r)*65536 + int(g)*256 + int(b)
plot 'hist_test.dat' using (column(0)):2:(0.5):(rgb($3,$4,$5)):xticlabels(1) w boxes lc rgb variable;

Related

Drawing a simple polygon using Gnuplot with "angle signs"

Disclaimer: I am new to Gnuplot, and I need to plot some "simple" things for my studies.
I want to plot a part of a polygon with some names and vectors added.
The picutre below was created with Euklid Dynageo, and I am now trying to create this with Gnuplot.
The biggest problem I am facing right now is the labeling u,v,w and adding the angles to the plot. I think I would use vectors and lines.
Do you now a 'simple' way to create this plot?
If you really need to use Gnuplot for this, you have to make it all manually by placing various objects, labels and arrows (keep in mind that complex plots will be cumbersome). A minimal example for two arrows and alpha_1, similar like in your example, could like like this:
# two arrows:
set arrow 1 from 0,0 to sqrt(2)/2,sqrt(2)/2
set arrow 2 from 0,0 to 1,0
# the alpha_1 symbol:
set label 1 '{/Symbol a}_1' at 0.2,0.1 front
# the filled yellow arc (from 0 to 45deg):
set style fill solid 1.0 border
set object 1 circle at 0,0 radius 0.2 arc[0:45] fc rgb "yellow"
# proper ratio, range, and plot 'something':
set xrange[-0.1:1.1]
set yrange[-0.1:1.1]
set size ratio 1
plot 1/0
Lookup the manual for possible object properties.
gnuplot is a very versatile plotting tool, but certainly not optimized for such tasks. For this type of drawing maybe Inkscape or others tools might be better choices, especially for interactive drawing by clicking, dragging and snapping.
And as #JackGlasshard already mentioned, of course you can do such graphs with gnuplot which will be rather time consuming if you do it manually.
Alternatively, you can facilitate the work if you use a "template".
Creating such a template probably only makes sense if you need to create more than one drawing.
Just for fun and feasibility, I tried to create such a template for general use to make such graphs easier.
For the input data you need 3 datablocks (without headers)
$Points: # no., x, y, label, xoff, yoff, pt, ps, color
you define: point number, x-coordinate, y-coordinate, point label, x-offset, y-offset, pointtype, pointsize, point color
$Vectors: # p1, p2, label, arrow, lw, dt, xoff, yoff, color
you define: first point number, second point number, label, arrowsstyle (-1=backhead, 0=nohead, 1=head, 2=heads), linewidth, dashtype, x-offset, y-offset, vector color
$Angles: # p1, p2, p3, r, label, aoff, roff, color
you define: 1st point number, 2nd point number (=angle center point), 3rd point number, radius, label, angular label offset, radial label offset, color
Now, you only have to change the data part of the script for your custom graph. In case you want to insert "invisible" vector starting or end points, simply set pointsize to 0 in $Points. The plotting of the variable arrowheads is a bit cumbersome because of the issue mentioned in this question.
For sure, more features and more flexibility can be added to the template. No warranty that the script is free of bugs. There is certainly room for improvements.
Update: (linewidth and dashtype added)
Since there is no variable linewidth (lw var) and no variable dashtype (dt var)
you have to plot each line separately in a for loop and every ::n::n.
Furthermore, I noticed that the order of variable pointsize (ps var) and and variable pointtype (pt var) apparently has changed from gnuplot 5.2 to 5.4. Not sure whether this intentional or a "bug".
The version below has the order for gnuplot 5.4.
Script:
### drawing sketch with points, vectors and angles
reset session
# no., x, y, label, xoff, yoff, pt, ps, color
$Points <<EOD
1 1 1 "" 0 0 5 1 0xff0000
2 10 2 V_{i-1} 0 0 5 1 0xff0000
3 15 10 V_i 0 0 5 1 0xff0000
4 4 12 X -1.5 0 5 1 0xff0000
5 14 16 V_{i+1} 0 0 5 1 0xff0000
6 5 20 "" 0 0 5 1 0xff0000
EOD
# p1, p2, label, arrow, lw, dt, xoff, yoff, color
# arrows: -1=backhead, 0=nohead, 1=head, 2=heads
$Vectors <<EOD
1 2 "" 0 1.0 1 0 0 0xff0000
2 3 "" 0 2.0 1 0 0 0x000000
3 5 "" 0 1.5 3 0 0 0x000000
4 2 w 1 1.0 1 1.0 0 0x000000
4 3 v 1 1.0 1 0 0.7 0x0000ff
4 5 u 1 1.0 1 0 0.7 0x000000
5 6 "" 0 1.0 1 0 0 0x000000
EOD
# p1, p2, p3, r, label, aoff, roff, color
# p2=angle center point
$Angles <<EOD
2 4 3 4.0 α_{i-1} 7.0 0.5 0xffcccc
3 4 5 4.5 α_i 3.0 0.7 0xffffcc
EOD
### end of data input
### start of template
# point/vector coordinates
px(n,m) = real(word($Points[int(word($Vectors[n],m))],2)) # x coordinate of point n
py(n,m) = real(word($Points[int(word($Vectors[n],m))],3)) # y coordinate of point n
vxd(n) = px(n,2)-px(n,1) # vector delta x
vyd(n) = py(n,2)-py(n,1) # vector delta y
vxc(n) = (px(n,2)+px(n,1))/2. # vector center x
vyc(n) = (py(n,2)+py(n,1))/2. # vector center y
lw(n) = real(word($Vectors[n],5))
dt(n) = int(word($Vectors[n],6))
# angles
as(n) = int(column(5)+2) # arrow style
ax(n) = real(word($Points[int(word($Angles[int(column(0)+1)],n))],2)) # angle center x
ay(n) = real(word($Points[int(word($Angles[int(column(0)+1)],n))],3)) # angle center y
set angle degrees
Angle(x0,y0,x1,y1) = (_dx=x1-x0, _dy=y1-y0, _L=sqrt(_dx**2 + _dy**2), _L==0 ? NaN : \
(_dy>=0 ? acos(_dx/_L) : -acos(_dx/_L) ))
a1(m) = Angle(ax(2),ay(2),ax(1),ay(1)) # starting angle
a2(m) = Angle(ax(2),ay(2),ax(3),ay(3)) # end angle
set style arrow 1 backhead filled # -1
set style arrow 2 nohead filled # 0
set style arrow 3 head filled # 1
set style arrow 4 heads filled # 2
set size ratio -1 # ensure same x- and y-ratio
set key noautotitle
set xrange [0:22]
set yrange [0:22]
set mxtics 5
set mytics 5
set grid x,y,mx,my
set style fill solid 0.5 border lc "black"
plot $Angles u (ax(2)):(ay(2)):4:(a1(0)):(a2(0)):8 w circles lc rgb var, \
'' u (ax(2)+($4*0.5+$7)*cos(0.5*(a1(0)+a2(0))+$6)): \
(ay(2)+($4*0.5+$7)*sin(0.5*(a1(0)+a2(0))+$6)):5 w labels font "Times New Roman,13" center, \
for [i=1:|$Vectors|] $Vectors \
u (px(i,1)):(py(i,1)):(vxd(i)):($4==-1?vyd(i):NaN):9 every ::i-1::i-1 w vectors lc rgb var lw lw(i) dt dt(i) filled backhead, \
for [i=1:|$Vectors|] '' \
u (px(i,1)):(py(i,1)):(vxd(i)):($4== 0?vyd(i):NaN):9 every ::i-1::i-1 w vectors lc rgb var lw lw(i) dt dt(i) filled nohead, \
for [i=1:|$Vectors|] '' \
u (px(i,1)):(py(i,1)):(vxd(i)):($4== 1?vyd(i):NaN):9 every ::i-1::i-1 w vectors lc rgb var lw lw(i) dt dt(i) filled head, \
for [i=1:|$Vectors|] '' \
u (px(i,1)):(py(i,1)):(vxd(i)):($4== 2?vyd(i):NaN):9 every ::i-1::i-1 w vectors lc rgb var lw lw(i) dt dt(i) filled heads, \
for [i=1:|$Vectors|] '' \
u (vxc(i)+$7):(vyc(i)+$8):3:9 every ::i-1::i-1 w labels font ",12" tc rgb var, \
$Points u 2:3:7:8:9 w p ps var pt var lc rgb var, \
'' u ($2+$5):($3+$6):4:9 w labels tc rgb var font ",12" left offset 1,0
### end of script
Result:

How to omit values in a threshold when plotting

I am trying to plot data using gnuplot. My data is in a matrix, i.e.:
-2 1 2
0 2 -5
3 0 1
For clarity I would like to assign all values in the threshold -2:2 white color, or assign them as missing. Is there an easy way to do it?
That depends on your actual plotting style and how you encode the color.
When plotting with image you can simply mark all values inside your range as invalid (as 1/0), and then they get plotted as white:
$data <<EOD
-2 1 2
0 2 -5
3 0 1
EOD
plot $data matrix u 1:2:($3 >= -2 && $3 <= 2 ? 1/0 : $3) with image notitle

producing variable colors with decimal numbers

I created a file which looks like where the first column is the color line in decimal and the second column is y-axis. The x-axis is the row number.
0 0
1 1
2 2
...
Then I run this command
plot "test.dat" u 0:2:1 pt 7 ps 1 lc rgb variable
As you can see in the picture, the output contains a range of black to blue colors only.
Why?
How can I produce other colors?
Basically you have the choice between three options, linecolor rgb variable, linecolor variable and linecolor palette. Which one you use and how depends on your actual requirements.
When you use linecolor rgb variable, the value given in the last column is used as integer representation of an rgb-tuple, i.e. the lowest byte is the blue part, the second lowest the green part and the third byte is the red component. This is what you have.
For using this option you must either have the full rgb-integer values in your data file, like
0 13.5 # black
0 17
65280 12 # green (255 * 2**8)
0 19.3
16711680 14.7 # red (255 * 2**16)
65280 10
16711680 22
and then use
plot 'test.txt' using 0:2:1 linecolor rgb variable pt 7
Alternatively you save the red, green and blue components in one column each and use a gnuplot function to calculate the rgb-integer:
0 0 0 13.5 # black
0 0 0 17
0 255 0 12 # green
0 0 0 19.3
255 0 0 14.7 # red (255 * 2**16)
0 255 0 10
255 0 0 22
and then use
rgb(r,g,b) = 65536 * int(r) + 256 * int(g) + int(b)
plot 'test.txt' using 0:4:(rgb($1,$2,$3)) linecolor rgb variable pt 7
Using linecolor variable would use the last column as linetype index. Large indices are wrapped to the set of defined linetype:
set xrange [0:1000]
plot '+' using 1:1:0 linecolor variable pt 7
Using linecolor palette uses the last column as index for the color palette:
set xrange [0:1000]
plot '+' using 1:1:0 linecolor palette pt 7
Which variant you use may depend both on the number of different colors and the distribution of the colors.

gnuplot: how to label points in multiplot

I want to label points on the plot using 'with labels' command but I get 'not enough columns for this style' error. My datafile looks like this:
method ∆G
A 0
B 15.01
C -1.4
D 12.2
E -3.9
method ∆H
A 0
B 8.4
C -2.58
D 3.6
E -2.12
method ∆SCF
A 0
B 11.66
C -0.96
D 6.28
E -1.3
I use this command to create a plot:
plot 'file.dat' using 2:xticlabel(1) index 0 pointtype 18 wth labels,''using 2 index 1 pointtype 18 with labels,''using 2 index 2 pointtype 18 with labels
I know that 'with labels' command requires 3 arguments, so the problem is probably lack of the third argument, but I've checked many combination and there was always some error. What am I doing wrong?
For your "missing" 3rd column you can use the pseudocolumn 0 (check help pseudocolumns).
If you want to address blocks via index you need to have two empty lines as separator. If you change your datafile format a bit it will make your script simpler.
The labels will have an offset depending on the sign. You can achieve this with the ternary operator, e.g. ($2<0?-1:1), (check help ternary). You could add another fixed offset via offset (check help label).
Data: SO24044166.dat
method ∆G
A 0
B 15.01
C -1.4
D 12.2
E -3.9
method ∆H
A 0
B 8.4
C -2.58
D 3.6
E -2.12
method ∆SCF
A 0
B 11.66
C -0.96
D 6.28
E -1.3
Script: (works with gnuplot 4.6.0, March 2012)
### use of pseudocolumn as x-value
reset
FILE = "SO24044166.dat"
set key title "methods" noautotitle
set style fill solid 0.3
set grid y
set yrange[-6:18]
set boxwidth 0.23
plot for [i=0:2] FILE u ($0+i/3.5):2 index i w boxes ti columnhead, \
for [i=0:2] '' u ($0+i/3.5):($2+($2<0?-1:1)):2 index i every ::1 w labels offset 0,0 notitle, \
'' u ($0+1/3.5):(NaN):xtic(1) index 0 every ::1
### end of script
Result: (created with gnuplot 4.6.0)

Gnuplot different colors

I'm trying to color a plot and a fit in gnuplot in different colors, but it doesn't work:
set ylabel "s in m"
set xlabel "t in s"
unset key
set style line 1 lt 2 lc rgb "red" lw 3
set style line 2 lt 2 lc rgb "orange" lw 2
plot "-" with lines ls1
0 0
1 4.2
2 7.9
3 11.7
4 16.3
fit "-" with lines ls2
0 0
1 4.2
2 7.9
3 11.7
4 16.3
Does anybody have an idea what I am doing wrong?
There are several things you are doing wrong:
The fit command is a bit different from the plot command. You must define a function like f(x) = a*x + b and fit this to your data. This calculates appropriate values for a and b. Afterwards you can plot the function.
You must terminate the inline data with an e.
To select a line style, use ls 1 (with the space before the number).
So your script should look as follows:
set ylabel "s in m"
set xlabel "t in s"
unset key
set style line 1 lt 2 lc rgb "red" lw 3
set style line 2 lt 2 lc rgb "orange" lw 2
f(x) = a*x + b
fit f(x) '-' via a,b
0 0
1 4.2
2 7.9
3 11.7
4 16.3
e
plot f(x) with lines ls 2, "-" with points ls 1
0 0
1 4.2
2 7.9
3 11.7
4 16.3
e
This plots your fit as a line, and the according data as points.

Resources