How do I plot contours colours within ranges?

27 vues (au cours des 30 derniers jours)
Hamid Attar
Hamid Attar le 28 Sep 2020
Commenté : Walter Roberson le 28 Sep 2020
Hi,
The contour image below was generated in Excel, where the colours indicate ranges of the Z direction values. For example the orange zone is values of the Z direction in the range 10-20.
I am looking to recreate this contour in Matlab with the same data, how can I plot it with colours for different ranges, like the figure shows?

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Sep 2020
contourf() and pass in a specific list of contour level values. Combine that with a custom colormap and probably the caxis() command.
However: in general, the custom colormap might end up needing extra duplicate colors, if the division of values is not even. For example if the list were 0-10, 10-25, 25-30, then that would have to be implemented like
blue %0-5
blue  %5-10
orange %10-15
orange %15-20
orange %20-25
gray %25-30
  2 commentaires
Hamid Attar
Hamid Attar le 28 Sep 2020
Sorry i'm not clear what you mean. Could you please provide some example code to illustrate the colour divisions?
Walter Roberson
Walter Roberson le 28 Sep 2020
contourf(X, Y, Z, [10 20 30]);
caxis([0 30]);
cmap = [0 0 255; %blue
255 94 19; %orange
30 30 30; %gray
] ./ 255; %rescale to 0 to 1
colormap(cmap)

Connectez-vous pour commenter.

Plus de réponses (1)

Ameer Hamza
Ameer Hamza le 28 Sep 2020
pcolor() will also be useful in this case
% generating example matrix
[x1, y1] = meshgrid([0 1]);
z1 = [0 0.5; 0.5 1];
[x2, y2] = meshgrid(linspace(0, 1));
z2 = interp2(x1, y1, z1, x2, y2);
z2d = discretize(z2, [0 0.3 0.7 1]); % [0 0.3 0.7 1] are ranges for each color
pcolor(x2, y2, z2d);
shading interp
  2 commentaires
Hamid Attar
Hamid Attar le 28 Sep 2020
Thanks for this, really helpful. But how do I select the colours I want for each zone?
For example, 0-0.3 to be blue, 0.3-0.7 orange, 0.7-1 grey.
Thanks
Walter Roberson
Walter Roberson le 28 Sep 2020
In MATLAB, you cannot directly specify numeric bounds for colormap entries. The colormap index is always done as
1 + floor((value - min_value) / (max_value - min_value) * number_of_entries in map)
and min_value and max_value are the limits established with the current caxis.
Because of this, if you just happen to have data bins that are all the same width, like your original example 0-10, 10-20, 20-30, then you only need as many color entries as there are bins.
However, if your data bins are not all the same width, then you have to find the GCD (greatest common divisor) of the width of the data bins, and divide the data bin widths by that GCD in order to figure out how many copies of the color for that bin you need. In the example 0-0.3, 0.3-0.7, 0.7-1 those widths are 0.3 and 0.4 and gcd of those is 0.1, so you would have to create 3 slots for 0-0.3, 4 slots for 0.3 - 0.7, 3 slots for 0.7-1.0, and you would fill those slots with identical copies of the color, so 3 copies of dark blue, 4 copies of orange, 3 copies of grey. The values from 0 to 0.1 would get the first blue slot, the values from 0.1 to 0.2 would get the second blue slot, the values from 0.2 to 0.3 would get the third blue slot -- and since those slots all have the same color, the effect is that all values from 0 to 0.3 show up the same.
If you happen to have irregular boundaries like 0, , e, π, 5, then you have to decide how accurate you need your boundaries to be -- is it okay if 3.2 is the same color as 3.1 for example? Is it okay if 3.15 is the same slot as 3.14 ? And then, having decided on the resolution you need, you would carry out the same GCD process and make the appropriate number of copies of each of the colors.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by