How to Automatically Change Line Color with plotyy?

5 views (last 30 days)
I am trying to have Matlab automatically cycle through the color order when using plotyy. Currently, every additional line added to the first y-axis has the same color unless I manually change it. For the second y-axis, this is not an issue because I am only plotting one line. Thank you for your help in advance!
[ax, h1, h2] = plotyy(Time, EngineCoolant, Time, EngineSpeed);
line(Time, EngineCoolantIntoHX, 'Parent', ax(1),'Color', [0,.5,0]);
line(Time, EngineCoolantIntoTOC, 'Parent', ax(1),'Color', [1,.5,0]);
line(Time, EngineCoolantOutOfHX, 'Parent', ax(1),'Color', [0,.5,1]);
line(Time, ClntAftInnercooler, 'Parent', ax(1),'Color', [0,.5,.5]);
set(h2,'color','red')
set(ax,{'ycolor'},{'k';'k'})

Accepted Answer

Matt Fig
Matt Fig on 9 Aug 2012
The behavior you are seeing is the documented behavior or the LINE function. Try this:
Time = 0:.01:10;
EngineCoolant = Time.^2;
EngineSpeed = Time.^8.1;
EngineCoolantIntoHX = Time.^2.2;
EngineCoolantIntoTOC = Time.^2.3;
EngineCoolantOutOfHX = Time.^2.4;
ClntAftInnercooler = Time.^2.5;
[ax, h1, h2] = plotyy(Time, EngineCoolant, Time, EngineSpeed);
hold all
plot(ax(1),Time, EngineCoolantIntoHX,Time, EngineCoolantIntoTOC,...
Time, EngineCoolantOutOfHX,Time, ClntAftInnercooler)
set(h2,'color','red')
set(ax,{'ycolor'},{'k';'k'})

More Answers (1)

Image Analyst
Image Analyst on 15 Aug 2012
For what it's worth, here is a color order demo I wrote where you can specify whatever color order you want.
% In the help it says this:
%
% "plot automatically chooses colors and line styles in the order
% specified by ColorOrder and LineStyleOrder properties of current axes.
% ColorOrder : m-by-3 matrix of RGB values
%
% Colors to use for multiline plots. Defines the colors used
% by the plot and plot3 functions to color each line plotted.
% If you do not specify a line color with plot and plot3,
% these functions cycle through the ColorOrder property to
% obtain the color for each line plotted. To obtain the current ColorOrder,
% which might be set during startup, get the property value:
%
% get(gca,'ColorOrder')
%
% Note that if the axes NextPlot property is replace (the default),
% high-level functions like plot reset the ColorOrder property
% before determining the colors to use. If you want MATLAB to
% use a ColorOrder that is different from the default,
% set NextPlot to replacechildren. You can also specify your
% own default ColorOrder."
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
originalColorOrder = get(gca,'ColorOrder') % Initial
% Change to new colors. You can get these via Photoshop if you want.
% Make sure you divide by 255 to get them in the range 0-1.
myColorOrder = [...
255 0 0;... % Red.
0 255 0;... % Green
0 0 255;... % Blue
0 255 255;... % Cyan
255 0 255;... % Magenta
255 255 0;... % Yellow
0 0 0;... % Black
255 128 0]/255 % Orange
% You can extend this by more rows if you want more colors.
numberOfColors = size(myColorOrder, 1)
set(gca, 'ColorOrder', myColorOrder, 'NextPlot', 'replacechildren');
% The things I put in the set() command are especially important.
newColorOrder = get(gca,'ColorOrder') % Verify it changed
% newColorOrder should be the same as myColorOrder
promptMessage = sprintf('Do you want a bar chart, line plot,\nor Cancel to quit?');
button = questdlg(promptMessage, 'Which type of chart', 'Line Plot', 'Bar Chart', 'Cancel');
if strcmp(button, 'Cancel')
return;
elseif strcmpi(button, 'Line Plot');
linePlot = true;
numberOfXSamples = 30; % Good values are 30 for plot and numberOfColors for bar.
else
linePlot = false;
numberOfXSamples = numberOfColors; % Good values are 30 for plot and numberOfColors for bar.
end
% Make up some random sample data:
x = 1:numberOfXSamples;
y = zeros(numberOfColors, numberOfXSamples);
for k = 1 : numberOfColors % 8 sets of data.
y(k,:) = k + rand(1, numberOfXSamples);
end
if linePlot
% Lineplot
% Now plot with changed colors.
plot(x,y, 'LineWidth', 3);
else
% Bar chart:
bar(x, y, 'BarWidth', 1);
colormap(myColorOrder);
end
grid on;
fontSize = 20;
title('Plot of 8 lines with custom ColorOrder', 'FontSize', fontSize);
xlabel('X Axis', 'FontSize', fontSize);
ylabel('Y Axis', 'FontSize', fontSize);
legend('Plot 1 = Red', 'Plot 2 = Green', 'Plot 3 = Blue', ...
'Plot 4 = Cyan', 'Plot 5 = Magenta', ...
'Plot 6 = Yellow', 'Plot 7 = Black', 'Plot 8 = Orange');
% Scale the y axis from 0 to 9:
ylim([0 ceil(max(y(:)))]);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!