change box colors in bar graph

  • Follow


I am relatively new to gnuplot and I'm trying to plot a bar graph with
boxerrorbars such that the color of the box depends on the value of a
certain variable.  If the variable is greater than 20, for example,
the bar will be blue, but if less than 20, the bar will be light
blue.  Is there any way to control the color of the bars in this
fashion using boxerrorbars?

Also, I would like to change the color of the error bars in a similar
fashion: if a given variable is greater than, say, 50, the error bar
should be red, but black otherwise.

Thanks,

Mike
0
Reply gnubie2 (2) 2/10/2010 6:04:21 PM

mikegnubie wrote:

> I am relatively new to gnuplot and I'm trying to plot a bar graph with
> boxerrorbars such that the color of the box depends on the value of a
> certain variable.  If the variable is greater than 20, for example,
> the bar will be blue, but if less than 20, the bar will be light
> blue.  Is there any way to control the color of the bars in this
> fashion using boxerrorbars?

There is a variable color option, but unfortunately it is not part of the
boxerrorbars plot style.  You would have to create this plot in two passes.
If you only need two colors it is probably easiest to do each color
in a separate pass:

plot 'data' using 1:($2>20 ? $2 : NaN):3 with boxerrorbars lc rgb "blue", \
     'data' using 1:($2>20 ? NaN : $2):3 with boxerrorbars lc rgb "cyan"

If you need a larger palette of colors then it is better to use one pass
to fill in the colors from an arbitrary palette, and a second pass to 
draw the errorbars:

First pass:  draw only the boxes, with solid fill variable color.
Second pass: draw boxerrorbars with no fill, 
             letting the previous color show through

Here is an example:
blue = 0x0000ff         # RGB components for pure blut
cyan = 0x00ffff         # RGB components for cyan
red = 0xff0000          # RGB components for red

set boxwidth 1.0

plot 'data' using 1:2:($2>20 ? blue : $2>10 ? cyan : red) \
            with boxes fillstyle solid lc rgb variable, \
     'data' using 1:2:3 with boxerrorbars fillstyle empty

> Also, I would like to change the color of the error bars in a similar
> fashion: if a given variable is greater than, say, 50, the error bar
> should be red, but black otherwise.

For this you would have to combine both of the above approaches.
Left as an exercise for the reader :-)
0
Reply sfeam 2/10/2010 7:48:03 PM


On Feb 10, 11:48=A0am, sfeam <sf...@users.sourceforge.net> wrote:
> mikegnubie wrote:
> > I am relatively new to gnuplot and I'm trying to plot a bar graph with
> > boxerrorbars such that the color of the box depends on the value of a
> > certain variable. =A0If the variable is greater than 20, for example,
> > the bar will be blue, but if less than 20, the bar will be light
> > blue. =A0Is there any way to control the color of the bars in this
> > fashion using boxerrorbars?
>
> There is a variable color option, but unfortunately it is not part of the
> boxerrorbars plot style. =A0You would have to create this plot in two pas=
ses.
> If you only need two colors it is probably easiest to do each color
> in a separate pass:
>
> plot 'data' using 1:($2>20 ? $2 : NaN):3 with boxerrorbars lc rgb "blue",=
 \
> =A0 =A0 =A0'data' using 1:($2>20 ? NaN : $2):3 with boxerrorbars lc rgb "=
cyan"
>
> If you need a larger palette of colors then it is better to use one pass
> to fill in the colors from an arbitrary palette, and a second pass to
> draw the errorbars:
>
> First pass: =A0draw only the boxes, with solid fill variable color.
> Second pass: draw boxerrorbars with no fill,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0letting the previous color show through
>
> Here is an example:
> blue =3D 0x0000ff =A0 =A0 =A0 =A0 # RGB components for pure blut
> cyan =3D 0x00ffff =A0 =A0 =A0 =A0 # RGB components for cyan
> red =3D 0xff0000 =A0 =A0 =A0 =A0 =A0# RGB components for red
>
> set boxwidth 1.0
>
> plot 'data' using 1:2:($2>20 ? blue : $2>10 ? cyan : red) \
> =A0 =A0 =A0 =A0 =A0 =A0 with boxes fillstyle solid lc rgb variable, \
> =A0 =A0 =A0'data' using 1:2:3 with boxerrorbars fillstyle empty
>
> > Also, I would like to change the color of the error bars in a similar
> > fashion: if a given variable is greater than, say, 50, the error bar
> > should be red, but black otherwise.
>
> For this you would have to combine both of the above approaches.
> Left as an exercise for the reader :-)

