2
$\begingroup$

I have defined the following colours and I want to use them for my densityplot.

color1 = RGBColor[0/255 , 0/255 , 0/255];
color2 = RGBColor[31.875/255 , 0/255 , 31.875/255];
color3 = RGBColor[63.75/255 , 0/255 , 63.75/255];
color4 = RGBColor[95.625/255 , 0/255 , 95.625/255];
color5 = RGBColor[127.50/255 , 0/255 , 127.50/255];
color6 = RGBColor[159.375/255 , 0/255 , 159.375/255];
color7 = RGBColor[191.25/255 , 0/255 , 191.25/255];
color8 = RGBColor[223.125/255 , 0/255 , 223.125/255];
color9 = RGBColor[255/255 , 0/255 , 255/255];
color10 = RGBColor[204/255 , 0/255 , 255/255];
color11 = RGBColor[153/255 , 0/255 , 255/255];
color12 = RGBColor[102/255 , 0/255 , 255/255];
color13 = RGBColor[51/255 , 0/255 , 255/255];
color14 = RGBColor[0/255 , 0/255 , 255/255];
color15 = RGBColor[0/255 , 31.875/255 , 255/255];
color16 = RGBColor[0/255 , 63.75/255 , 255/255];
color17 = RGBColor[0/255 , 95.625/255 , 255/255];
color18 = RGBColor[0/255 , 127.50/255 , 255/255];
color19 = RGBColor[0/255 , 159.375/255 , 255/255];
color20 = RGBColor[0/255 , 191.25/255 , 255/255];
color21 = RGBColor[0/255 , 223.125/255 , 255/255];
color22 = RGBColor[0/255 , 255/255 , 255/255];
color23 = RGBColor[0/255 , 255/255 , 204/255];
color24 = RGBColor[0/255 , 255/255 , 153/255];
color25 = RGBColor[0/255 , 255/255 , 102/255];
color26 = RGBColor[0/255 , 255/255 , 51/255];
color27 = RGBColor[0/255 , 255/255 , 0/255];
color28 = RGBColor[51/255 , 255/255 , 0/255];
color29 = RGBColor[102/255 , 255/255 , 0/255];
color30 = RGBColor[153/255 , 255/255 , 0/255];
color31 = RGBColor[204/255 , 255/255 , 0/255];
color32 = RGBColor[255/255 , 255/255 , 0/255];
color33 = RGBColor[255/255 , 229.50/255 , 0/255];
color34 = RGBColor[255/255 , 204/255 , 0/255];
color35 = RGBColor[255/255 , 178.50/255 , 0/255];
color36 = RGBColor[255/255 , 153/255 , 0/255];
color37 = RGBColor[255/255 , 127.50/255 , 0/255];
color38 = RGBColor[255/255 , 102/255 , 0/255];
color39 = RGBColor[255/255 , 76.50/255 , 0/255];
color40 = RGBColor[255/255 , 51/255 , 0/255];
color41 = RGBColor[255/255 , 25.50/255 , 0/255];
color42 = RGBColor[255/255 , 0/255 , 0/255];
color43 = RGBColor[255/255 , 51/255 , 51/255];
color44 = RGBColor[255/255 , 102/255 , 102/255];
color45 = RGBColor[255/255 , 153/255 , 153/255];
color46 = RGBColor[255/255 , 204/255 , 204/255];
color47 = RGBColor[255/255 , 255/255 , 255/255];

Each colour represents a temperature degree as below:

