How to fix contourf plot interpolating across NaN region? - matlab

I'm trying to create a countourf plot with the region in the bottom left hand corner whited out. Values for the contour are provided at the corner of each rectangle in the below image and all other points on the mesh have a value of NaN.
I want to know how to stop the countourf plot from drawing the triangular section at the top right of the white square, i.e. how do I stop it from interpolating across these two values.
End game: I would like a complete white rectangle on the bottom left, not a chamfered rectangle.

You are getting that triangle because on that specific square your data looks something like:
[1 2 ;...
NaN 3]
And that is a completely valid upper right triangle to contour.
So you can interpolate your data to get more resolution and make that triangle smaller. Or you could just use patch to add a white square at the desired position.
data=rand(8);
data(1:4,1:4)=NaN;
contourf(data)
hold on;
patch([1 1 5 5],[1 5 5 1],'w')

It looks like seven squares a side, so we can set
x = 1:7;
[XX,YY] = meshgrid(x); % create x,y grid for the square
ZZ = nan(7); % create number grid for the square
ZZ(1:3,1:3)=ones(3); % set the lower 3x3 to 1
figure;
hold on % hold your plot
plot()% your contour
imagesc(XX,YY,ZZ); % Or similar
This is the outline, I can't determine what exact plotting function you need since you didn't show your code. In general, the idea is to create a grid as large as that of your contour plot, and set the lower left square to 1, thus white, leaving the rest NaN, thus not plotted.

Related

Changing perspective of Matlab plots