Thanks very much, sfeam.  I really liked your first suggestion,
regarding the box colors, but haven't tried to implement the second
one yet.  I got a very strange result when I tried your first
suggestion.  Even though all of the blue bars appeared correctly, some
of the cyan bars displayed as very slender lines (=3D zero-width boxes?)
instead of as normal-width boxes.  So far I haven't been able to
discern a pattern, however.  Any idea what might be causing this?
BTW, I forgot to mention in my first post that I'm using gnuplot 4.2.5
on Mac OS X 10.4.
0
Reply mikegnubie 2/10/2010 9:57:38 PM

mikegnubie wrote:

> On Feb 10, 11:48 am, sfeam <sf...@users.sourceforge.net> wrote:
>> mikegnubie wrote:
>> > I am relatively new to gnuplot and I'm trying to plot a bar graph with
>> > boxerrorbars such that the color of the box depends on the value of a
>> > certain variable.  If the variable is greater than 20, for example,
>> > the bar will be blue, but if less than 20, the bar will be light
>> > blue.  Is there any way to control the color of the bars in this
>> > fashion using boxerrorbars?
>>
>> There is a variable color option, but unfortunately it is not part of the
>> boxerrorbars plot style.  You would have to create this plot in two
>> passes. If you only need two colors it is probably easiest to do each
>> color in a separate pass:
>>
>> plot 'data' using 1:($2>20 ? $2 : NaN):3 with boxerrorbars lc rgb "blue",
>> \ 'data' using 1:($2>20 ? NaN : $2):3 with boxerrorbars lc rgb "cyan"
>>
>> If you need a larger palette of colors then it is better to use one pass
>> to fill in the colors from an arbitrary palette, and a second pass to
>> draw the errorbars:
>>
>> First pass:  draw only the boxes, with solid fill variable color.
>> Second pass: draw boxerrorbars with no fill,
>> letting the previous color show through
>>
>> Here is an example:
>> blue = 0x0000ff         # RGB components for pure blut
>> cyan = 0x00ffff         # RGB components for cyan
>> red = 0xff0000          # RGB components for red
>>
>> set boxwidth 1.0
>>
>> plot 'data' using 1:2:($2>20 ? blue : $2>10 ? cyan : red) \
>> with boxes fillstyle solid lc rgb variable, \
>> 'data' using 1:2:3 with boxerrorbars fillstyle empty
>>
>> > Also, I would like to change the color of the error bars in a similar
>> > fashion: if a given variable is greater than, say, 50, the error bar
>> > should be red, but black otherwise.
>>
>> For this you would have to combine both of the above approaches.
>> Left as an exercise for the reader :-)
> 
> Thanks very much, sfeam.  I really liked your first suggestion,
> regarding the box colors, but haven't tried to implement the second
> one yet.  I got a very strange result when I tried your first
> suggestion.  Even though all of the blue bars appeared correctly, some
> of the cyan bars displayed as very slender lines (= zero-width boxes?)
> instead of as normal-width boxes.  So far I haven't been able to
> discern a pattern, however.  Any idea what might be causing this?
> BTW, I forgot to mention in my first post that I'm using gnuplot 4.2.5
> on Mac OS X 10.4.

I left out any preliminary commands that would control your box sizes.
I assume you know how you want that to work?  You can either provide
an explicit width for each box, or specify a uniform width for all or them:

    set boxwidth <whatever> absolute

Stay away from "relative" boxwidths if you are doing multiple passes,
because the neighboring boxes may belong to a different pass.
0
Reply sfeam 2/10/2010 11:05:16 PM

