I have x,y Cartesian Coordinates and a value next to them that I would like to plot in color in the corresponding x,y.
xy = linspace(0,20,11);
x_y_value = [(combvec(xy,xy) )' rand(121,1)];
Here I would like to plot 2x2 squares at x,y (two first columns of x_y_value matrix) colored according to the value (third column of x_y_value matrix).
I am looking something like the attached image.
Thank you

 Réponse acceptée

Anton Semechko
Anton Semechko le 12 Juin 2018
Here is an example:
x_lim=[0 20];
x=linspace(x_lim(1),x_lim(2),11);
[X,Y]=meshgrid(x);
F=randn(size(X)).*X-randn(size(X)).*Y;
figure('color','w')
imagesc(x_lim,x_lim,F)
axis equal
set(gca,'XLim',x_lim+[-1 01],'YLim',x_lim+[-1 1])

6 commentaires

Spyros Polychronopoulos
Spyros Polychronopoulos le 12 Juin 2018
Thank you Anton Semechko,
Is there maybe a way to just plot the square points at x,y coordinates without having to create an 11x11 matrix (F) but rather having the values of each point in a 1x121 matrix?
Yep, here is one approach:
xy = linspace(0,20,11);
x_y_value = [(combvec(xy,xy) )' rand(121,1)];
figure('color','w')
sz=1000; % size of the squares
h=scatter(x_y_value(:,1),x_y_value(:,2),sz,x_y_value(:,3),'filled','s');
axis equal
Spyros Polychronopoulos
Spyros Polychronopoulos le 12 Juin 2018
Thank you very much Anton, it worked nicely. The only issue is that I set the squares to be sz=1000 and then I have gaps between them or when I zoom in/out they are not 1by1 anymore. Please check the attached plots.
<<
<<
>>
>>
Ok, here is yet another approach:
xy = linspace(0,20,11);
x_y_value = [(combvec(xy,xy) )' rand(121,1)];
N=size(x_y_value,1);
dx=xy(2)-xy(1);
C=(dx/2)*[-1 -1; 1 -1; 1 1; -1 1];
figure('color','w')
for i=1:N
Ci=bsxfun(@plus,C,x_y_value(i,1:2));
h=fill(Ci(:,1),Ci(:,2),x_y_value(i,3));
set(h,'EdgeColor','k')
hold on
end
axis equal
XLim=[min(x_y_value(:,1)) max(x_y_value(:,1))]+dx*[-1 1];
YLim=[min(x_y_value(:,2)) max(x_y_value(:,2))]+dx*[-1 1];
set(gca,'XLim',XLim,'YLim',YLim)
Spyros Polychronopoulos
Spyros Polychronopoulos le 12 Juin 2018
This is exactly what I was looking for! I have never came across bsxfun. Thank you again Anton!
Anton Semechko
Anton Semechko le 12 Juin 2018
You are welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by