Nonlinear colour axis on colorbar
17 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to make a nonlinear (not logarithmic though) axis on my colorbar. To explain - if I use the following code:
a = rand(20,20);
colormap(jet(5))
conts = [0:1/5:1];
contourf(a,conts,'k:')
cb = colorbar;
caxis([0 1])
set(cb,'YTick',conts)
It produces a correct contourf figure with contour values equally spaced through the colorbar.
However, if I wanted to contour different values, and the change the routine to
a = rand(20,20);
colormap(jet(5))
conts = [0 0.01 0.05 0.9 0.95 1];
contourf(a,conts,'k:')
cb = colorbar;
caxis([0 1])
set(cb,'YTick',conts)
I end up with the colorbar patches not matching the contours I specified and the contours themselves not equally spaced on the colorbar axis, but shown rather as they would be in relation to the distance from zero. My intention is to make the shaded patches match up with the contour intervals I specify, and then have those spaced out neatly along the colorbar regardless of their actual value. The reason I have this is because I have data where the contour values will be something like [-0.1 -0.5 0.5 0.75 0.8 1 10 50 80 85 90:2:100] with each contour chosen for a reason and I need to display it nicely.
Any suggestions?
2 个评论
Jiro Doke
2012-2-19
Do you want the ticks of the color bar to be equally spaced (even if the actual values aren't equally spaced)? Or instead, do you want the color patches to be non-equally spaced to match the actual tick marks? The latter would be more difficult, possibly requiring you to create your own colorbar.
回答(3 个)
Stephen Licata
2018-3-13
One way of handling this challenge - which I think is what the first answer suggested - is to create a set of colors and treat each color as a separate bin. You would then "translate" data from different value ranges into a sixed, fixed bin number.
For example, you could have a color bar with five colors, as follows: new colors = zeros(5,3);
[041 009 216] scaled to 255 => new colors(1,1:3) = [0.1608 0.0353 0.8471] % dark blue [170 248 255] scaled to 255 => new_colors(2,1:3) = [0.6667 0.9725 1.0000] % light blue [255 255 191] scaled to 255 => new_colors(3,1:3) = [1.0000 1.0000 0.7490] % yellow [248,109,094] scaled to 255 => new_colors(4,1:3) = [0.9725 0.4275 0.3686] % orange [165 000 033] scaled to 255 => new_colors(5,1:3) = [0.6471 0.0000 0.1294] % dark red
Then assign a bin number range of 1 to 5
Then, if you have data that varies from, say, -10 (or less) to + 10.0 (or more), examine each data point and assign it a bin number that corresponds to any non-linear set of ranges.
For example, for data array float X(1000) and corresponding bin assignment integer B(1000)...
if X(n) < -10.0 and X(n) <=-3.0, then B(n) = 1; % dark blue if X(n) >-3.0 and X(n) <-0.50, then B(n) = 2; % light blue if X(n) >=-0.5 and X(n) <= 0.50, then B(n) = 3; % yellow if X(n) > 0.50 and X(n) < 3.0, then B(n) = 4; % orange if X(n) >= 3.0 and X(n) <= 10.0, then B(n) = 5; % dark red
You would then simply plot the B array (not X). When you create your color bar, just scale it from 1 to 5 but add the "real data value" labels of '-10', '-3', '-0.5', '0.5', '3.0', '10';
0 个评论
Constantino
2018-11-9
I have the same difficulty, but solved it another way. In short, I plotted with contourf, with the non evenly spaced contours given by a vector. Then, I plotted an empty axis, and made a discrete colorbar (for example jet(4)). The I just changed the colors and the labels of the colorbar ticks. Here is the result. I will read your strategy in detail to see if it is better. To add the second (empty) plot I just put hold on, fakeax=axes, and turned the visible propertie off. The colorbar of the fake invisible axes covers the wrong previous axes.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194351/image.jpeg)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!