Contourf - How to define colors between, above and below contourlines
조회 수: 26 (최근 30일)
이전 댓글 표시
Hello,
I have searched for a solution to this problem but couldn't find any answers.
I have X, Y, and Z data (example given below), and I want to create a contourf plot. I would like to specify the contour levels and assign specific colors to the areas between these contour lines. Additionally, I want to define colors for the regions below the lowest contour line and above the highest contour line.
I am aware of the contourfcmap function, but I want to implement these plots in MATLAB's App Designer, where using contourfcmap from MATLAB File Exchange is quite complicated.
Can anyone help me with this?
Thanks a lot!
Kind regards
Steve
Code as example:
figure
% Random-Data
x = linspace(-5, 5, 100); % x-Data
y = linspace(-5, 5, 100); % y-Data
[X, Y] = meshgrid(x, y);
sigma = 1;
Z = exp(-(X.^2 + Y.^2) / (2 * sigma^2));
% Contourlevels
contour_levels = [0.1, 0.3, 0.5, 0.7, 0.9];
contour_level_below = min(min(Z(:)),min(contour_levels))-1;
cmap = [
1 0 0; % < 0.1
0 0 1; % 0.1 - 0.3
0 0.5 0; % 0.3 - 0.5
0 1 0; % 0.5 - 0.7
1 1 0; % 0.7 - 0.9
0 0 0; % > 0.9
];
colormap(cmap)
[~, hc] = contourf(X, Y, Z, [contour_level_below, contour_levels], 'LineColor', 'k');
As you can see, with given code contourf does everything as expected. But the area between 0.1 and 0.3 isn't colored as expected (should be blue).
댓글 수: 0
채택된 답변
Cris LaPierre
2024년 10월 23일 18:39
편집: Cris LaPierre
2024년 10월 23일 18:40
I note that the documentation states the following:
"The contourf function uses the current colormap to fill the spaces between the levels in the plot. The first color fills the space between the lowest level and the level above it. The last color corresponds to Z-values that are greater than the highest level in the plot. If Z contains values that are smaller than the lowest level displayed in the plot, the region between the lowest level and the smallest Z-value is white."
My observation is that does not accurately describe what is happening. I also see different behavior depending on if I specify the number of contours vs specify the levels. I suspect it will also depend on if the step size is uniform or not.
Currently, it seems that contourf spreads the colors evenly across the range of values displayed in your chart. You can see this if you add a colorbar. So the results may be more like what you expect if you specify the number of contours because they will be evenly spaced across the range of your data.
figure
% Random-Data
x = linspace(-5, 5, 100); % x-Data
y = linspace(-5, 5, 100); % y-Data
[X, Y] = meshgrid(x, y);
sigma = 1;
Z = exp(-(X.^2 + Y.^2) / (2 * sigma^2));
% Contourlevels
contour_levels = [0.1, 0.3, 0.5, 0.7, 0.9];
contour_level_below = min(min(Z(:)),min(contour_levels))-1;
cmap = [
1 0 0; % < 0.1
0 0 1; % 0.1 - 0.3
0 0.5 0; % 0.3 - 0.5
0 1 0; % 0.5 - 0.7
1 1 0; % 0.7 - 0.9
0 0 0; % > 0.9
];
colormap(cmap)
[~, hc] = contourf(X, Y, Z, [contour_level_below, contour_levels], 'LineColor', 'k');
colorbar
Here is the workaround. To ensure your colormap behaves the way you intend, determine the smallest step size, then define your colormap across the full range using that stepsize. You also will want to set your color limits to ensure the colormap is applied as intended.
Here, the smallest stepsize is 0.1, the first step. Since you want to incorporate a color for values >0.9, set clim to [0 1]. Finally, so that values <0.1 are not white, prepend your contour_levels with -inf.
figure
% Contourlevels
contour_levels = [-inf 0.1, 0.3, 0.5, 0.7, 0.9];
cmap = [
1 0 0; % < 0.1
0 0 1; % 0.1 - 0.2
0 0 1; % 0.2 - 0.3
0 0.5 0; % 0.3 - 0.4
0 0.5 0; % 0.4 - 0.5
0 1 0; % 0.5 - 0.6
0 1 0; % 0.6 - 0.7
1 1 0; % 0.7 - 0.8
1 1 0; % 0.8 - 0.9
0 0 0; % > 0.9
];
colormap(cmap)
[~, hc] = contourf(X, Y, Z, contour_levels, 'LineColor', 'k');
clim([0 1])
colorbar
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 White에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!