Matlab Default Color Orders and Types - matlab

I'm looking for a way, preferably a GUI, to change the default line colors, their orders, and the types globally.
I want to specify that the first line to be this color, this type, and this width; the second line to be that color, that type, and that width; and so on.
Maybe a uitable with the row names being the line number (the first, the second, etc.), the colors specified in the second column, the type in the third, and the width in the fourth. Something like this.
What is a good approach to solve this problem?

This is not fully the customization you are looking for, but you can set the default colors and line types (--, -., an so on) using DefaultAxesColorOrder and DefaultAxesLineStyleOrder, respectively:
myColorOrder = [
0 0.4470 0.7410 % rgb triplet
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250
0.4940 0.1840 0.5560
0.4660 0.6740 0.1880
0.3010 0.7450 0.9330
0.6350 0.0780 0.1840]
set(groot,'DefaultAxesColorOrder',myColorOrder,...
'DefaultAxesLineStyleOrder','-|--|:|-.')
Z = peaks;
x = 1:length(Z);
y = Z(4:7,:);
plot(x,y)
The rows in the 3-column matrix myColorOrder contain RGB triplets describing colors, and the row ordering of this matrix corresponds to line color order when setting DefaultAxesColorOrder property. The default line styles (set to DefaultAxesLineStyleOrder property) is a single string containing line styles separated by |.
Note that when setting several colors as well as line styles, plots will cycle as follows:
For first line style: cycle through all colors
Change to next line style: cycle through all colors
...
For details, see
http://se.mathworks.com/help/matlab/creating_plots/set-default-line-styles.html
If you really want to customize, you'll probably need to write a plot wrapper to customize plot line specs in the "manual way", however performed cleverly by the wrapper. See e.g. the code for Arrow3 by Tom Davis:
The current LineStyleOrder property will be used if LineStyle is
specified with '*'. MATLAB cycles through the line styles defined by
the LineStyleOrder property only after using all colors defined by the
ColorOrder property. If however, the global variable LineWidthOrder
is defined, and LineWidth is specified with '/', then each line will
be drawn with sequential color, linestyle, and linewidth.
Possibly you can make use of Tom Davis' approach and apply to Matlab line plots.

Related

Matlab legend colors don't match lines

Not sure what is going wrong here. I created a minimal example below:
clear
steps = 1:6;
labels = cell(length(steps),1);
xvals = 1:10;
fig = figure(1);
ax = axes('parent',fig);
hold on
for ii=1:length(steps)
s=steps(ii);
yvals = zeros(length(xvals)) + ii;
labels{ii} = ['gain = ' num2str(s)];
plot(ax,xvals,yvals);
end
legend(ax, labels);
hold off
And the result on my system is:
With less lines it can even put colors in the legend that aren't even on the plot. What is happening?!
Explanation of what's happening
The problem is in the line
yvals = zeros(length(xvals)) + ii;
This creates a 10x10 square matrix, not a 1x10 vector. plot then plots each column of that matrix against xvals. That causes a mixing up of colors which is probably not what you want.
It's interesting to analyze specifically what happens. Matlab plots each column of that matrix with a different color, using the default cycle of colors, which in Matlab R2014b onwards is
But all columns of that matrix are the same, so each covers (overwrites) the preceding one, and you only see the last color.
Now, the color cycle has 7 colors and the matrix has 10 columns. So in the first iteration the last plotted column (the one you see) has color mod(10,7)==3 (yellow). In the second iteration you cycle through 10 more colors starting from 3, that is, you get color mod(3+10,7)==6 (light blue). And so on. Thus the color you see in the figure depends on the loop index ii, but not in the way you expected.
legend creates its entries by picking the color (and line spec) of each plotted line, in the order in which they were plotted. There are 10*6==60 plotted lines, each corresponding to a column of a matrix. But since you only supply six strings to legend, it just picks the first six of those lines, and uses their colors to create the legend entries. Those colors follow the default order, as explained above.
None of those first six lines that make it into the legend are actually seen in the figure, because they are covered by other lines. But legend doesn't care about that. So you get six legend entries with the default color order, which of course doesn't match the lines you actually see in the graph.
Solution
From the above, the solution is clear: replaced the referred line by
yvals = zeros(1, length(xvals)) + ii;
to create yvals as a vector (not a matrix). That way you'll get the figure you expected:

