How to show surface plot of 2D data?
조회 수: 8 (최근 30일)
이전 댓글 표시
Hi.
I have a set of data points, C at each x and y (for many cases). In 2D, the plot looks simply like this:
So this is a sample plot for one x. There are similar plots for other x values as well.
What I want is some kind of surface plot. How can I get it? I tried to use surf and contour, but they need their z to be a matrix, not an array.
For instance, the data looks like this:
% set1
x = [1 1 1 1];
y = [1 3 5 16];
C = [100 400 200 500];
% set2
x = [2 2 2 2];
y = [4 7 8 13];
C = [200 500 700 100];
Thank you
댓글 수: 0
채택된 답변
Star Strider
2019년 12월 10일
Try this (with your own vectors):
x = rand(10,1); % Create Data
y = rand(10,1); % Create Data
z = rand(10,1); % Create Data
[X,Y] = ndgrid(sort(x), sort(y)); % Create Interpolation Grids
Z = griddata(x, y, z, X, Y); % Interpolate ‘z’
figure
surf(X, Y, Z)
hold on
stem3(x, y, z, 'filled')
hold off
grid on
Experiment to get different results.
댓글 수: 9
Star Strider
2019년 12월 10일
As always, my pleasure!
You can make them all the same size with the interp1 or interp2 functions. It is slightly more work, however you can then use all your data.
For example, to extend ‘x1’, ‘y1’, and ‘C1’ to each have a length of 7:
x1i = x1(1)*ones(1,7);
y1i = interp1((1:numel(x1)), y1, linspace(1, numel(x1), numel(x1i)));
C1i = interp1((1:numel(x1)), C1, linspace(1, numel(x1), numel(x1i)));
You could probably create a function to do this.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Contour Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!