Stop contourf from interpolating between my grid

15 次查看(过去 30 天)
I am using a contourf plot of the form: contourf(B0, B1, Z)
The spacing of B0 and B1 is .05.
The function values in Z only take on a few discrete values.
The resulting behavior is that if say Z(b0,b1) = 1 and Z(b0+1,b1) = 2, then MATLAB draws many contours between the two points, thereby creating many lines between the two points.
Is there a way to prevent this behavior?
My initial thought was to try to specify the number of contours directly. However, when this is done MATLAB just fills the region between the two points with one of the other colors being used, which is also not appropriate.

采纳的回答

even coil
even coil 2015-6-15
Turns out the pcolor command works for what I want.
colormap(gray(length(unique(Z))));
h = pcolor(B0,B1,Z);
caxis([-.5 1]);
xlim([-.2 1.6]);
ylim([-1 1]);
set(h, 'EdgeColor', 'none');

更多回答(2 个)

Walter Roberson
Walter Roberson 2015-6-12
If your Z is an array then you are using the contourf(X,Y,Z) form, and you instead want to use the contourf(X,Y,Z,v) form. As there are only a few discrete values, then
contourf(B0,B1,Z,unique(Z(:));

even coil
even coil 2015-6-12
Sorry, I should have included a MWE. The code is below. I attach the necessary data files for my example. Also, the plots are below.
clear;
close all;
B0 = load('B0.txt');
B1 = load('B1.txt');
fontsize = 20;
colormap(gray);
Z = load('Z.txt');
Z = round(Z*1000)/1000;
Z(abs(Z) == Inf) = 1;
% This creates many lines
contourf(B0, B1, Z);
caxis([-.5 1]);
xlim([-.2 1.6]);
ylim([-1 1]);
print('-dpng', 'Version1.png');
% Still wrong---there should be no light grey outline on the dark grey area
% (Look at Z---it goes immediately from the smallest value -.1420 to the largest
% value of 1, without going through the intermediate value 0. MATLAB is
% interpolating here.)
close;
colormap(gray);
contourf(B0, B1, Z, unique(Z));
caxis([-.5 1]);
xlim([-.2 1.6]);
ylim([-1 1]);
print('-dpng', 'Version2.png');

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by