How can I create different colors in the plotted chart for a lot of data
Show older comments
Hello everyone;
I have huge data set, and i plotted for at least 70 different sets. What should I do to color all lines differently? What are your suggestions?
I thank everyone in advance
2 Comments
Turlough Hughes
on 18 Sep 2020
Edited: Turlough Hughes
on 18 Sep 2020
What code do you have so far? The plot function usually varies line colours for you if you haven't specified it.
Abidin Burak Nuzumlali
on 19 Sep 2020
Accepted Answer
More Answers (2)
Dana
on 18 Sep 2020
Are you trying to plot 70 different data series on the same plot? If so, Matlab's default color scheme won't give you 70 different colors. Instead, there are 7 colors that it cycles through, so you'll end up with 10 data series for each color.
You can create your own color order with as many colors as you want in it, but before you do, let me ask: are you sure you want to do that? 70 plots on one graph is...a lot.
If you're sure, then the easiest way is probably to pick a pre-set color map (see colormap), use it to generate 70 different colors, and then use colororder to apply it to your figure. So if X is a matrix with 70 columns, each containing one data series, something like:
N = size(X,2); % number of data series
plot(X) % plot your data
colororder(parula(N)) % pick N colors from the parula color map and use them
4 Comments
Abidin Burak Nuzumlali
on 19 Sep 2020
Stephen23
on 19 Sep 2020
You do not need to use the colororder function (introduced in R2019b), you can set the line ColorOrder directly in the axes properites, and works for all versions:
Adam Danz
on 20 Sep 2020
To add to Dana's answer,
1) Some colormaps only conatain a discrete set of colors with much less than 70 colors so carefully select a colormap with a continuous range of colors. https://www.mathworks.com/help/matlab/ref/colormap.html#buc3wsn-6
2) As Stephen mentioned, you can set the color values directly or you could use Dana's suggestion to use colororder. If you want to apply that directly you can do so within a loop, indexing from the RGB color matrix, or you can apply different colors to a vector of handles using a cell array of RGB values.
3) No matter what colormap you choose, with 70 colors you're going to have lots of colors that are very similar. You could add a legend that defines each color but with 70 lines, the legend will be large and disorganized. Instead, define a colorbar that defines each line.
Abidin Burak Nuzumlali
on 21 Sep 2020
Adam Danz
on 20 Sep 2020
Inspired by Turlough Hughes's slider used to chose a line object, here's a way to add info to each line's data tip so you can hover the mouse of any line to display the line info.
Here's another demo containing a series of gaussians and I've added the sigma (width) and amplitude (height) of each curve to the data tip. The data tip appears when you hover the mouse over the curve or if you click on the curve.
Modify the demo to add any information you want to the line.
% Plot 12 gaussians with different widths and heights
x = linspace(0,200,1000);
gaus = @(x,mu,sig,amp)amp.*exp(-(((x-mu).^2)./(2.*sig.^2)));
amp = linspace(10,12,8)';
sig = linspace(20,30,8)';
y = gaus(x,100,sig,amp);
h = plot(x,y');
% Add two rows to the data tips to show their sigma (width)
% and amp (height) values.
for i = 1:numel(h)
% Add a new row to DataTip showing the gaus parameters
h(i).DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Sigma',repmat({round(sig(i),2)},size(h(i).XData)));
h(i).DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Amp',repmat({round(amp(i),2)},size(h(i).XData)));
end
Result

5 Comments
Turlough Hughes
on 21 Sep 2020
Nice one.
Abidin Burak Nuzumlali
on 21 Sep 2020
Turlough Hughes
on 21 Sep 2020
Well to be fair if you find this answer more useful then you should probably accept this one as opposed to mine.
Adam Danz
on 21 Sep 2020
Take the credit for inspiring my idea.
All 3 answers here are viable solutions and may benefit future visitors.
Abidin Burak Nuzumlali
on 22 Sep 2020
Categories
Find more on Color and Styling 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!