2
$\begingroup$

I would like to get a BarLegend similar to this one with unequally spaced contours: enter image description here

Here is my code:

colors = {RGBColor[0.9529411764705882, 0.9686274509803922, 0.9215686274509803], 
RGBColor[0.9529411764705882, 0.9137254901960784, 0.8156862745098039], 
RGBColor[0.9803921568627451, 0.788235294117647, 0.7607843137254902], 
RGBColor[0.9568627450980391, 0.6196078431372549, 0.796078431372549], 
RGBColor[0.8235294117647058, 0.49019607843137253`, 0.8941176470588235], 
RGBColor[0.592156862745098, 0.4784313725490196, 0.984313725490196],
RGBColor[0.3333333333333333, 0.6, 0.9568627450980391],
RGBColor[0.16470588235294117`, 0.807843137254902, 0.7686274509803921], 
RGBColor[0.16862745098039217`, 0.9725490196078431, 0.43529411764705883`], 
RGBColor[0.38431372549019605`, 0.9215686274509803, 0.11764705882352941`], 
RGBColor[0.6078431372549019, 0.6470588235294118, 0.00392156862745098], 
RGBColor[0.6941176470588235, 0.33725490196078434`, 0.07058823529411765], 
RGBColor[0.615686274509804, 0.10980392156862745`, 0.22352941176470587`], 
RGBColor[0.4235294117647059, 0.01568627450980392, 0.34901960784313724`], 
RGBColor[0.21568627450980393`, 0.0392156862745098, 0.3764705882352941], 
RGBColor[0.07450980392156863, 0.10196078431372549`, 0.29411764705882354`], 
RGBColor[0.023529411764705882`, 0.12549019607843137`, 0.16470588235294117`],
RGBColor[0.027450980392156862`, 0.07450980392156863, 0.054901960784313725`]};

values = {0, 0.764828, 0.906574, 1.07459, 1.27374, 1.50981, 1.78962, 2.1213, 2.51444, 
2.98044, 3.53281, 4.18754, 4.96362, 5.88354, 6.97394, 8.26642, 9.79844, 11.6144, 14.8};

pairs = Partition[Riffle[values, colors],2];

The following piece of code provides BarLegend with continously varying colours :

Contbar = BarLegend[{Blend[pairs, #1] &, {0, 14.8}}, LegendLayout -> "Row"]

I tried "discontinous" version of BarLegend with visible contours:

Discbar = BarLegend[{Blend[pairs, #1] &, {0, 14.8}},values, LegendLayout -> "Row"]

but now the contours are equally spaced, which is not what I wanted.

I will be grateful for any help.

$\endgroup$

2 Answers 2

2
$\begingroup$

Maybe build it from scratch:

d1 = Module[{a=Partition[Insert[pairs[[All, 1]], values[[-1]], -1], 2, 1]},
Table[{pairs[[i, 2]],Rectangle[{a[[i, 1]], 0}, {a[[i, 2]], 1}]}, {i, 1, Length[a]}]];

and visualise adjusting tick sizes (if you want to!):

 barLeg = Graphics[d1, Axes -> {True, False}, PlotRange -> {{0, 14}, All}, BaseStyle -> 18, 
 ImageSize -> 700, Ticks -> {Join[{#, Column[{"", #}, Spacings -> 0], -0.02, 
 {Black, Thick}}& /@ Range[0, 14, 4], Table[{i, "", -0.01, {Black, Thick}}, {i, 1, 14, 1}]], Automatic}]

enter image description here

$\endgroup$
0
$\begingroup$
Clear["Global`*"]
colors = {RGBColor[0.9529411764705882, 0.9686274509803922, 
    0.9215686274509803], 
   RGBColor[0.9529411764705882, 0.9137254901960784, 
    0.8156862745098039], 
   RGBColor[0.9803921568627451, 0.788235294117647, 
    0.7607843137254902], 
   RGBColor[0.9568627450980391, 0.6196078431372549, 
    0.796078431372549], 
   RGBColor[0.8235294117647058, 0.49019607843137253`, 
    0.8941176470588235], 
   RGBColor[0.592156862745098, 0.4784313725490196, 0.984313725490196],
    RGBColor[0.3333333333333333, 0.6, 0.9568627450980391], 
   RGBColor[0.16470588235294117`, 0.807843137254902, 
    0.7686274509803921], 
   RGBColor[0.16862745098039217`, 0.9725490196078431, 
    0.43529411764705883`], 
   RGBColor[0.38431372549019605`, 0.9215686274509803, 
    0.11764705882352941`], 
   RGBColor[0.6078431372549019, 0.6470588235294118, 
    0.00392156862745098], 
   RGBColor[0.6941176470588235, 0.33725490196078434`, 
    0.07058823529411765], 
   RGBColor[0.615686274509804, 0.10980392156862745`, 
    0.22352941176470587`], 
   RGBColor[0.4235294117647059, 0.01568627450980392, 
    0.34901960784313724`], 
   RGBColor[0.21568627450980393`, 0.0392156862745098, 
    0.3764705882352941], 
   RGBColor[0.07450980392156863, 0.10196078431372549`, 
    0.29411764705882354`], 
   RGBColor[0.023529411764705882`, 0.12549019607843137`, 
    0.16470588235294117`], 
   RGBColor[0.027450980392156862`, 0.07450980392156863, 
    0.054901960784313725`]};

values = {0, 0.764828, 0.906574, 1.07459, 1.27374, 1.50981, 1.78962, 
   2.1213, 2.51444, 2.98044, 3.53281, 4.18754, 4.96362, 5.88354, 
   6.97394, 8.26642, 9.79844, 11.6144, 14.8};

p = Transpose[{Partition[values, 2, 1], colors}]
r = ({Last@#, First@First@# <= x <= Last@First@#} & /@ p);
cf[x_] = Piecewise[r]

Taking advantage of a few undocumented options:

BarLegend[{cf, MinMax[values]}
 , LegendLayout -> "Row"
 , LegendMarkerSize -> {400, 30}
 , Ticks -> Transpose[{Range[Sequence @@ MinMax@values, 4]
    , ToString /@ Range[Sequence @@ MinMax@values, 4]
    }]
 , FrameStyle -> White
 ]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you for your answer, but it does not work for me. I got purely black and white 'BarLegend'. I am using Mathematica 12.1. $\endgroup$
    – Moonwalk
    Dec 14, 2022 at 9:37
  • $\begingroup$ @Moonwalk, I am using v12.2.0 on Win7-x64. $\endgroup$
    – Syed
    Dec 16, 2022 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.