How to make a a certain range with different color in colorbar
Show older comments
I created a a contour plot and I would like to make the range between -50 and 0 different than the range 0 and 50 as below. Anway to help please
[X,Y] = meshgrid(10:5:80,0:1:20);
Z = f([X(:), Y(:)]);
Z1 = reshape(Z,[21,15]);
figure
contour(X,Y,Z1,15000)
colormap hsv

colorbar
xlabel('\gamma (%)')
ylabel('\lambda (%)')
zlabel('m^{*}_{NPV} ($B)')
Accepted Answer
More Answers (1)
Walter Roberson
on 9 Aug 2020
The above function requires that the positions be defined in relative terms, 0 to 1, rather than in absolute terms. You would have to decide ahead of time what your caxis() is going to be, and use proportional positions through that range,
relative_position = (data_x - min_to_map)./(max_to_map - min_to_map)
8 Comments
Yaser Khojah
on 9 Aug 2020
Walter Roberson
on 9 Aug 2020
Create a vector containing all of the range boundaries that you want to distinguish. For example you might want [-100 -50 -25 -10 -5 0 5 10 25 50 100] in the case where fine differences near 0 were important to you.
diff() the boundary vector. Now find the gcd(), greatest common divisor https://www.mathworks.com/help/matlab/ref/gcd.html . This will be the size of one "slot" in the colormap, how quickly the colormap can switch between colors but without extra colormap entries. In the above example vector, the gcd would be 5. The size of the colormap you will generate will be (max(boundaries) - min(boundaries)) / gcd
Now take the difference vector divided by the gcd(). The result at each point is number of consecutive slots of the same color to use to represent that range. In the above example this would be 10 slots needed for the -100 to -50 range, 5 slots needed for the -50 to -25 range, 3 slots for -25 to -10, 1 slot for -10 to -5, -5 to 0, 0 to 5, 5 to 10, 3 for 10 to 25, 5 for 25 to 50, 10 for 50 to 100.
Take your color list, and repeat each entry the calculated number of times.
Now you can activate that colormap. And be sure to caxis() the entire range.
In theory you could take this approach right down to eps() difference between values. In practice, older versions of Windows only supported 256 colors; newer Windows 10 supports 65536 now.
... No, there is no way to just give a list of values representing boundaries, and corresponding colors, and have the graphics system work it all out; for it to do that, it would have to do the above kind of calculation, as the graphics standards such as OpenGL do not offer that facility.
Yaser Khojah
on 11 Aug 2020
Walter Roberson
on 11 Aug 2020
Could you post the relevant part of the code?
At the moment it looks to me as if you might have an off-by-one in the bins.
Yaser Khojah
on 11 Aug 2020
Edited: Yaser Khojah
on 11 Aug 2020
Walter Roberson
on 12 Aug 2020
You posted a .xlsx but your code loads a .mat ?
Yaser Khojah
on 12 Aug 2020
Oh I’m sorry I just added the xsl for you since I though I can’t upload the mat file.
Walter Roberson
on 12 Aug 2020
levels = [-700 -600 -500 -400 -300 -200 -100 -50 -40 -30 -20 -10 0 10 20 30 40 50 100 200 300];
[a,b] = contourf(X, Y, Z1, levels);
This will not in itself create new colormap entries for the various levels, but it will outline them more clearly.
To create new colormap entries, you have to follow the strategy I outlined above, of creating a colormap with replicated slots.
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




