Reputation: 41
I'm looking to create a bar graph using gnuplot that looks very similar to the following. I have the raw data for the "Technology": "-0.72%" and so on in array.
I have gotten it up to horizontally drawing the graphs, but adjusting and making sure that the final image looks like seems to be beyond me. Any pointers are appreciated ?
Upvotes: 0
Views: 786
Reputation: 25684
Here is a sugggestion using the plotting style boxxyerror
(check help boxxyerror
).
The color is chosen depending on the column 2 values using the ternary operator (check help ternary
).
The values are plotted as numbers using plotting style with labels
and the alignment of the numbers which is either right
for negative numbers and left
for positive numbers is "filtered" using the ternary operator.
Edit: Code changed such that it will show +
-sign for positive numbers (and zero), and furthermore it will not matter whether your have the %
-sign in your data or not (it will add %
when plotting the label).
Code:
### Horizontal bar graph
reset session
$Data <<EOD
A -3.5%
B -1.3%
C 0.0%
D 1.5%
E 2.4%
F 5.3%
G 7.3%
EOD
myColor(col) = column(col)<0 ? 0xff0000 : column(col)==0 ? 0xcccccc : 0x00ff00
BoxWidth = 0.8
BoxYLow(i) = i - BoxWidth/2.
BoxYHigh(i) = i + BoxWidth/2.
set style fill transparent solid 0.3
set yrange [:] reverse
unset key
set offsets 1,1,0.5,0.5
plot $Data u (0):0:(0):2:(BoxYLow($0)):(BoxYHigh($0)):(myColor(2)):ytic(1) w boxxy lc rgb var, \
'' u ($2<0?$2:NaN) :0:(sprintf("%+g%",$2)) w labels offset -0.5,0 right, \
'' u ($2>=0?$2:NaN):0:(sprintf("%+g%",$2)) w labels offset 0.5,0 left
### end of code
Result:
Upvotes: 2