# Adding a second color bar to a plot

Here comes a sample set of data

data = {{1.03971, 0.00617284, 12.11, 0.227037, 1},
{1.0231, 0.0216931, 27.23, 1.40703, 1},
{1.02864, -0.0294533, 1450, 0.0821789, 9},
{1.01636, -0.0149912, 16.89, 1.02227, 1},
{0.938865, -0.00899471, 68.94, 0.0000322202, 9},
{0.955953, 0.0199295, 1.89, 0.00448189, 9},
{1.03321, 0.0329806, 84.23, 0.094761, 1},
{1.02406, 0.0393298, 13.21, 0.099565, 2},
{0.9728, -0.00194004, 1000, 0.000845306, 0},
{1.00649, -0.00582011, 62.1, 0.0286736, 1}};


The first two columns correspond to the coordinates $(x,y)$, while the other three are some indicators. Now I want the following:

Create a plot of the $(x,y)$ points where the color is according to the value of the third column. In particular

• All points for which the fifth element is 1 should be colored in a blue tone (DeepSeaColors), where dark blue indicate high values of the third column.
• All points for which the fifth element is 9 should be colored in a Rainbow tone, where light reddish color should suggest low values of the third column
• All points for which the fifth element is not 1 or 9 should be colored in white or transparent color.

First we select the corresponding points

data1 = Select[data, #[[5]] == 1 &];
data9 = Select[data, #[[5]] == 9 &];


Then we re-scale them and we also define a color function

valrange1 = {0, 100};
valrange9 = {0, 10};
data1[[All, 3]] = Rescale[data1[[All, 3]] // N, valrange1];
data9[[All, 3]] = Rescale[data9[[All, 3]] // N, valrange9];
colfunc[x_, cf_] := ColorData[cf][1 - x[[3]]];


The respective graphics are

g1 = Graphics[{PointSize[0.005], Point[#[[1 ;; 2]], VertexColors -> colfunc[#, "Rainbow"]] & /@ data1}];
g9 = Graphics[{PointSize[0.005], Point[#[[1 ;; 2]], VertexColors -> colfunc[#, "DeepSeaColors"]] & /@ data9}];


Finally we show them together (this is the plot of the actual data set)

Obviously there is something missing. In fact we need two color bars in order to explain the different colors.

First a vertical color bar corresponding to points with fifth column equal to 1.

Clear[colorbar]
colorbar[{min_, max_}, colorFunction_: Automatic, divs_: 150] :=
DensityPlot[y, {x, 0, 0.1}, {y, min, max}, AspectRatio -> 10,
PlotRangePadding -> 0, PlotPoints -> {2, divs}, MaxRecursion -> 0,
Frame -> True,
FrameLabel -> {{None, Row[{Subscript["t", "esc"]}]}, {None, None}},
LabelStyle -> Directive[FontFamily -> "Helvetica", 20],
FrameTicks -> {{None, All}, {None, None}},
FrameTicksStyle -> Directive[FontFamily -> "Helvetica", 20, Plain],
ColorFunction -> colorFunction]


Then

With[{opts = {ImageSize -> {Automatic, 400}}, cf = "Rainbow"},
Row[{Show[{g1, g9}, Axes -> False, Frame -> True,
FrameLabel -> {"x", "y"}, RotateLabel -> False,
LabelStyle -> Directive[FontFamily -> "Helvetica", 20],
AspectRatio -> 3/4, ImagePadding -> {{90, 10}, {60, 40}}, opts],
Show[colorbar[valrange1, ColorData[cf][1 - #] &],
ImagePadding -> {{10, 80}, {60, 40}}, opts]}]]


which gives

Now I want the following:

Add a second horizontal color bar at the upper part of the frame corresponding to the DeepSeaColors of points with fifth column equal to 9. The range of this color bar should go from 0 to 10, while the title above it should read t_col. Note that the width of the color bar should match the width of the frame.

Any ideas?

• maybe Show[Legended[g1, BarLegend["Rainbow", 1 - data1[[All, 3]]]], Legended[g9, Placed[BarLegend["DeepSeaColors", 1 - data9[[All, 3]], LegendLayout -> "Row"], Top]]]?
– kglr
Mar 5, 2017 at 16:10
• @kglr Not exactly! The ranges of the two color bars are wrong, and also the colors (dark color correspond to low values). Moreover the size of the color bars do not match the size (width and height) of the bounding box. Mar 5, 2017 at 16:14
• @kglr Please see my edit. Mar 5, 2017 at 16:23
• It would be possible using SciDraw. There, you can define a Multipanel of 2x2 dimensions and place the color bars in {1,1} and {2,2}. They are guaranteed to align well with your frame. Mar 5, 2017 at 16:53

OK, this is my try

First the definition of the vertical color bar

Clear[colorbar1]
colorbar1[{min_, max_}, colorFunction_: Automatic, divs_: 150] :=
DensityPlot[y, {x, 0, 0.1}, {y, min, max}, AspectRatio -> 10,
PlotRangePadding -> 0, PlotPoints -> {2, divs}, MaxRecursion -> 0,
Frame -> True,
FrameLabel -> {{None, Row[{Subscript["t", "esc"]}]}, {None, None}},
LabelStyle -> Directive[FontFamily -> "Helvetica", 20],
FrameTicks -> {{None, All}, {None, None}},
FrameTicksStyle -> Directive[FontFamily -> "Helvetica", 20, Plain],
ColorFunction -> colorFunction]


Then the definition of the horizontal color bar

Clear[colorbar2]
colorbar2[{min_, max_}, colorFunction_: Automatic, divs_: 150] :=
DensityPlot[x, {x, min, max}, {y, 0, 0.1}, AspectRatio -> 1/13.5,
PlotRangePadding -> 0, PlotPoints -> {divs, 2}, MaxRecursion -> 0,
Frame -> True,
FrameLabel -> {{None, None}, {None, Row[{Subscript["t", "col"]}]}},
LabelStyle -> Directive[FontFamily -> "Helvetica", 20],
FrameTicks -> {{None, None}, {All, None}},
FrameTicksStyle -> Directive[FontFamily -> "Helvetica", 20, Plain],
ColorFunction -> colorFunction]


Finally we show together the graphics with the color bars

With[{opts = {ImageSize -> {Automatic, 400}}, cf1 = "Rainbow",
cf2 = "DeepSeaColors"},
Column[{Show[colorbar2[valrange9, ColorData[cf2][1 - #] &],
ImagePadding -> {{90, 70}, {25, 40}}, ImageSize -> 600],
Row[{Show[{g1, g9}, Axes -> False, Frame -> True,
FrameLabel -> {"x", "y"}, RotateLabel -> False,
LabelStyle -> Directive[FontFamily -> "Helvetica", 20],
AspectRatio -> 3/4, ImagePadding -> {{90, 20}, {60, 10}},
opts], Show[colorbar1[valrange1, ColorData[cf1][1 - #] &],
ImagePadding -> {{10, 80}, {60, 10}}, opts]}]}]]


which gives the following beautiful image