color1 -> -22
color2 -> -21.125
color3 -> -20.25
color4 -> -19.375
color5 -> -18.5
color6 -> -17.625
color7 -> -16.75
color8 -> -15.875
color9 -> -15
color10 -> -14
color11 -> -13
color12 -> -12
color13 -> -11
color14 -> -10
color15 -> -9
color16 -> -8
color17 -> -7
color18 -> -6
color19 -> -5
color20 -> -4
color21 -> -3
color22 -> -2
color23 -> -1.6
color24 -> -1.2
color25 -> -0.8
color26 -> -0.4
color27 -> 0
color28 -> 0.4
color29 -> 0.8
color30 -> 1.2
color31 -> 1.6
color32 -> 2
color33 -> 3.6
color34 -> 5.2
color35 -> 6.8
color36 -> 8.4
color37 -> 10
color38 -> 13
color39 -> 16
color40 -> 19
color41 -> 22
color42 -> 25
color43 -> 28
colo44 -> 31
color45 -> 34
color46 -> 37
color47 -> 40

What I would like to do now is to create just the bar legend of these colours with the desired distributions. As you can see my colour function is nonlinear and the colours are not equally spaced along the temperature range. However, when I use the following code, I realise that mathematica just linearly distribute the colours and does not take into consideration the temperatures that I have specified.

BarLegend[{{color1 , color2 , color3 , color4 , color5 , color6 , 
   color7 , color8 , color9 , color10 , color11 , color12 , color13 , 
   color14 , color15 , color16 , color17 , color18 , color19 , 
   color20 , color21 , color22 , color23 , color24 , color25 , 
   color26 , color26 , color27 , color28 , color29 , color30 , 
   color31 , color32 , color33 , color34 , color35 , color36 , 
   color37 , color38 , color39 , color40 , color41 , color42 , 
   color43 , color44 , color45  , color46 , color47} , {-22 , 
   40}} , {-22 , -21.125 , -20.25, -19.375 , -18.5 , -17.625 , -16.75
, -15.875 , -15 , -14 , -13 , -12 , -11 , -10 , -9 , -8 , -7 , -6 ,
-5 , -4 , -3 , -2 , -1.6 , -1.2 , -0.8 , -0.4 , 0 , 0.4 , 0.8 , 1.2 , 
  1.6 , 2 , 3.6 , 5.2 , 6.8 , 8.4 , 10 , 13 , 16 , 19 , 22 , 25 , 
  28 , 31 , 34 , 37 , 40}, LegendLayout -> "Row"  , 
 LabelStyle -> {Black }] 

Is there any solution for creating this barlegend?

thanks in advance

$\endgroup$
0

1 Answer 1

2
$\begingroup$

I will answer my own question. All I had to do was to create a function for the colour function. So I did it as below and now it works fine.

mycf = Blend[{{-22 , color1}, {-21.125 , color2} , {-20.25 , 
     color3} , {-19.375 , color4} , {-18.5 , color5}, {-17.625 , 
     color6}, {-16.75 , color7}, {-15.875 , color8}, {-15 , 
     color9}, {-14, color10}, {-13 , color11}, {-12 , 
     color12}, {-11 , color13}, {-10 , color14}, {-9 , 
     color15}, {-8 , color16} , {-7 , color17} , {-6 , 
     color18} , {-5 , color19} , {-4 , color20} , {-3 , 
     color21} , {-2 , color22} , {-1.6 , color23} , {-1.2 , 
     color24} , {-0.8 , color25} , {-0.4 , color26} , {0 , 
     color27} , {0.4 , color28} , {0.8 , color29} , {1.2 , 
     color30} , {1.6 , color31} , {2 , color32} , {3.6, 
     color33} , {5.2 , color34} , {6.8 , color35} , {8.4 , 
     color36} , {10 , color37} , {13 , color38} , {16 , 
     color39} , {19 , color40} , {22 , color41} , {25 , 
     color42} , {28 , color43} , {31 , color44} , {34 , 
     color45} , {37 , color46} , {40 , color47}} , #1] &

After that simply I was able to get the desired result:

BarLegend[{mycf1[#] & , {-20 , 40}}, LegendLayout -> "Row"  , 
  LabelStyle -> {Black } ,   
  Method -> {AbsoluteThickness -> AbsoluteThickness[1] , 
    TicksStyle -> Black } ,   
  BaseStyle -> {FontFamily -> "Times" , FontSize -> 14}]
$\endgroup$

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.