Say I have a matrix hey 15x15. I want to plot the value of the matrix as a 2D plot for better visualization. But Matlab plots with the convention that origin is in bottom-left corner and positive x is along the left and positive y is along the up direction from origin.
but i want to make my plots such that origin is in top-left corner, +ve x is left and +ve y is down.
So i just used a slight trick.
figure
axis([0 15 -15 0]);
daspect([1,1,1])
hold on
rectangle('Position',[3,-6,2,3],...
'EdgeColor','black',...
'LineWidth',2,...
'FaceColor','cyan')
for i=1:nrows
for j=1:ncolumns
if char(hey(i,j))=='^'
text(j,-i,'^');
elseif char(hey(i,j))=='>'
text(j,-i,'>');
elseif char(hey(i,j))=='v'
text(j,-i,'v');
elseif char(hey(i,j))=='<'
text(j,-i,'<');
end
if obstacle(i,j)==1
text(j,-i,'X');
end
end
end
text(goalY,-goalX,'T');
I made the transformation (x,y)-->(y,-x). But the downside is that the axes are then numbered along y as -1 to -15. However if reader was following above, i only wanted to plot the matrix values and in matrix the y runs +ve downwards from 1 to 15 for my case.
So i want the plot to show +1 thru +15 along y with origin at top-left and x graduated as it is but the values +1 to +15 written at the top of the plot rather than below.
How to do this? In the extreme case, i am alos willing to transfer the matrix hey to another software that can do the nice plot as i want. If any of the two alternatives is possible, please give concrete steps to do it.
EDIT:
After using the helpful methods below, i still have to use a trick like plot (j,i) instead of the innocent plot(i,j). This is because for matrix (i,j) is mapped to graph plot (x,y) as x=j, y=i. Is there a similar workaround? a matrix element is (row #, column #). But in 2D matlab graph, we will denote it's position as (column #, row #). I was just guessing if there was some matlab in-built function to take care of this. like i will give it (row #, column #) but matlab will plot (column #, row #). Is there such a function?
I think axis ij does what you want:
axis ij places the coordinate system origin in the upper left corner. The i-axis is vertical, with values increasing from top to bottom. The j-axis is horizontal with values increasing from left to right.
To locate the x axis on top, change the 'XAxisLocation' of the axes to 'top' (default is 'bottom').
Example:
x = 1:10;
y = x.^2;
plot(x,y)
axis ij
set(gca,'XAxisLocation','top')
Original plot (lines 1-3 of above code):
After axis ij (line 4):
After set(gca,'XAxisLocation','top') (line 5):
If I followed correctly you are looking for the axes XAxisLocation and YDir properties. You can set them to top and reverse respectively to get the output you want. You can also set the XTick property to 1:15 to show every value from 1 to 15.
Example:
clear
clc
%// Create dummy data
[x,y] = meshgrid(1:15,1:15);
u = cos(x).*y;
v = sin(x).*y;
figure
quiver(x,y,u,v)
set(gca,'XAxisLocation','top','XTick',1:15,'YDir','reverse')
hold on
%// I changed the coordinated of the rectangle to fit with the change in
%y-axis.
rectangle('Position',[3,3,2,3],...
'EdgeColor','black',...
'LineWidth',2,...
'FaceColor','cyan')
axis([0 15 0 15])
Which gives the following:

"Density" plots in Matlab, but not in the sense of density of data points

I would like to plot a sort of "density map" in Matlab, but have not found the right tool yet.
I have "continuous" data with x between (x_min and x_max), and y between (y_min and y_max). At each of these pairs of points (x_i,y_i), there is associated to it a value between 0 and 1.
I would like to plot this information in a 2d graph, such that in each small square containing (x_i,y_i) the plot shades the square black for the value 0, white for the value 1, and the appropriate shade of gray for intermediate values.
Can this be done easily in Matlab?
http://www.mathworks.com/help/images/ref/mat2gray.html seems to do exactly what I need.
If the data is in a matrix A, you can just use
image(255*A); colormap gray(256); axis image;
I'm not sure what you mean by continuous (uniformly spaced?), so my answer won't make too many assumptions other than that there is a reason why you mention the coordinates (if just a regular mesh, then just image or imagesc). So, only assuming your x and y coordinates are possibly non-uniformly spaced, but at least monotonically increasing samples, try surf with view(2):
surf(X,Y,data)
view(2)
colormap gray
By default surf sets the FaceColor property with the 'flat' option:
flat — The values of CData determine the color for each face of the surface. The color data at the first vertex determine the color of the entire face.
In other words, the value will determine the shade.
Assuming your data is in data and your x and y coordinates are in x and y, here is how to do it:
imagesc(x, y, data) % to create a heat map
colormap(gray) % for gray levels
caxis([0 1]) % to set 0 to black and 1 to white
axis xy % if you want the y axis to point up
colorbar % to display the colorbar

How do I fill in the area between two lines and a curve that's not straight in MATLAB (the region is not a polygon)

Using matlab's FILL function creates a filled region confined by a polygon with straight edges:
Unfortunately this leaves a small white region in the figure above, because the boundary of the region I want filled in is not a straight-edged polygon, but rather has a curved boundary on the left side. I have a curve (nearly parabolic but not exactly), and I want to fill in the region between two horizontal lines AND the curve itself. I also looked into the MATLAB function IMFILL, but with no luck.
What you need to do is make a polygon with more corners, so that it fits the curve more smoothly:
%# create a parabola and two straight lines
x = -3:0.1:3;
y = x.^2/4;
plot(x,y)
hold on, plot([-3 3],[1 1],'r',[-3 3],[2 2],'r')
%# create a polygon that hugs the parabola
%# note that we need to interpolate separately
%# for positive and negative x
x1 = interp1(y(x<0),x(x<0),1:0.1:2);
%# interpolate in reverse so that the corners are properly ordered
x2 = interp1(y(x>0),x(x>0),2:-0.1:1);
%# fill the area bounded by the three lines
fill([x1,x2],[1:0.1:2,2:-0.1:1],'g')

How can I create a rectangle with an outlined border?

I want to draw a rectangle to outline an area of an image that I've plotted in one axes of a figure. I have multiple axes in this figure, so I am using the rectangle() function. What I want is to draw a white rectangle a thin black border just inside and just outside the rectangle. The part of the image inside the rectangle should be visible, so all 'facecolor' should be 'none'. I have tried drawing 3 rectangles, two black ones with thin linewidths and one thicker white one, but the problem is that 'Position' is defined in axes units and 'LineWidth' is defined in point units, so that the scaling doesn't work too well, especially when the figure is resized.
FYI, the outline is so that the white rectangle is more visible against a light background. The images plotted vary widely, so a single color won't be universally visible for my data.
Any suggestions on how I can do this?
How about just using different line widths for black and white rectangle?
imshow('cameraman.tif')
rectangle('position',[80 30 100 100],'edgecolor','k','LineWidth',4)
rectangle('position',[80 30 100 100],'edgecolor','w','LineWidth',1)
Hmm, the corners look much better on MATLAB figure than as PNG file.
Better with getframe:
I like #Yuks solution. But there is another possibility that you can consider:
You could also calculate the mean value of the pixels inside the rectangle, and set the box color to the inverse. In this way, you will always have a good contrast.
Here is the code:
function PlotRect(im,x,y,w,h)
m = double(im( round(y): round(y+h) , round(x): round(x+w),:));
if (mean(m(:)) < 255/2)
col = [1 1 1];
else
col = [0 0 0];
end
rectangle('Position',[x y w h],'EdgeColor', col);
end
And the test:
function Inverse()
im = imresize( uint8(0:5:255), [250, 400]) ;
figure;imshow(im); hold on;
PlotRect(im,5,8,50,75);
PlotRect(im,100,30,25,42);
PlotRect(im,200,10,40,40);
PlotRect(im,300,10,40,40);
end
Yuk's solution works quite well for adding a rectangle to a normal MATLAB plot, too. The 'position' values are not interpretet as pixels but are adjusted to the plot values (see code example below):
figure;
plot(0:10,0:10); grid on;
hold on;
rectangle('position',[1 1 8.5 8.5],'LineWidth',2);
hold off;
This code results in the following plot:

Relative Markersize in Matlab plots

I am trying to plot a matrix where each element is in one out of two states. (ising model..)
Now, I would like to have one state colored and the other one white. That works using
[i,j] = find(S);
figure(gcf);
plothandle = scatter(i,j);
axis([0 nNodes+1 0 nNodes+1]);
when S holds the Spins and one state is equal to 0. (find returns a matrix of only non-zero elements)
To have a useful plot, the sizes of the markers should be 1x1 in RELATIVE coordinates. So if the whole matrix S would be in a state non-zero, everything would be colored.
However, it seems like Matlab only allows MarkerSizes in points or inches. How could I solve this?
One idea I had was, that I find out the point-size of the axes and then can easily calculate how big my markers should be. Then I would have to create a callback function if I want to zoom in and so on. Also, I have not yet found a way (without the image acq. toolbox) to find out the absolute size of my axes.
To clarify what I want: How could I plot a chessboard using a matrix with 1 for black and 0 for white fields?
For displaying data of this sort I generally prefer IMAGE or IMAGESC to PCOLOR since PCOLOR won't display the last row and column of the matrix when using faceted shading (the default). Also, IMAGE and IMAGESC flip the y axis so the image more intuitively matches what you think of when looking at a matrix (i.e. rows start from 1 at the top). You can visualize your matrix like this:
S = round(rand(20)); %# Sample 20-by-20 matrix of ones and zeroes
imagesc(S); %# Plot the image
colormap([1 1 1; 0 0 0]); %# Set the colormap to show white (zero elements) and
%# black (non-zero elements)
And here's a sample image:
Just as a suggestion, you can try using pcolor instead of `scatter' Example:
pcolor(hadamard(20))
colormap(gray(2))
axis ij
axis square