2D graph + Colour. With meshed grid of variables X and Y

I have a file containing the variables X Y and Z. Id like to plot a graph, on which theres a meshed grid of the variables X and Y (the grid lines should appear for every value of X and Y) and then plot the graph with the corresponding color of the variable Z, this variable depends of x and y.
So far i've tried the function scatter and came across a plotted graph, but the problem i have, is that my gridlines appear every 5 values of X and Y, instead of appearing for every value of X (from 0 to 23 by steps of 1) and Y (from 2 to 30, and by steps of 0.5). And with this function i get circles as markers where id like to get tiny rectangles as markers.
pointsize = 10;
scatter(X, Y, pointsize, Z);axis([00 23 2 30 ]);grid on;hold on;
The idea I have in mind of the plotted graph is like a chessboard, on which every square(in this case rectangle) should have a color matching the value of Z.
Id also would like to know how can be set the min and max values for Z color wise (if Z goes from 0.5 to 1).
Thanks !!

2 commentaires

Jürgen
Jürgen le 21 Août 2012
Modifié(e) : Jürgen le 21 Août 2012
Hi, it's a bit confusing: if you work with a grid of X and Y values , the Z values are actually the values at the grid points, not inside the squares, if you want to have each square in a color corresponding to the Z values, you probably need to estimated the Z value in the middle of the square and relate that one to the colors.
on the otherhand if you want to create a surface take a look at the surf command
Laura
Laura le 21 Août 2012
Looks like you were right Jürgen,
with the function peaks i think i get an estimation for my variables, the problem i have with surface is that the variable which i am aplying this has to be a matrix, not an escalar as it is on my code..
thanks for your quick response !!

Connectez-vous pour commenter.

Réponses (1)

