0
votes

I would like to create a color bar plot of a single variable that draws a box to the left in red if the variable is negative and green to the right if positive.

I am failing to get rid of the y axis completely. There shall be no marking of it whatsoever. Second the x-axis and tics is hidden behind the box. I need it visible. Third the plot of the line at 0 is really unnecessary as I have already drawn all I need but gnuplot wants a plot cmd with some sort of argument. I tried plot 0 lt bgnd but that left an ugly white line in my box. I guess I can live with that. Arrows at the ends of the x-axis would be nice, too.

This is the current state of the code. (the variable v will later come from the outside world as command line argument)

v= 7.3
if (v<0){boxcolor= 'red'}
if (v>=0){boxcolor= 'green'}

unset border
unset ytics
unset key

set yzeroaxis
set xzeroaxis
set xtics axis
unset ytics 
set xrange [-10:10]

set object 1 rect from 0.0,-0.5 to v,0.5 back fillcolor rgb boxcolor

plot 0 

Result currently: enter image description here

2

2 Answers

1
votes

You are probably looking for something like this:

reset session
set border 0
unset key
unset ytics

v= 7.3
boxcolor = (v<0) ? 'red' : 'green'

set xtics axis nomirror 
xmin = -10
xmax = 10
set xrange [xmin:xmax]

set object 1 rect from 0.0,-0.5 to v,0.5 behind fillcolor rgb boxcolor
set arrow 1 from xmin-0.5,0 to xmax+0.5,0 heads filled
plot 0 

enter image description here

0
votes

Another answer: This one uses the built-in axis variants rather than an arrow:

set border 0
unset key
# In newer gnuplot versions there is a keyword "nodraw"
# Here we define a synonym that works with older versions also
  hide = -4
set yzeroaxis lt hide lc hide
set xzeroaxis lt black
set tics front
unset ytics
set xtics axis
# define rectangle here
  set object 1 rect from 0,-.5 to 5,.5 behind fs noborder fc "green"
#
plot 0 with lines lc "black"

enter image description here