On Feb 10, 3:05=A0pm, sfeam <sf...@users.sourceforge.net> wrote:
> mikegnubie wrote:
> > On Feb 10, 11:48=A0am, sfeam <sf...@users.sourceforge.net> wrote:
> >> mikegnubie wrote:
> >> > I am relatively new to gnuplot and I'm trying to plot a bar graph wi=
th
> >> > boxerrorbars such that the color of the box depends on the value of =
a
> >> > certain variable. =A0If the variable is greater than 20, for example=
,
> >> > the bar will be blue, but if less than 20, the bar will be light
> >> > blue. =A0Is there any way to control the color of the bars in this
> >> > fashion using boxerrorbars?
>
> >> There is a variable color option, but unfortunately it is not part of =
the
> >> boxerrorbars plot style. =A0You would have to create this plot in two
> >> passes. If you only need two colors it is probably easiest to do each
> >> color in a separate pass:
>
> >> plot 'data' using 1:($2>20 ? $2 : NaN):3 with boxerrorbars lc rgb "blu=
e",
> >> \ 'data' using 1:($2>20 ? NaN : $2):3 with boxerrorbars lc rgb "cyan"
>
> >> If you need a larger palette of colors then it is better to use one pa=
ss
> >> to fill in the colors from an arbitrary palette, and a second pass to
> >> draw the errorbars:
>
> >> First pass: =A0draw only the boxes, with solid fill variable color.
> >> Second pass: draw boxerrorbars with no fill,
> >> letting the previous color show through
>
> >> Here is an example:
> >> blue =3D 0x0000ff =A0 =A0 =A0 =A0 # RGB components for pure blut
> >> cyan =3D 0x00ffff =A0 =A0 =A0 =A0 # RGB components for cyan
> >> red =3D 0xff0000 =A0 =A0 =A0 =A0 =A0# RGB components for red
>
> >> set boxwidth 1.0
>
> >> plot 'data' using 1:2:($2>20 ? blue : $2>10 ? cyan : red) \
> >> with boxes fillstyle solid lc rgb variable, \
> >> 'data' using 1:2:3 with boxerrorbars fillstyle empty
>
> >> > Also, I would like to change the color of the error bars in a simila=
r
> >> > fashion: if a given variable is greater than, say, 50, the error bar
> >> > should be red, but black otherwise.
>
> >> For this you would have to combine both of the above approaches.
> >> Left as an exercise for the reader :-)
>
> > Thanks very much, sfeam. =A0I really liked your first suggestion,
> > regarding the box colors, but haven't tried to implement the second
> > one yet. =A0I got a very strange result when I tried your first
> > suggestion. =A0Even though all of the blue bars appeared correctly, som=
e
> > of the cyan bars displayed as very slender lines (=3D zero-width boxes?=
)
> > instead of as normal-width boxes. =A0So far I haven't been able to
> > discern a pattern, however. =A0Any idea what might be causing this?
> > BTW, I forgot to mention in my first post that I'm using gnuplot 4.2.5
> > on Mac OS X 10.4.
>
> I left out any preliminary commands that would control your box sizes.
> I assume you know how you want that to work? =A0You can either provide
> an explicit width for each box, or specify a uniform width for all or the=
m:
>
> =A0 =A0 set boxwidth <whatever> absolute
>
> Stay away from "relative" boxwidths if you are doing multiple passes,
> because the neighboring boxes may belong to a different pass.

That makes sense, but doesn't seem to make a difference in this case.
I tried

     set boxwidth 1 absolute

and still get "lines" instead of boxes.
0
Reply mikegnubie 2/11/2010 12:02:05 AM

mikegnubie wrote:
> 
> That makes sense, but doesn't seem to make a difference in this case.
> I tried
> 
>      set boxwidth 1 absolute
> 
> and still get "lines" instead of boxes.

I think we need to see the exact plot command you are using.
0
Reply sfeam 2/11/2010 1:30:37 AM

On Feb 10, 5:30=A0pm, sfeam <sf...@users.sourceforge.net> wrote:
> mikegnubie wrote:
>
> > That makes sense, but doesn't seem to make a difference in this case.
> > I tried
>
> > =A0 =A0 =A0set boxwidth 1 absolute
>
> > and still get "lines" instead of boxes.
>
> I think we need to see the exact plot command you are using.

I'm sending everything, just in case the cause of the phenomenon is in
some other part of the command set.

Here is the plot file:

