How do I plot contours colours within ranges?
조회 수: 9 (최근 30일)
이전 댓글 표시
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?

댓글 수: 0
채택된 답변
Walter Roberson
2020년 9월 28일
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
Walter Roberson
2020년 9월 28일
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)
추가 답변 (1개)
Ameer Hamza
2020년 9월 28일
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
Walter Roberson
2020년 9월 28일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!