Sven
Sven le 21 Août 2012
Modifié(e) : Sven le 21 Août 2012
Hi Laura, try this:
% Make your data (you've probably got your own X,Y,Z)
[X,Y,Z] = peaks;
% Show your data
figure, surf(X,Y,Z)
view(2) % This will give you a top-down view
The method above actually makes a 3D view (and then changes the camera angle to look at it top-down).
You can then set the colour range as follows:
colorbar % Just so you can see the colour scale
caxis([-2 3]) % Set new min/max limits on the colour scale

5 commentaires

Laura
Laura le 21 Août 2012
Hey Sven, i tried your code, and im not certain of how it works.. The x goes from 0 to 23 the y goes from 2 to 30 (at steps of 0.5) the z goes from 0.5 to 1
So for each combination of X and Y theres a value of Z.
With the function 'peaks' i get estimations for these values right? So when we do the 'surf' of these estimations i should get the plot i am looking for... and it looks like its the right one, only that if i set a marker at a random point of the graph the values of X Y abd Z dont correspond with the ones that should be. on the graph the limits of the axes are (x[-3..3] Y[-3..3] Z[-8..10])
how can i change these into the ones i mentioned before?
Thanks again for the feedback!
Laura, the "peaks" example is an example of how to make a "chessboard" picture like the one you were describing, but of course not with your data.
If you can get your data into a similar shape (ie, kind of like the chessboard you described) as the X and Y and Z matrices in the example, then the exact same type of picture can be made with your data.
I think the major problem is that your data is not in X, Y, Z chessboard-like matrices... But, it's not quite clear from your description what form your data is in.
Can you please describe very clearly what your X, Y, and Z are? Perhaps even paste them into a comment here.
For instance, when you call:
scatter(X, Y, pointsize, Z)
Do you truly get scattered values for X and Y, or are all your X and Y locations confined to a nice orderly grid?
If they are confined to a grid, then the task will be to rearrange your X, Y, Z vectors into grid form rather than vector form (we can help with that).
If your points are not confined to a grid, you will need to use interpolation to get gridded data. We can also help with that, but you need to show the kind of input you have. If we don't know your input, then we have to do things like use peaks() to make example input which may not be helpful if your real input is completely different.
Laura
Laura le 22 Août 2012
Modifié(e) : Laura le 22 Août 2012
Ok Sven. I have a file containing all my data (X and Y are both strings X represents Time and Y represents Frequency) and Z is double. So i read my file line per line and plot these variables into a graph with the function image.
Before i set them into the graph, i transform X and Y into doubles. So all the variables i pass to the function image are doubles. Heres my code if you want to check it out..
close all;
clear all;
%%Extract the date from current path (mes=dia1, dia=dia2)
currentfolder=pwd;
dia1(1)=currentfolder(93);
dia1(2)=currentfolder(94);
dia2(1)=currentfolder(96);
dia2(2)=currentfolder(97);
fileID = fopen('senyals_corr_ok','r'); %%Open file with data
B=fgets(fileID); %%Read title of file
[frecu,count] = fscanf(fileID,'%s',1); %%Read first line of file
[hora,count] = fscanf(fileID,'%s',1); %%Read first line of file
[corr,count] = fscanf(fileID,'%f',1); %%Read first line of file
%%transform frecu and hora into scalars
num_f=str2num(frecu);
num_f=num_f/1000;
num_h=str2num(hora);
figure(1);
image(num_h,num_f,corr);axis([00 23 2 30 0.5 1 0.5 1]);grid on;ylabel('Freq[MHz]');xlabel('Hora[00..23]');title(['Dia',num2str(dia2),'/',num2str(dia1),]);hold on;
while ~feof(fileID)%%Read rest of the file
B=fgets(fileID);
[frecu,count] = fscanf(fileID,'%s',1);
[hora,count] = fscanf(fileID,'%s',1);
[corr,count] = fscanf(fileID,'%f',1);
num_f=str2num(frecu);
num_f=num_f/1000;
num_h=str2num(hora);
image(num_h,num_f,corr);axis([00 23 2 30 0.5 1 0.5 1]);grid on;ylabel('Freq [MHz]');xlabel('Hora [00..23]');title(['Dia',num2str(dia2),'/',num2str(dia1),]);hold on;
end
fclose(fileID);
So the image i get has the right values, but i don't get the gridlines as id like to. If i use a marker to check the values of the variables at a certain point, i get X, Y, Index, and RGB. The X and Y variables value are set correctly. I have noticed that the value of the variable Z (corr) corresponds with the value of index.. is that OK? or for a marker i should get X,Y,Z?
It is interesting because on the graph all the points corresponding with Z(corr) have all the same color, when they have different values. So i guess i have to define the range of color for the variable Z(corr).. Could you guide me through that?
One thing i cant seem to fix is that the Y axes starts at 30 and ends at 2 when it should be the other way (starting at 2 and ending at 30).
I'd show you the graph but cant find a way to attach it to the post.
Thank youu!
Jürgen
Jürgen le 22 Août 2012
Modifié(e) : Walter Roberson le 23 Août 2012
Hey,
I think what Sven proposed is
X= frecu; % Column Array representing the frequencies
Y= hora; % Column Array representing time
Z=corr;% values of Z at (X,Y)
[Xgrid,Ygrid] = meshgrid(X,Y);
Zgrid=zeros(size(Xgrid);
% then fill Zgrid with the values of Z that you want at X en Y
% Zgrid(indexX, IndexY) = Zvalues
FigHandle=figure('Name','YourFigureName');
title(gca,'YourTitleName');
hold on
plot(MidCompAngleInDegCompNb(11:36,1),EstimatedSpeed,'^r');
surf(Xgrid,Ygrid,Zgrid)
view(2) % This will give you a top-down view
an the other hand if you use image see teh help file
'image(x,y,C), where x and y are two-element vectors, specifies the range of the x- and y-axis labels, but produces the same image as image(C).
'
maybe this helped,
Sven
Sven le 23 Août 2012
Hi Laura,
I think that using image() inside a loop (basically making many many plots of a single scalar pixel) is usually a bad idea. Generally, you should:
  1. Read all your data
  2. Organise your data into the right shape
  3. Display your data
I think that you misunderstand what we mean by shape because you are only plotting one scalar value at a time. Your final code will have one single call to image() or surf(), and the size of your X, Y, and Z data will be M-by-N (where M and N will be numbers much bigger than 1-by-1).
Please show us the data you are reading. Either upload the file somewhere, or at least copy/paste in the first few (~50) lines.

Connectez-vous pour commenter.

Catégories

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

Question posée :

le 21 Août 2012

Community Treasure Hunt

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

Start Hunting!

Translated by