Overlapping graphs in matlab

I am drawing several graphs in the same plot in matlab by using the command, hold on.
My problem is that I am drawing points with big markersize on top of some of the graphs, and I would like theses points to have some specific color. Only what happens is that some of them will take some color and some others take another color, my guess is that the colors of my points get mixed with the colors of the graphs on top of which i put them. Is there a mean to ask matlap to overwrite whatever is under my points so that they get the color I assign to them?
Example:
x= 0:1:10;
plot(x,x,'r',x,-x,'b','linewidth',2)
hold on
plot(5,-5,'.',10,10,'.','MarkerColor',[0,0.5,0],'Markersize',24)
hold on
plot(5,5,'.',10,-10,'.','MarkerFaceColor',[0,0.75,0],'MarkerSize', 24)
Imagine that the curves are much more complicated than theses simple lines so no way to start having fun cutting them each time I want to represent a point...
Now my problem is that I would like that the points 5,-5 and 10,10 to have the same color. Namely 0 0.5 0 dark green. but their color get mixed depending on which line they lie.
If I specify their color as '.g' I don't get this problem, but the problem is that I got too many points to get covered by the few number of colors that are labeled by letters (eg 'r' 'b' 'k' etc..).
Thankfully
Note that 'MarkerColor' doesn't exist, so I suppose it's a type and you meant 'MarkerFaceColor', just like in the other plot.
Then, hold on once is enough, you don't need to repeat it every time you want overlap another plot.
Finally, I suggest you simply use 'Color' instead of MarkerFaceColor. This should display your dots with the wanted color.
x= 0:1:10;
plot(x,x,'r',x,-x,'b','linewidth',2)
hold on
plot(5,-5,'.',10,10,'.','Color',[0,0.5,0],'Markersize',24)
plot(5,5,'.',10,-10,'.','Color',[0,0.75,0],'MarkerSize', 24)

I have a 2D plot in Matlab where I want to specify the RGB value for each point to vary to color

I am trying to plot a 2D line in Matlab with a color that varies based on an RGB code that I assign to each point. The code below works well for a given colormap ('col' values defining the color), but I am trying to keep tighter control over the color assignment, so that values always render the same color across multiple charts.
surface([x;x],[y;y],[z;z],[col;col],...
'facecol','no',...
'edgecol','interp',...
'linew',2);
The question is how do you decide which colour does each point have? defining the colormap is easy (shown below).
col=colormap(hot(128)) %% or other colour style like hsv or jet
if you have a variable (lets called it V) that for each point has a certain value and you want the colours to change based on that:
first define the values for the extremes in the colormap:
min_col=0; %%%can be the minimum of V
max_col=1; %%%can be the maximum of V
Then interpolate to your data
new_col=interp1(linspace(min_col,max_col,length(col)),col, V(:))

How to give different colors when I loop for plot in MATLAB?