unset multiplot
reset
set terminal postscript landscape enhanced color font "ArialMT,16"
set style fill solid border -1
set boxwidth .8 absolute
set xtics border in scale 1,0.5 mirror rotate by 90 font "ArialMT,10"
set ytics font "ArialMT,16"
set key autotitle columnhead
set yrange [3.2:20000]
set logscale y
set grid xtics
set grid ytics
set format y "10^{%L}"
set offsets 1,1
set output "tmp.ps"
plot 'tmp.txt' u :($4<20 ? $2: NaN):3:xticlabels(1) w boxerrorbars t
"Eotaxin" lt 1 lw 1 lc rgb 'blue',\
'' u :($4<20 ? NaN : $2):3 w boxerrorbars notitle fs solid .5 lt 1 lw
1 lc rgb 'cyan'
set output
-----------EOF-------------

Here is the input file (tmp.txt).  It's not as large as the actual
file, but still generates similar output:

"PID"	"Eotaxin mean"	"sd"	"cv"
"0"	1.2	1.7	141.4
"3.2"	3.6	1.2	32.5
"16"	16.5	1.0	6.1
"80"	111.1	10.2	9.2
"400"	999.9	1056.8	105.7
"2000"	4537.7	13.9	0.3
"10000"	9656.4	97.1	1.0
"QC-low"	40.0	3.0	7.5
"QC-1"	202.2	215.9	106.8
"QC-2"	2445.8	278.0	11.4
"ID1"	179.0	12.7	7.1
"ID2"	63.8	18.0	28.2
--------------EOF----------------

