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

조회 수: 42 (최근 30일)
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.
figure
for H = 1:A
plot( H*sin(time), 'color', plotColor(:,H) )
hold on
end
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
Hans123
Hans123 2019년 7월 22일
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

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

채택된 답변

Hans123
Hans123 2019년 7월 22일
편집: Hans123 2019년 7월 22일
Hope this helps
clc; close; clear
time = 0:0.1:5; %seconds
A = 10; %number of plots
colormap(jet(10))
jetcustom = jet(10);
figure
for H = 1:A
plot(time,H*sin(time), 'Color', jetcustom(H,:))
hold on
end
xlabel('Time (s)')
ylabel ('Your Y label')
colormap(jet(10))
cb = colorbar;
caxis([-10 10])
ylabel(cb,'Height')

추가 답변 (1개)

Adam Danz
Adam Danz 2019년 10월 9일
편집: Adam Danz 2019년 10월 10일
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
figure()
hold on
for i = 1:15
plot([0,1],[i,i],'-','LineWidth',2)
end
% 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);
colormap(axh,cmap(1:nLines,:));
cbh = colorbar();
caxis([1,nLines+1]) % tick number 'n' is at the bottom of the n_th color
ylabel(cbh,'Line number')
  댓글 수: 2
Shashank Pathrudkar
Shashank Pathrudkar 2023년 2월 7일
Hello Adam,
for scatter: scatter(x,y,sz,c) specifies the circle colors. You can specify one color for all the circles, or you can vary the color. For example, you can plot all red circles by specifying c as "red".
Where if I give c as an vector of length equal to x and y, I get the points colored according to the values in c.
Is there any such option for plot?
Example:
figure; plot(randn(100,5)) % this will plot 5 signals of length 100
Now I want colors for these signals based on another vectorr, say c = [0.1 4 2.5 3 10]
Adam Danz
Adam Danz 2023년 2월 7일
The plot function does not have an option to specify groups of colors. However, there are two relatively easy ways to accomplish this.
Set axis ColorOrder
When not defined by the user, line color is determined by the axis ColorOrder property.
Either set it before adding the line objects,
figure()
ax = axes();
ax.ColorOrder = [1 0 0; 0 1 0; 0 0 1; 1 1 0; 0 1 1];
hold on % important
plot(rand(100,5)+(1:5), 'o')
or after adding the line objects,
figure()
ax = axes();
plot(rand(100,5)+(1:5), 'o')
ax.ColorOrder = [1 0 0; 0 1 0; 0 0 1; 1 1 0; 0 1 1];
Set the line object color properties
Alternatively, you can directly set the color of the line objects. When you plot an nxm matrix, plot will return m handles.
figure()
h = plot(rand(100,5)+(1:5), 'o')
h =
5×1 Line array: Line Line Line Line Line
colors = [1 0 0; 0 1 0; 0 0 1; 1 1 0; 0 1 1];
for i = 1:numel(h)
h(i).Color = colors(i,:);
end

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

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by