I have some data say X with size (100,2). This X is composed of data for 10 categories (set of 10). Now I would like to look the pattern in the data for each category. For this I need to have different colors assigned to each category. I am trying to loop instead of doing 10 different plots. I tried the below.
hold on
for i=1:10:100
plot(X(i:i+9,1),X(i:i+9,2),'.')
end
hold off
This gave me a plot with same color. How can I assign different colors for different range?
The answers mentioning hold all are correct and useful for cycling through the colors specified by the ColorOrder axes property (even though just hold on is now equivalent to hold all). However, by default MATLAB only specifies a short list of colors (just 7 as of R2013b) to cycle through, and on the other hand it can be problematic to find a good set of colors for more data series. For 10 plots, you obviously cannot rely on the default ColorOrder, so a great way to define N visually distinct colors is with the "Generate Maximally Perceptually-Distinct Colors" (GMPDC) submission on the MATLAB Central File File Exchange. It is best described in the author's own words:
This function generates a set of colors which are distinguishable by reference to the "Lab" color space, which more closely matches human color perception than RGB. Given an initial large list of possible colors, it iteratively chooses the entry in the list that is farthest (in Lab space) from all previously-chosen entries.
For example, here are the colors generated when 25 are requested:
The GMPDC submission was chosen on MathWorks' official blog as Pick of the Week a few years ago in part because of the ability to request an arbitrary number of colors (in contrast to MATLAB's built in 7 default colors). They even made the excellent suggestion to set MATLAB's ColorOrder on startup to,
distinguishable_colors(20)
Of course, you can set the ColorOrder for a single axis or simply generate a list of colors to use in any way you like. For example, to generate 10 "maximally perceptually-distinct colors" and use them for 10 plots on the same axis (not using ColorOrder):
% Starting with X of size 100x2
X = reshape(X,10,10,2); % for clarity, column is category, row is observation
mpdc10 = distinguishable_colors(10) % 10x3 color list
hold on
for ii=1:10,
plot(X(:,ii,1),X(:,ii,2),'.','Color',mpdc10(ii,:));
end
Alternatively, using the ColorOrder axis property simplifies the process:
X = reshape(X,10,10,2); % for clarity, and to avoid loop
mpdc10 = distinguishable_colors(10) % 10x3 color map
ha = axes; hold(ha,'on')
set(ha,'ColorOrder',mpdc10)
plot(X(:,:,1),X(:,:,2),'-.') % loop NOT needed, 'Color' NOT needed
APPENDIX
To get the ColorOrder RGB array used for the current axis,
get(gca,'ColorOrder')
To get the default ColorOrder for new axes,
get(0,'DefaultAxesColorOrder')
Example of setting new global ColorOrder with 10 colors on MATLAB start, in startup.m:
set(0,'DefaultAxesColorOrder',distinguishable_colors(10))
The easiest solution is to replace hold on by hold all.
If you want more control you have to manually define your line specifications (more info here) and then pass them to plot:
linespec = {'b.', 'r-', 'g--o'}; % define your ten linespecs in a cell array
hold on
for i=1:10:100
plot(X(i:i+9,1),X(i:i+9,2),linespec{i})
end
hold off
hold on makes sure the new plot command adds to the plot instead of replacing it. However, each command works as if it were generating a fresh plot, including starting with the first line color (blue). If you want subsequent plots use different colors, use hold all instead. That way the standard 7 line colors are used in turn.
Since you have 10 lines to plot, you might want to specify the colors explicitly to make sure they are all different. For that, use the syntax
plot(..., 'Color', [r g b])

Matlab Plot with Customize color

does anyone know what's the command or way to plot with different colors besides the preset color, i.e., b,g,r,c,m,y,k? I would prefer to use my own customize colors instead of these....
Cheers
Use the Color property with a ColorSpec triple:
plot(x, y, 'Color', [0.5, 1.0, 0.0], 'LineStyle', '--')
The vector here contains RGB values between 0.0 and 1.0. The Lineseries and ColorSpec documentation entries have more information about this.
From the matlab docs:
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.
All together, this means you want the commands:
figure();
axis();
set(gca, 'colororder', <color matrix>, 'nextplot', 'replacechildren');
plot(x,y);
I had the same problem with the limited set of preset colours MATLAB made available. So, I created this page on my website, where you can choose from a large palette of non-standard colours, and the colour code is pasted directly to your clipboard:
http://www.shirt-ediss.me/matlab-octave-more-colours/