Colormap not showing properly
25 次查看(过去 30 天)
显示 更早的评论
I used both cmocean and colormap for plots, and when I dont define the levels of the color (i.e. the quantity of colors) it displays normally. But when I put the quantity such as:
colormap(jet(15))
cmocean('tarn',15) (this is in the climate data toolbox
The higher colors are distorted as I show you here (the distorted vs the normal colormap)
Whats the reason? I checked both 2023a and 2022b versions and the problem is still here.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1410934/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1410939/image.png)
0 个评论
采纳的回答
DGM
2023-6-15
编辑:DGM
2023-6-15
This looks exactly like what should be expected. When you quantize your z data to 15 levels, you have less z-resolution than when you quantize your data to (e.g.) 256 levels.
x = linspace(0,1,50);
y = linspace(0,1,50).';
z = x+y;
imagesc(z)
colorbar
colormap(jet) % default is 256 in newer versions
figure
imagesc(z)
colorbar
colormap(jet(16)) % explicitly use fewer levels
It should be noted that when you don't specify the number of levels, the number of levels that's used depends on the version that you're running (defaults are either 64 or 256) and the last colormap that was used in the same figure (even if the figure has been explicitly cleared). If you care about the number of levels in the colormap, you must specify it explicitly.
% if the length of the colormap is unspecified, and we have an existing
% figure open, the length of the new colormap will be the same as the
% length of the last colormap used in that figure
clf % clear the figure
imagesc(z)
colorbar
colormap(parula) % default is ignored, and the length of the last map is used
% so explicitly specify the map length if you want a particular length
clf % clear the figure
imagesc(z)
colorbar
colormap(parula(256)) % explicitly specify the desired map length
This behavior is mentioned in the documentation for jet(), parula(), etc.
3 个评论
DGM
2023-6-15
编辑:DGM
2023-6-15
I added more examples and code comments. If you're seeing the relative positions of colored regions shift when you rerun the code, that may be due to the length of the colormap changing (if it's not specified), or that may also be due to the color axis limits changing. See clim() (or caxis() in older versions)
更多回答(1 个)
Ishit
2023-6-15
Hi Luis,
The reason for the distortion in the higher colors of your colormap when you define the number of levels is likely due to the way that the colors are mapped to the data values. When you specify the number of levels (i.e., the number of colors) in your colormap, MATLAB or the Climate Data Toolbox (CDT) uses that number to determine how to map the colors to the data values.
It's also possible that there may be differences in the way that colormap and cmocean handle the mapping of colors to data values. You may want to try using different colormaps or adjusting the parameters of the colormap functions to see if that resolves the issue.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!