-the first line contains the column labels
-subsequent lines contain data
-columns of data are delimited by tabs
-the first column of each data line contains a text identifier to be
used as a tic label on the x-axis
-the second column contains the y-value to be plotted
-the third column contains the standard deviation (used for the error
bar)
-the fourth column contains the %CV (if the %CV > 20, the box is
plotted in a lighter color
0
Reply mikegnubie 2/11/2010 4:47:43 AM

mikegnubie wrote:

> On Feb 10, 5:30 pm, sfeam <sf...@users.sourceforge.net> wrote:
>> mikegnubie wrote:
>>
>> > That makes sense, but doesn't seem to make a difference in this
>> > case. I tried
>>
>> > set boxwidth 1 absolute
>>
>> > and still get "lines" instead of boxes.
>>
>> I think we need to see the exact plot command you are using.
> 
> I'm sending everything, just in case the cause of the phenomenon is in
> some other part of the command set.
> 
> Here is the plot file:
> 
> unset multiplot
> reset
> set terminal postscript landscape enhanced color font "ArialMT,16"
> set style fill solid border -1
> set boxwidth .8 absolute
> set xtics border in scale 1,0.5 mirror rotate by 90 font "ArialMT,10"
> set ytics font "ArialMT,16"
> set key autotitle columnhead
> set yrange [3.2:20000]
> set logscale y
> set grid xtics
> set grid ytics
> set format y "10^{%L}"
> set offsets 1,1
> set output "tmp.ps"
> plot 'tmp.txt' u :($4<20 ? $2: NaN):3:xticlabels(1) w boxerrorbars t
> "Eotaxin" lt 1 lw 1 lc rgb 'blue',\
> '' u :($4<20 ? NaN : $2):3 w boxerrorbars notitle fs solid .5 lt 1 lw
> 1 lc rgb 'cyan'
> set output

Weird. I don't know.
But I can give you a work-around while I think about it.

Add an additional column to each command, containing the boxwidth:

plot 'tmp.txt' u :($4<20 ? $2: NaN):3:(.8):xticlabels(1)...
            '' u :($4<20 ? NaN : $2):3:(.8) ...











> -----------EOF-------------
> 
> Here is the input file (tmp.txt).  It's not as large as the actual
> file, but still generates similar output:
> 
> "PID"	"Eotaxin mean"	"sd"	"cv"
> "0"	1.2	1.7	141.4
> "3.2"	3.6	1.2	32.5
> "16"	16.5	1.0	6.1
> "80"	111.1	10.2	9.2
> "400"	999.9	1056.8	105.7
> "2000"	4537.7	13.9	0.3
> "10000"	9656.4	97.1	1.0
> "QC-low"	40.0	3.0	7.5
> "QC-1"	202.2	215.9	106.8
> "QC-2"	2445.8	278.0	11.4
> "ID1"	179.0	12.7	7.1
> "ID2"	63.8	18.0	28.2
> --------------EOF----------------
> 
> -the first line contains the column labels
> -subsequent lines contain data
> -columns of data are delimited by tabs
> -the first column of each data line contains a text identifier to be
> used as a tic label on the x-axis
> -the second column contains the y-value to be plotted
> -the third column contains the standard deviation (used for the error
> bar)
> -the fourth column contains the %CV (if the %CV > 20, the box is
> plotted in a lighter color

0
Reply sfeam 2/11/2010 6:50:54 AM

On Feb 10, 10:50=A0pm, sfeam <sf...@users.sourceforge.net> wrote:
> mikegnubie wrote:
> > On Feb 10, 5:30 pm, sfeam <sf...@users.sourceforge.net> wrote:
> >> mikegnubie wrote:
>
> >> > That makes sense, but doesn't seem to make a difference in this
> >> > case. I tried
>
> >> > set boxwidth 1 absolute
>
> >> > and still get "lines" instead of boxes.
>
> >> I think we need to see the exact plot command you are using.
>
> > I'm sending everything, just in case the cause of the phenomenon is in
> > some other part of the command set.
>
> > Here is the plot file:
>
> > unset multiplot
> > reset
> > set terminal postscript landscape enhanced color font "ArialMT,16"
> > set style fill solid border -1
> > set boxwidth .8 absolute
> > set xtics border in scale 1,0.5 mirror rotate by 90 font "ArialMT,10"
> > set ytics font "ArialMT,16"
> > set key autotitle columnhead
> > set yrange [3.2:20000]
> > set logscale y
> > set grid xtics
> > set grid ytics
> > set format y "10^{%L}"
> > set offsets 1,1
> > set output "tmp.ps"
> > plot 'tmp.txt' u :($4<20 ? $2: NaN):3:xticlabels(1) w boxerrorbars t
> > "Eotaxin" lt 1 lw 1 lc rgb 'blue',\
> > '' u :($4<20 ? NaN : $2):3 w boxerrorbars notitle fs solid .5 lt 1 lw
> > 1 lc rgb 'cyan'
> > set output
>
> Weird. I don't know.
> But I can give you a work-around while I think about it.
>
> Add an additional column to each command, containing the boxwidth:
>
> plot 'tmp.txt' u :($4<20 ? $2: NaN):3:(.8):xticlabels(1)...
> =A0 =A0 =A0 =A0 =A0 =A0 '' u :($4<20 ? NaN : $2):3:(.8) ...
>
> > -----------EOF-------------
>
> > Here is the input file (tmp.txt). =A0It's not as large as the actual
> > file, but still generates similar output:
>
> > "PID" =A0 =A0"Eotaxin mean" =A0 =A0 =A0 =A0"sd" =A0"cv"
> > "0" =A0 =A0 =A01.2 =A0 =A0 1.7 =A0 =A0 141.4
> > "3.2" =A0 =A03.6 =A0 =A0 1.2 =A0 =A0 32.5
> > "16" =A0 =A0 16.5 =A0 =A01.0 =A0 =A0 6.1
> > "80" =A0 =A0 111.1 =A0 10.2 =A0 =A09.2
> > "400" =A0 =A0999.9 =A0 1056.8 =A0105.7
> > "2000" =A0 4537.7 =A013.9 =A0 =A00.3
> > "10000" =A09656.4 =A097.1 =A0 =A01.0
> > "QC-low" 40.0 =A0 =A03.0 =A0 =A0 7.5
> > "QC-1" =A0 202.2 =A0 215.9 =A0 106.8
> > "QC-2" =A0 2445.8 =A0278.0 =A0 11.4
> > "ID1" =A0 =A0179.0 =A0 12.7 =A0 =A07.1
> > "ID2" =A0 =A063.8 =A0 =A018.0 =A0 =A028.2
> > --------------EOF----------------
>
> > -the first line contains the column labels
> > -subsequent lines contain data
> > -columns of data are delimited by tabs
> > -the first column of each data line contains a text identifier to be
> > used as a tic label on the x-axis
> > -the second column contains the y-value to be plotted
> > -the third column contains the standard deviation (used for the error
> > bar)
> > -the fourth column contains the %CV (if the %CV > 20, the box is
> > plotted in a lighter color

Explicitly specifying the column width worked.  Thanks very much.  I
really learned a lot.
0
Reply mikegnubie 2/11/2010 7:12:43 AM

8 Replies
6273 Views

(page loaded in 0.149 seconds)

Similiar Articles:













7/19/2012 3:19:35 PM


Reply: