How can I plot multiple 2d plots with a z value that changes the value of each line?

조회 수: 3 (최근 30일)
Hello,
I have x, y and z data. I would like to plot x and y data for all different z values using plot, and vary each line color based on the corresponding z value. I want to specfically use plot so that I may use straight lines, and so I am not interested in using scatter, nor contour.
For example...
colormap(jet(3))
hold on
plot(linspace(0,10),linspace(0,10)+1)
plot(linspace(0,10),2.*linspace(0,10)+1)
plot(linspace(0,10),.25.*linspace(0,10))
hold off
hc=colorbar;
cb=1:5:10;
set(hc,'YTick',cb, 'YTickLabel', {'1','5','10'})
This will return...
which is not what I want for multiple reasons. First, the lines do not match the jet colormap settings I have inputted. Second, the colorbar does not have all the tick labels. How can I fix this so that the color of the plot lines matches the colorbar and the ytick labels are shown correctly?

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 1월 15일
hello Alessandro
check this please
n = 3;
z = [1 2 0.25]';
z = sort(z,'ascend');
x = linspace(0,10);
y = z*x;
data_min = min(y,[],'all');
data_max = max(y,[],'all');
map = colormap(jet(n));
[mmap,nmap] = size(map);
hold on
for ci = 1:n
h = plot(x,y(ci,:));
set(h, 'Color', map(ci,:)) ;
end
hold off
hc=colorbar;
cb=(1/(2*n):1/n:1);
set(hc,'YTick',cb, 'YTickLabel', num2str(z))
hc.Title.String = "Z range";
  댓글 수: 2
Alessandro Maria Laspina
Alessandro Maria Laspina 2021년 1월 16일
편집: Alessandro Maria Laspina 2021년 1월 16일
How can I make this work for tiledlayout? If I use a tilelayout structure, what will end up happening is that the colorbar will go from a discrete color representation to a gradient where it gradually changes from one color to another, and it makes it harder to distinguish.
EDIT: nevermind it also works with tiledlayout.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by