Matlab Plot Colours
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Robbie
el 23 de Feb. de 2012
Hi,
I have some data, which plots two lines on a graph, when I run my code. I have to manually run my code for different datasets but I hold the original lines on the graph. Every subsequent time I run the code I get two additional lines. I want the colour of the new lines to be different every time I run the code, but I don't know how I can modify my code to do this. Because of the way the code is implemented I can't use a loop. Here is my code for plotting:
hold on
plot(Xupper,CPupper,'r',Xlower,CPlower,'r')
hold off
So instead of the line being red each time, i want the colour to change. Does anyone know how I can implement this?
Thanks
0 comentarios
Respuesta aceptada
Image Analyst
el 23 de Feb. de 2012
Try the 'Color' option to plot
randomColor = rand(3,1);
plot(x, y, 'Color', randomColor);
3 comentarios
Image Analyst
el 23 de Feb. de 2012
From the help:
plot automatically chooses colors and line styles in the order specified by ColorOrder and LineStyleOrder properties of current axes. MATLAB supports four line styles, which you can specify any number of times in any order. MATLAB cycles through the line styles only after using all colors defined by the ColorOrder property. For example, the first eight lines plotted use the different colors defined by ColorOrder with the first line style. MATLAB then cycles through the colors again, using the second line style specified, and so on.
So you want to do something like:
set(gca, 'ColorOrder', myColorOrder, 'NextPlot', 'replacechildren');
where myColorOrder is an N by 3 array of your custom colors. See my demo for setting ColorOrder here: http://www.mathworks.com/matlabcentral/answers/19815-explicitly-specifying-line-colors-when-plotting-a-matrix
Más respuestas (2)
Jan
el 23 de Feb. de 2012
map = colormap('lines', 16);
nDrawnAlready = numel(get(gca, 'Children')) / 2;
plot(x, y, 'Color', map(nDrawnAlready + 1, :));
Image Analyst
el 23 de Feb. de 2012
Robbie: This is a better demo than the link of my prior message that I referred to in my comment above. Just copy and paste. No special toolboxes needed. It's very well commented so you should be able to follow it easily.
% Unless you specify the 'Color' property when you plot,
% plots are plotted according to the 'ColorOrder' property of the axes.
% This demo shows how you can change the default color order of plots.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 18;
% Make 20 plots, with 25 data points in each plot.
numberOfDataSets = 20;
x = 1:25;
y = rand(numberOfDataSets, length(x));
% These y would all be on top of each other.
% Separate the plots vertically.
offsets = repmat((1:numberOfDataSets)', [1, length(x)]);
y = y + offsets;
% Get the initial set of default plot colors.
initialColorOrder = get(gca,'ColorOrder') % Initial
% See what the colors look like when plotted:
subplot(2, 1, 1);
plot(x,y, 'LineWidth', 3);
grid on;
caption = sprintf('%d plots with the Initial Default Color Order (Note the repeating colors)', numberOfDataSets);
title(caption, 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Make a new axes:
subplot(2, 1, 2);
% Create a new colormap that will define the new default color order property.
switch choice
case 1
newDefaultColors = jet(numberOfDataSets);
case 2
newDefaultColors = rand(numberOfDataSets, 3);
case 3
newDefaultColors = hsv(numberOfDataSets);
case 4
newDefaultColors = hot(numberOfDataSets);
case 5
newDefaultColors = cool(numberOfDataSets);
case 6
newDefaultColors = spring(numberOfDataSets);
case 7
newDefaultColors = summer(numberOfDataSets);
case 8
newDefaultColors = autumn(numberOfDataSets);
case 9
newDefaultColors = winter(numberOfDataSets);
case 10
newDefaultColors = lines(numberOfDataSets);
case 11
newDefaultColors = gray(numberOfDataSets);
case 12
newDefaultColors = bone(numberOfDataSets);
case 13
newDefaultColors = copper(numberOfDataSets);
otherwise
newDefaultColors = pink(numberOfDataSets);
end
% Note: You can build your own custom order if you want,
% just make up a array with numberOfDataSets rows and 3 columns
% where each element is in the range 0-1.
% Apply the new default colors to the current axes.
set(gca, 'ColorOrder', newDefaultColors, 'NextPlot', 'replacechildren');
% Now get the new set of default plot colors.
% Verify it changed by printing out the new default color set to the command window.
newColorOrder = get(gca,'ColorOrder')
% Now plot the datasets with the changed default colors.
plot(x,y, 'LineWidth', 3);
grid on;
caption = sprintf('%d plots with the New Default Color Order', numberOfDataSets);
title(caption, 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
msgbox('Done with ColorOrder demo!');
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!