Search code examples
matlablimitscontourf

Why doesn't the contourf function in Matlab use the highest value of the plotted data?


does anybody know why the function countourf doesn't use the color corresponding to the maximum value anywhere in the plot area?

if you try the code below, and then the command get(h_colorbar,'YLim') Matlab returns an upper limit which is not the highest element of the matrix (500), but a smaller number (475.9091).

As you can see from the commented lines in the code, I was able to change the upper/lower limits of the colorbar, but of course this doesn't solve the problem. I'd like Matlab to actually use the highest values in my matrix of data; for instance, I'd like to see the point corresponding to (200,300) colored in the darkest red.

Xdata=[7 11 15 19 23 27 31 39 50 75 100 200];
Ydata=[50 100 140 150 200 300];
dataZ=[...
    500 500 438 310 269 253 245 238 235 237 241 500 ...
    500 414 291 259 248 244 241 239 239 250 274 500 ...
    500 335 268 251 246 243 241 240 242 261 308 500 ...
    500 323 264 250 245 243 241 241 243 265 319 500 ...
    500 289 256 248 244 243 242 243 248 287 500 500 ...
    360 264 250 245 244 243 244 247 261 376 500 500 ...
    ]';    

% % % In matrix form
mdataZ=vec2mat(dataZ,length(Xdata));

[mXdata,mYdata]=meshgrid(Xdata,Ydata);

figure_5=figure;
set(gca,'FontName','Times New Roman', 'FontSize',16,'YColor','k')
hold on
box on
% % % set(gca,'CLim',[min(dataZ) max(dataZ)])
contourf(mXdata,mYdata,mdataZ,10)
scatter(19,140,50,'k')
h_colorbar=colorbar;
set(get(h_colorbar,'ylabel'),'string','Z','FontName','Times New Roman', 'FontSize',18)
set(h_colorbar,'FontName','Times New Roman','FontSize',16)
% % % set(h_colorbar,'YLim',[200 500],'YTick',[0:50:500])
% % % caxis([200 500])
axis([min(min(mXdata)),max(max(mXdata)),min(min(mYdata)),max(max(mYdata))])
xlabel('X','FontName','Times New Roman', 'FontSize',18)
ylabel('Y','FontName','Times New Roman', 'FontSize',18)

Any idea?

Thanks in advance!


Solution

  • contourf splits your data into n (10 for your case) levels. Unless you specify the levels they are chosen automatically by the function.

    The highest level must be lower than the highest point in your data. Maybe it could be the same, I'm not sure how matlab treats values equal to a contour in this case. But if you leave it to automatic contour levels it will definitely be lower.

    The individual data points are not plotted by the function, only the contour heights. Therefore, the value 500 is not in the colormap and the max is the height of the highest contour.

    To solve this you can put a vector of contour values rather than n. Put the highest value close or equal to 500.