MATLAB Answers

How to set a colorbar using specific RGB values and limit values?

32 views (last 30 days)
Philippe Corner
Philippe Corner on 28 Oct 2019
Edited: Image Analyst on 29 Oct 2019
The attached file contains a contour plot code which gets the following colorbar:
Please run the attached code to see the colorbar I'm currently obtaining (using a jet colorbar).
% colorbar question
clear;clc
A=[1.941 1947.138 625.500 0.001599 14.014
5.939 1947.077 1289.900 0.000775 19.300
9.938 1947.015 581.960 0.001718 22.987
13.936 1946.950 758.890 0.001318 28.259
17.933 1946.882 1333.500 0.000750 31.257
21.931 1946.812 217.800 0.004591 20.110
25.929 1946.739 446.300 0.002241 23.991
29.926 1946.662 261.310 0.003827 20.253
33.260 1945.329 293.540 0.003407 21.296
36.326 1943.093 4547.300 0.000220 18.993
40.058 1942.137 638.980 0.001565 14.785
44.057 1942.110 268.030 0.003731 18.367
48.056 1942.084 248.240 0.004028 18.609
52.055 1942.056 374.330 0.002671 28.995
56.054 1942.028 1213.400 0.000824 23.916
60.054 1941.998 126.760 0.007889 27.747
64.053 1941.968 628.800 0.001590 16.702
68.051 1941.937 316.540 0.003159 25.726
72.050 1941.905 198.610 0.005035 26.283
76.049 1941.871 246.990 0.004049 21.835
80.048 1941.836 216.320 0.004623 16.395
84.047 1941.800 150.220 0.006657 17.376
88.045 1941.762 234.310 0.004268 13.832
92.044 1941.722 28.310 0.0353 5.897
1.824 1945.123 392.850 0.002546 14.733
5.820 1945.023 159.100 0.006285 18.993
9.816 1944.919 581.530 0.001720 21.716
13.811 1944.812 512.470 0.001951 21.257
17.807 1944.699 214.740 0.004657 32.690
21.802 1944.582 7111.000 0.000141 19.865
25.796 1944.459 40.650 0.0246 19.236
29.791 1944.330 408.570 0.002448 21.704
33.139 1943.253 20.157 0.0496 16.667
36.228 1941.406 45.401 0.0220 19.619
39.965 1940.547 36.044 0.0277 21.503
43.963 1940.498 517.490 0.001932 22.070
47.961 1940.447 503.470 0.001986 20.255
51.959 1940.395 640.280 0.001562 29.378
55.957 1940.341 148.020 0.006756 23.256
59.954 1940.285 695.240 0.001438 25.728
63.951 1940.227 52.922 0.0189 23.278
67.949 1940.167 74.723 0.0134 16.712
71.946 1940.104 202.610 0.004936 14.442
75.943 1940.038 162.530 0.006153 22.836
79.939 1939.970 313.060 0.003194 31.804
83.936 1939.898 199.050 0.005024 17.033
87.932 1939.824 152.240 0.006569 13.796
91.929 1939.745 125.130 0.007992 7.540
1.701 1943.011 384.880 0.002598 15.792
5.696 1942.879 213.860 0.004676 18.482
9.689 1942.743 443.110 0.002257 20.597
13.683 1942.602 535.490 0.001867 20.139
17.676 1942.455 169.240 0.005909 20.209
21.669 1942.302 642.590 0.001556 16.612
25.662 1942.142 422.030 0.002369 17.110
29.654 1941.976 4123.000 0.000243 19.104
33.014 1941.106 24.004 0.0417 17.199
36.123 1939.584 15.608 0.0641 20.787
39.865 1938.813 1870.500 0.000535 21.935
43.861 1938.743 431.630 0.002317 22.631
47.858 1938.670 25.172 0.0397 20.826
51.854 1938.595 567.980 0.001761 26.448
55.851 1938.517 173.370 0.005768 24.556
59.847 1938.437 382.260 0.002616 25.764
63.843 1938.353 356.150 0.002808 25.527
67.838 1938.266 341.330 0.002930 17.118
71.834 1938.176 258.650 0.003866 11.283
75.829 1938.082 29.999 0.0333 11.573
79.824 1937.984 311.460 0.003211 30.600
83.819 1937.883 227.970 0.004387 17.021
87.814 1937.776 151.200 0.006614 13.476
91.808 1937.665 151.010 0.006622 8.100
1.567 1940.692 384.870 0.002598 16.725
5.559 1940.534 386.550 0.002587 18.166
9.552 1940.372 498.560 0.002006 19.432
13.544 1940.203 537.360 0.001861 19.138
17.535 1940.030 168.210 0.005945 18.321
21.527 1939.850 356.010 0.002809 16.273
25.518 1939.664 406.370 0.002461 16.747
29.508 1939.471 4157.500 0.000241 18.232
32.879 1938.771 26.815 0.0373 18.019
36.003 1937.528 15.534 0.0644 21.087
39.750 1936.841 3401.900 0.000294 22.145
43.746 1936.750 618.790 0.001616 22.334
47.741 1936.655 35.707 0.0280 20.814
51.736 1936.557 281.230 0.003556 23.143
55.731 1936.457 175.170 0.005709 23.629
59.726 1936.353 164.400 0.006083 24.666
63.720 1936.245 201.820 0.004955 25.196
67.714 1936.134 339.340 0.002947 17.284
71.708 1936.018 322.740 0.003098 10.790
75.702 1935.899 32.243 0.0310 10.311
79.696 1935.775 97.517 0.0103 14.638
83.689 1935.646 151.340 0.006608 14.587
87.682 1935.512 150.830 0.006630 11.879
91.675 1935.373 152.300 0.006566 8.259
1.418 1938.141 384.210 0.002603 17.366
5.410 1937.956 408.330 0.002449 18.067
9.400 1937.766 636.350 0.001571 18.735
13.391 1937.572 633.730 0.001578 18.507
17.381 1937.373 534.120 0.001872 17.722
21.371 1937.168 523.520 0.001910 16.456
25.361 1936.959 664.740 0.001504 16.666
29.350 1936.745 4573.900 0.000219 17.556
32.731 1936.224 30.296 0.0330 17.962
35.873 1935.295 31.815 0.0314 20.403
39.627 1934.719 3264.300 0.000306 22.167
43.621 1934.598 1165.700 0.000858 21.976
47.614 1934.474 267.040 0.003745 20.134
51.608 1934.347 282.380 0.003541 19.813
55.601 1934.216 187.840 0.005324 19.716
59.594 1934.081 125.860 0.007945 19.951
63.586 1933.943 126.510 0.007905 20.009
67.579 1933.801 209.800 0.004766 16.208
71.571 1933.654 271.320 0.003686 10.129
75.563 1933.503 87.792 0.0114 9.139
79.555 1933.348 90.154 0.0111 9.242
83.546 1933.187 136.170 0.007344 9.380
87.537 1933.022 150.800 0.006631 9.006
91.528 1932.852 154.120 0.006488 8.047
1.256 1935.343 378.440 0.002642 17.755
5.246 1935.143 445.620 0.002244 18.090
9.236 1934.940 1464.600 0.000683 18.402
13.226 1934.732 1486.600 0.000673 18.164
17.215 1934.520 1464.900 0.000683 17.438
21.205 1934.305 1455.700 0.000687 16.525
25.194 1934.085 1474.000 0.000678 16.513
29.182 1933.862 4996.600 0.000200 16.802
32.569 1933.436 38.394 0.0260 16.726
35.722 1932.685 46.085 0.0217 17.921
39.479 1932.177 1215.300 0.000823 22.366
43.472 1932.038 1160.500 0.000862 21.779
47.464 1931.896 579.430 0.001726 18.674
51.457 1931.749 492.920 0.002029 16.120
55.449 1931.599 324.460 0.003082 13.764
59.441 1931.446 118.570 0.008434 12.013
63.432 1931.289 116.440 0.008588 11.813
67.424 1931.127 166.740 0.005997 11.466
71.415 1930.962 206.150 0.004851 8.868
75.406 1930.792 88.028 0.0114 7.657
79.396 1930.619 88.969 0.0112 7.558
83.387 1930.440 129.280 0.007735 7.873
87.377 1930.258 151.360 0.006607 7.960
91.367 1930.070 161.070 0.006208 7.603];
X = A(:,1);
Y = A(:,2);
Z = A(:,3);
[xq,yq]=meshgrid(linspace(min(X),max(X),100),linspace(min(Y),max(Y),100));
zq=griddata(X,Y,Z,xq(:),yq(:),'cubic');
figure()
[c,h]=contourf(xq,yq,reshape(zq,100,100));
hold on
colormap(jet)
h = colorbar;
set( h, 'YDir', 'reverse' );
I would like to obtain the same contour plot but with these colors and values for the ploting colorbar, i defined also RGB values for eacth limit value.
the values are:
colorbarlimits=[6.5696e+000
9.4236e+000
1.3517e+001
1.9390e+001
2.7813e+001
3.9896e+001
5.7228e+001
8.2089e+001
1.1775e+002
1.6890e+002
2.4228e+002
3.4753e+002
4.9851e+002
7.1508e+002
1.0257e+003
1.4713e+003];
The colorbar RGB values may be the following (instead of using colorbar):
RGB=[153 0 0
204 0 0
153 76 0
204 102 0
153 153 0
204 204 0
76 153 0
102 204 0
0 153 0
0 204 0
0 153 76
0 204 102
0 76 153
0 102 204
0 0 153
0 0 204];

  0 Comments

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 28 Oct 2019
Edited: Cris LaPierre on 28 Oct 2019
If you want specific colors at specific values, specify the contour lines. Then you can set the colormap and Caxis limits to force a particular coloring. Perhaps some of the discussion in this post might help.
This might get you started. Note that the colorbar is still linearly spacing the colors. The only way I've found to do nonlinear spacing is to create a color entry for each data point (see this post).
[c,h]=contourf(xq,yq,reshape(zq,100,100),[-1000; colorbarlimits]);
colormap(RGB./255)
h = colorbar;
caxis(colorbarlimits([1 end]))

  2 Comments

Philippe Corner
Philippe Corner on 28 Oct 2019
Thanks for your comment, I checked it already. Do you think there is any way to keep the thicknesses of each color equal, even if the ranges are different? As you can see, doing this plots a colorbar that doesn't show all the color values. Cheers!
Cris LaPierre
Cris LaPierre on 28 Oct 2019
In this case, you may just need to set your Caxis limits to the desired range.

Sign in to comment.

Sign in to answer this question.