I want to plot a function z = f (x,y), but I want the lines (y-axis) to be the color scale.
This is my code:
% parameters
x= [0:1:30]' ;
y= linspace(0.1,10,length(x)) ;
% function
if (x <= max(x)*ones(size(x)) )
z = (x./max(x)).^y ;
else
z = 1 ;
end
% Plot
fig1 = plot3(x,y,z) ;
view([-120 20])
xlabel('x')
ylabel('y')
zlabel('z')
colorbar

2 件のコメント

Rahul Kumar
Rahul Kumar 2019 年 9 月 23 日
I think you should change the sequence of plot as x,z,y.
Aikaterini Mountraki
Aikaterini Mountraki 2019 年 9 月 23 日
編集済み: Aikaterini Mountraki 2019 年 9 月 23 日
Than you for your reply, but even so, the lines still have random colors, they are not scaled as a function of y.
Moreover, what you are suggesting can also be done without changing the order of the plot with:
caxis(ylim)

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

 採用された回答

darova
darova 2019 年 9 月 23 日
編集済み: darova 2019 年 9 月 23 日

0 投票

Why not use surf()?
Color lines using patch()
clc,clear
% parameters
x= [0:1:30]' ;
y= linspace(0.1,10,length(x)) ;
[X,Y] = meshgrid(x,y);
X(end+1,:) = nan; % put NaN where line ends
Y(end+1,:) = nan;
% function
Z = (X./max(x)).^Y ;
% plot preparations
n = length(x);
cm = jet(n); % create 31 colors
z0 = linspace(min(Z(:)),max(Z(:)),n);
cmap = interp1(z0,cm,Z(:)); % get color for each Z
% cmap(isnan(cmap)) = 0; % replace NaN
% Plot
cla
f = 1:length(Z(:))-1;
p = patch('Vertices',[X(:),Y(:),Z(:)], ...
'Faces', [f' f'+1],...
'EdgeColor','interp', ...
'FaceVertexCData',cmap);
% 'Marker','.');
view([-120 20])
xlabel('x')
ylabel('y')
zlabel('z')
colorbar
% if graphical driver fails
% opengl software

その他の回答 (1 件)

Ankit
Ankit 2019 年 9 月 23 日

1 投票

Hello Aikaterini,
Please try this out:
% parameters
x= [0:1:30]' ;
y= linspace(0.1,10,length(x)) ;
% function
if (x <= max(x)*ones(size(x)) )
z = (x./max(x)).^y ;
else
z = 1 ;
end
% Plot
fig1 = plot3(x,y,z) ;
view([-120 20])
xlabel('x')
ylabel('y')
zlabel('z')
cb = colorbar;
set(gca, 'clim', get(gca, 'ylim'));
set(cb, 'Ticks', get(gca, 'ytick'));
Regards
Ankit

2 件のコメント

Aikaterini Mountraki
Aikaterini Mountraki 2019 年 9 月 23 日
Ankit thank you for your reply. It is an interesting suggestion, but the lines still get random colors.
Ankit
Ankit 2019 年 9 月 23 日
Just give it a try! here i get the colormap and the later set it to the colorbar. But I dont see the difference Just to give you an Idea!
% parameters
x= [0:1:30]' ;
y= linspace(0.1,10,length(x)) ;
% function
if (x <= max(x)*ones(size(x)) )
z = (x./max(x)).^y ;
else
z = 1 ;
end
% Plot
fig1 = plot3(x,y,z) ;
view([-120 20])
xlabel('x')
ylabel('y')
zlabel('z')
cmap = colormap(gca);
cb = colorbar;
set(gca, 'clim', get(gca, 'ylim'));
set(cb, 'colormap', cmap);
set(cb, 'Ticks', get(gca, 'ytick'));

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

カテゴリ

ヘルプ センター および File ExchangeColor and Styling についてさらに検索

製品

リリース

R2019a

質問済み:

2019 年 9 月 23 日

編集済み:

2019 年 9 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by