How can I create a surface plot (with height in Z) from X-Y coordinates and Z as a matrix?

21 ビュー (過去 30 日間)
I try to plot a surface plot (with height in Z) from X-Y coordinates and Z as a matrix.
X and Y are my coordinates from 1 to 100 and Z is a matrix containing the values I want to plot as a surface.
Here, my Z matrix is called mymatrix.
I tried to use the commands pcolor and surf but instead of getting a surface with surf I still get the same plot as the one generated with pcolor.
I have attached the figures I get as well as mymatrix.
Here is what I tried:
%% pcolor command
figure(1)
clf;
hold on;
pcolor(mymatrix);
cb = colorbar;
xlim([1 100]);
ylim([1 100]);
title(['pcolor command'],'FontSize', 24, 'FontWeight', 'Bold');
%% surf command
[X,Y] = meshgrid(1:100,1:100);
figure(2)
clf;
hold on;
grid on;
grid minor;
xlim([1 100]);
ylim([1 100]);
surf(X,Y,mymatrix);
cb = colorbar;
title(['surf command'],'FontSize', 24, 'FontWeight', 'Bold');
Does anyone know how to produce a surface plot with height and colours as the third dimension of my plot?
Thank you

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 8 月 12 日
Remove the clf and hold on commands from your plotting commands.
The coloring is already done by height, but by default, each panel is a solid color, with the color being determined by the Z value on the left/bottom edge. Change your FaceColor property to 'interp' to apply a gradient.
load mymatrix.mat
figure(2)
grid on
grid minor
surf(1:100,1:100,mymatrix,'LineStyle',"none","FaceColor","interp")
cb = colorbar;
title(['surf command'],'FontSize', 24, 'FontWeight', 'Bold')
  3 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 8 月 12 日
Not easily. Grid lines are drawn on the box containing the axes. You'd have to manually add the lines to your surface using secondary plot commands.
A LL
A LL 2021 年 8 月 12 日
ok, thanks again!

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 8 月 12 日
load('mymatrix.mat') ;
surf(mymatrix) ;
shading interp
colorbar
  3 件のコメント
KSSV
KSSV 2021 年 8 月 12 日
load('mymatrix.mat') ;
surf(mymatrix) ;
colorbar
Don't use shading interp.
A LL
A LL 2021 年 8 月 12 日
great! Thanks KSSV!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by