How can I create different colors in the plotted chart for a lot of data
78 次查看(过去 30 天)
显示 更早的评论
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 个评论
Turlough Hughes
2020-9-18
编辑:Turlough Hughes
2020-9-18
What code do you have so far? The plot function usually varies line colours for you if you haven't specified it.
采纳的回答
Turlough Hughes
2020-9-20
I'm coming at this from a slightly different angle. You can interactively highlight different lines by setting up a slider in the live editor as per the demo I've attached. With this approach you can see all the other data while highlighting one line of interest at a time (in this case I just increase the linewidth and bring it to the front of the graph, though one could specify other line properties).
Advantages: You do not have to read down a list of 70 or more legend entries. You avoid any colour similarity issue, and furthermore, you ensure the line you want to assess is in the foreground; with 70 or more lines it is quite likely that many cover a large portion of the line you’re currently looking at.
2 个评论
更多回答(2 个)
Dana
2020-9-18
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 个评论
Adam Danz
2020-9-20
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.
Adam Danz
2020-9-20
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 个评论
Adam Danz
2020-9-21
Take the credit for inspiring my idea.
All 3 answers here are viable solutions and may benefit future visitors.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!