Colormap not showing properly

35 次查看(过去 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.

采纳的回答

DGM
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
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)
Luis Jesús Olvera Lazcano
Thanks for the examples! Unfortunately it kept happening, even specifying more in the clim().
But I noted one thing, I downloaded the 2021b version in my pc, and it works perfectly fine, the colors dont get distorted. Do you know why in more recent versions the "levels" doesnt work properly?

请先登录,再进行评论。

更多回答(1 个)

Ishit
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.
  1 个评论
Luis Jesús Olvera Lazcano
Hello! Thank you for your response
I have tried to put other colormaps (jet, parula, etc) and it does the same thing. The thing is that, when I try several times, SOMETIMES it does it normal, but most of the times it does not. Its more probable to happen if I specify lower number of levels (30,20,10..)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by