contourf and Contour in the same figure with 2 different sets of data. Colors of contourf plot of data 1 are changed when using contour with data 2.

조회 수: 4 (최근 30일)
Hi all,
in the same figure, I use contourf(X_vector, Y_vector, M1) with data1 and then I use contour with different data (contour(X_vector, Y_vector, M2)). If at least one value of M2 is greater than the highest value of the colorbar scale used for contourf, the colors of the contourf plot of data 1 are changed.
Why this happens? How can I avoid this (by disactivating the update of the colors if the contourf or in other ways?)?
Thank you in advance.
Here is a code as an example:
X_vec=[10:10:100];
Y_vec=[10:10:100];
% Create the matrix
n = 10;
M_mat = zeros(n);
for i = 1:n
for j = 1:i
value = 100 - (i - 1) * 10 - (j - 1) * 10;
M_mat(i,j) = max(value, 0); %
end
end
f2=0.7; f3=10;
M2_mat=M_mat*f2; M3_mat=M_mat*f3;
% Figure
fig1=figure();
contourf(X_vec,Y_vec,M_mat,'ShowText','off','LineColor','none');hold on;
scatter(min(X_vec),max(Y_vec),'r','filled');
text(min(X_vec)+2,max(Y_vec)-2,num2str(max(max(M_mat))),'FontWeight','bold','Color','r');
ylabel('Y','FontWeight','bold');
xlabel('X','FontWeight','bold');
cc=colorbar;
cc.Color='r';
cc.Limits=[0 max(max(M_mat))];
colormap('sky');
fig1.Position=[679.4000 389.4000 540.7680 405.5760];
axis equal;
[C h]=contour(X_vec,Y_vec,M_mat,'ShowText','on'); hold on;
set(h,'EdgeColor','r');
h.LabelColor='r';
% now the issue starts:
[CC hh]=contour(X_vec,Y_vec,M3_mat,[5:20:105]*f3,'ShowText','on'); hold on;
set(hh,'EdgeColor','k');
hh.LineStyle='-.';
% The color of the map is changed: the colors previously shown become clearer
% It seems that the colors have been updated with the new data even if I
% used contour and not contourf. How can I avoid this?
% This issue does not appear if I use M2_mat, which has values lower that
% the maximum value of M_mat.
%%
fig2=figure();
contourf(X_vec,Y_vec,M_mat,'ShowText','off','LineColor','none');hold on;
scatter(min(X_vec),max(Y_vec),'r','filled');
text(min(X_vec)+2,max(Y_vec)-2,num2str(max(max(M_mat))),'FontWeight','bold','Color','r');
ylabel('Y','FontWeight','bold');
xlabel('X','FontWeight','bold');
cc=colorbar;
cc.Color='r';
cc.Limits=[0 max(max(M_mat))];
colormap('sky');
fig2.Position=[679.4000 389.4000 540.7680 405.5760];
axis equal;
[C h]=contour(X_vec,Y_vec,M_mat,'ShowText','on'); hold on;
set(h,'EdgeColor','r');
h.LabelColor='r';
I would like to have a figure with the colors show in fig2 but with both the red and back contour of fig1.
In the case of M2_mat, a matrix where the maximum value is lower than the highest value of the colorbar of contourf(X_vec,Y_vec,M_mat), this issue does not appear:
X_vec=[10:10:100];
Y_vec=[10:10:100];
% Create the matrix
n = 10;
M_mat = zeros(n);
for i = 1:n
for j = 1:i
value = 100 - (i - 1) * 10 - (j - 1) * 10;
M_mat(i,j) = max(value, 0); %
end
end
f2=0.7; f3=10;
M2_mat=M_mat*f2; M3_mat=M_mat*f3;
% Figure
fig3=figure();
contourf(X_vec,Y_vec,M_mat,'ShowText','off','LineColor','none');hold on;
scatter(min(X_vec),max(Y_vec),'r','filled');
text(min(X_vec)+2,max(Y_vec)-2,num2str(max(max(M_mat))),'FontWeight','bold','Color','r');
ylabel('Y','FontWeight','bold');
xlabel('X','FontWeight','bold');
cc=colorbar;
cc.Color='r';
cc.Limits=[0 max(max(M_mat))];
colormap('sky');
fig3.Position=[679.4000 389.4000 540.7680 405.5760];
axis equal;
[C h]=contour(X_vec,Y_vec,M_mat,'ShowText','on'); hold on;
set(h,'EdgeColor','r');
h.LabelColor='r';
% now the issue starts:
[CC hh]=contour(X_vec,Y_vec,M2_mat,[5:20:105]*f2,'ShowText','on'); hold on;
set(hh,'EdgeColor','k');
hh.LineStyle='-.';

답변 (1개)

Walter Roberson
Walter Roberson 2025년 2월 23일
This is normal. You are using contourf() and contour() on the same axes. There is only one CLim (color limit) property per axes.
One possibility would be if you were to call
clim("manual")
after the contourf() but before contour() . This will prevent the CLim property from automatically updating after it was set from the contourf() call. However, if the contour() has data that is outside of the range implied by contourf() then the colors for the outside data will be clipped.
  댓글 수: 3
Walter Roberson
Walter Roberson 2025년 2월 23일
clim auto (which is the default) automatically adjusts the color limits according to bounds() of all of the color variables together. Effectively the order of plotting does not matter if clim auto is in effect.
clim manual freezes the current CLim, so order of plotting does matter for clim manual.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by