Unique color for every plot line

77 Ansichten (letzte 30 Tage)
Jesse
Jesse am 8 Nov. 2018
Kommentiert: Jesse am 9 Nov. 2018
To identify every line in a legend i want it to have a unique color.
as you can see, is already tried some. I'm still learning to use Matlab, so excuse me for my bad coding...
clear vars
close all
load 'Y:\Geovib\08\matlab.mat';
%afstand = '10m'; node='n201'; rij=2;
%afstand = '10.8m'; node='n202'; rij=3
%fstand = '11.6m'; node='n203'; rij=4;
%afstand = '12.7m'; node='n204'; rij=5;
%afstand = '13.3m'; node='n205'; rij=6;
%afstand = '14.3m'; node='n206'; rij=7
%afstand = '15.5m'; node='n207'; rij=8
%afstand = '16.8m'; node='n208'; rij=9
%afstand = '18.5m'; node='n209'; rij=10
%afstand = '20m'; node='n210'; rij=11
%afstand = '22m'; node='n211'; rij=12
%afstand = '24m'; node='n212'; rij=13
%afstand = '26.5m'; node='n213'; rij=14
%afstand = '29m'; node='n214'; rij=15
%afstand = '32m'; node='n215'; rij=16
afstand = 'alle'; node='alle';
%afstand = 'test';
%%%%%%%%%%%%%%%
matlab.Properties.VariableNames{1} = 'time';
matlab = removevars(matlab, {'VarName29','VarName27','VarName25','VarName23','VarName21','VarName19','VarName17','VarName15','VarName13','VarName11','VarName9','VarName7','VarName5','VarName3'});
matlab.Properties.VariableNames{2} = 'n201';matlab.Properties.VariableNames{3} = 'n202';matlab.Properties.VariableNames{4} = 'n203';matlab.Properties.VariableNames{5} = 'n204';
matlab.Properties.VariableNames{6} = 'n205';matlab.Properties.VariableNames{7} = 'n206';matlab.Properties.VariableNames{8} = 'n207';matlab.Properties.VariableNames{9} = 'n208';matlab.Properties.VariableNames{10} = 'n209';
matlab.Properties.VariableNames{11} = 'n210';matlab.Properties.VariableNames{13} = 'n212';matlab.Properties.VariableNames{12} = 'n211';
matlab.Properties.VariableNames{14} = 'n213';matlab.Properties.VariableNames{15} = 'n214';matlab.Properties.VariableNames{16} = 'n215';matlab.Properties.VariableNames{16} = 'n215';
matlab.time(2) = 0;
matlab(1,:) = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%vars={'n201','n202','n203','n204','n205','n206','n207','n208','n209','n210','n211','n212','n213','n214','n215'};
vars={'10.0m','10.8m','11.6m','12.7m','13.3m','14.3m','15.5m','16.8m','18.5m','20.0m','22.0m','24.0m','26.5m','29.0m','32.0m'};
if isequal(afstand,'alle')
data_time = table2array(matlab(3:end,2:end));
else
data_time = table2array(matlab(3:end,rij)); %%origineel
vars=node;
end
time = matlab.time(3:end);
data_fft = fft(data_time);
Y = 2*abs(data_fft(2:end/2+1,:));
freqs = 1:1000/2;
figure
subplot(211)
semilogx(freqs,Y)
if isequal(afstand,'alle')
else
title({['Afstand ',afstand, ' - node ',node]});
end
xlabel('frequentie [Hz]')
ylabel('trillingssnelheid(rms) [mm/s]')
colormap(hsv)
legend(vars)
% for i = 1:length(vars)
% legend(i)
% return
%
% end
grid on;
hold on;
axis tight;
xlim([0 100])
ylim([0 0.000040])
%%%Subplot in tijddomein
subplot(212)
%plot(time,data_time(:,1:1:end),'color',rand(1,3)) % je kan ook een aantal lijnen plotten bijv. [1:5:7]
plot(time,data_time)
legend(vars)
colormap(prism)
%legend(unique(vars))
grid on;
axis tight;
hold on;
%ylim([-0.00000043 0.00000055])
clear node rij time Y afstand data_fft matlab2

Akzeptierte Antwort

jonas
jonas am 8 Nov. 2018
Bearbeitet: jonas am 8 Nov. 2018
Here's an example that will give you truly unique colors from a colormap of your choice.
% Create 10 data series
XY = rand(100,10)
% Build colormap and shuffle
cmap = colormap(jet(size(XY,2)));
cmap = cmap(randperm(length(cmap)),:)
%Set colororder and plot
ax = axes('colororder',cmap);hold on
plot(XY)
I shuffled the colors (randperm) to make them random in the sense that the lines swap colors every time you plot. If the color has some significance, then you may want to remove that line. For example, you could design your plot so that the intensity of the line color represents the "quantity" of each line (10.0m, 10.8m, etc..). However, in that case it may be better to just go for the 3d plot instead :)
  7 Kommentare
jonas
jonas am 8 Nov. 2018
Bearbeitet: jonas am 8 Nov. 2018
Fixed code in the attachments. Note that I have changed to the default colormap (parula) and commented out the color randomization, so now you get a nice color gradient. I'd suggest you only display a single legend, as they are identical.
It does seem like you actually have three variables, so you could plot this using scatter and assign colors automatically from the third variable (units in [m]). If you did that, then you could skip the legend and just add a colorbar instead. Unfortunately I do not think MATLAB has a built in lineplot where the color is connected to a third variable (please someone correct me if I'm wrong) but there are various FEX functions (like this )
Jesse
Jesse am 9 Nov. 2018
Thank you very much! That's the answer i was looking for. Now i can apply this to all my plots. Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by