MATLAB Answers


Create plot with multiple overlayed lines, where colorbar corresponds to color of line

Michael1997 님이 질문을 제출함. 22 Jul 2019
최근 활동 Adam Danz 님이 편집함. 10 Oct 2019
my goal is to create a colorbar to represent the colors of data lines plotted on a figure
the color of the lines follow the jet colormap pattern, so I feel this should be possible to do in Matlab.
for example:
time = 0:.1:5; %seconds
A = 10: %number of plots
plotColor = jet(A); %Creates a 10 by 3 matrix of colors transitioning from dark blue to dark red.
for H = 1:A
plot( H*sin(time), 'color', plotColor(:,H) )
hold on
In this example I want to have a color bar to represent the height of the sine wave as a function of the color of the lines.
I haven't been able to figure out a way to implement this yet and I would appreciate any help! Thank you in advance.

  댓글 수: 1

The reason you did not get colored plots was because you referenced the newly created color array in the incorrect way - all the rows of column H
plot( H*sin(time), 'color', plotColor(:,H) )
It should be all the columns (RGB) of row H
plot( H*sin(time), 'color', plotColor(H,:) )
Hope that cleared out any confusion

로그인 to comment.




답변 수: 2

Hans123 님의 답변 22 Jul 2019
Hans123 님이 편집함. 22 Jul 2019
 채택된 답변

Hope this helps
clc; close; clear
time = 0:0.1:5; %seconds
A = 10; %number of plots
jetcustom = jet(10);
for H = 1:A
plot(time,H*sin(time), 'Color', jetcustom(H,:))
hold on
xlabel('Time (s)')
ylabel ('Your Y label')
cb = colorbar;
caxis([-10 10])

  댓글 수: 2

Thank you, I was able to figure it out with your help!

로그인 to comment.

Adam Danz 님의 답변 9 Oct 2019
Adam Danz 님이 편집함. 10 Oct 2019

In addition to Hans' method, you can also apply the colorbar based on whatever line colors are already set up in the "ColorOrder" property of your axes.
% Create demo plot
hold on
for i = 1:15
% Get the axis handle and the number of lines drawn
% Usually you can skip this step because you already have
% these two variables.
axh = gca();
nLines = length(findall(axh,'Type','line'));
% Produce a colormap based on the ColorOrder of the axis
cmap = axh.ColorOrder;
cmap = repmat(cmap, ceil(nLines/size(cmap,1)), 1);
cbh = colorbar();
caxis([1,nLines+1]) % tick number 'n' is at the bottom of the n_th color
ylabel(cbh,'Line number')

  댓글 수: 0

로그인 to comment.

Translated by