How to show surface plot of 2D data?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
採用された回答
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 件のコメント
Steven
2019 年 12 月 10 日
Thanks.
The problem is x values are fixed each time, so for instance for your example:
x = ones(10,1); % Create Data
y = rand(10,1); % Create Data
z = rand(10,1); % Create Data
For this case, there would be an error of points being collinear. Right?
Star Strider
2019 年 12 月 10 日
My pleasure.
Please go into a bit of detail with respect to ‘x values are fixed each time’, since I do not understand what this means. There was no mention of that in the original Question.
The rand values would likely not be collinear, especially for such short vectors as in my example. My code threw no errors when I ran it, so griddata did not detect any non-unique values.
Steven
2019 年 12 月 10 日
I meant my x values are something like this:
x = [1 1 1 1 1 1];
Yes, your code throws no error with random numbers, but throws error for the above x values. Any idea how to resolve that?
Star Strider
2019 年 12 月 10 日
Your data might be gridded. (I would have to see it to be certain.) If that is true, you would only need to reshape your vectors to form matrices from them, then plot the matrices. You could later use griddata to interpolate the results to a denser grid, if you wanted to.
For instance, these are the data:
% 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];
How to either reshape or grid them then?
Thanks
Star Strider
2019 年 12 月 10 日
Yes.
Use the same reshape call with each vector, changing only the vector argument.
Star Strider
2019 年 12 月 10 日
I was not expecting that possibility.
Try this:
% set1
x1 = [1 1 1 1];
y1 = [1 3 5 16];
C1 = [100 400 200 500];
% set2
x2 = [2 2 2 2];
y2 = [4 7 8 13];
C2 = [200 500 700 100];
xm = [x1; x2];
ym = [y1; y2];
Cm = [C2; C2];
figure
surf(xm, ym, Cm)
grid on
Extend it for more vectors. Note that they all must have the same column sizes for this to work (that is, they all need to be row vectors with the same number of columns).
Unfortuantely some of them are not of the same size, but some of them are! But it works for those that are though :)
P.S., I edited the original post to include the data.
Thanks again
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 件)
カテゴリ
ヘルプ センター および File Exchange で Surface and Mesh Plots についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
