Plotting a 2D Contour from data (A variable with its XY coordinates)

Hello!
I need to plot a 2D Contour form data. I have a variable called "Intensity" with 224x1 values and I have a variable called "XYAxis" with the corresponding coordinates of this Intensity (224 points (X,Y)).
What i want to do is to plot both variables, to have a contour2D with the distribution of the Intensity among the points analized (each value of Intensity corresponds to one point(X,Y).
I have tried:
Contour (XYAxis,Intensity);
But it only displays an empty plot and the axis are not those i desire (i want the contour to be like scattered).
Any idea? Maybe TriScatteredInterp()?? but i need the contour to be 2D
Thanks in advanced

댓글 수: 2

Could you provide a small set of sample data?
Sure,
For example, I have one variable called " XYAxis " (224x2 double) this way (1º column is X and 2º Column is Y):
0 0
0 5
5 10
10 20
20 30
... until 224 points
And I have another variable called " Intensity " like this:
0.5
0.8
0.9
0.10
0.11
...
until 224 values
So each value of "Intensity" corresponds with one value of "XYAxis" And i want to plot it with a contour 2d. Thanks

댓글을 달려면 로그인하십시오.

 채택된 답변

Sean de Wolski
Sean de Wolski 2014년 12월 29일
편집: Sean de Wolski 2014년 12월 29일
% Synthetic Data
xy = rand(20,2);
intensity = rand(20,1);
% Interpolate to grid
interpolant = scatteredInterpolant(xy(:,1),xy(:,2),intensity);
% Grid
[xx,yy] = meshgrid(linspace(0,1,10)); % replace, 0 1, 10 with range of your values
% Interpolate
intensity_interp = interpolant(xx,yy);
contourf(xx,yy,intensity_interp)
You'll have to interpolate it to a grid and then call contour.

댓글 수: 9

Thank for the explanation and the code, but when i run it, it displays this error message:
??? Undefined function or method 'scatteredInterpolant' for input arguments of type
'double'.
Both variables "XYAxis" and "Intensity" in my case are double
You must be using an old release. TriScatteredInterp was the predecessor to scatteredInterpolant and griddata the predecessor to TriScatteredInterp.
For this example, I believe scatteredInterpolant can be directly substituted for TriScatteredInterp. Of course, if your company or university if current on maintenance, you can upgrade to the newest release for no additional cost.
Thank you very much for that info. I tried with TriScatteredInterp and it worked.
However, i would like to know if by defining linespace(....) i am fixing the size of my axis...because my axis are not a square (i mean, with linespace my plot displays same lenth for x axis and same lenght for y and my data is not this way).
I want the mesh to be just the points of my data (only my X and Y coordinates) no others. (Like scattered).
Do you know how could i do it?
Thanks!
xv = linspace(min(xy(:,1)),max(xy(:,1)),20);
yv = linspace(min(xy(:,2)),max(xy(:,2)),20)
[xx,yy] = meshgrid(xv,yv)
The grid does not need to be square.
You could also use
ux = unique(xy(:,1))
uy = unique(xy(:,2))
etc.
Thank you very much, it really worked and I understood your code.
Could you please tell me, if you know how to save the contour plot into an image , for example jpg?. Wich image format would your recommend?
Thanks
For single frame images, I usually use PNG since it's really common, uses lossless compression, and supports transparency.
You can use print/saveas/hgsave etc.
saveas(gcf,'test.png')
Peter
Peter 2014년 12월 29일
편집: Peter 2014년 12월 29일
I mean "automatically", with the names of the axis on it and a legend
xlabel('whatever x is')
ylabel('whatever y is')
filename = ['file' num2str(1) '.png'] % replace 1 with whatever you want automatically created.
saveas(gcf,filename)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

태그

질문:

2014년 12월 29일

댓글:

2014년 12월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by