How to use MATLAB RGB triplets and hexadecimal color codes for a custom plots?
516 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I need to plot four variables in a single plot with 4 different RGB triplets colors whose RGB Triplet are as follows: [0 0.4470 0.7410], [0.8500 0.3250 0.0980], [0.9290 0.6940 0.1250], [0.4940 0.1840 0.5560] and the corresponding hexadecimal color codes of these colors are '#0072BD', '#D95319', '#EDB120', '#7E2F8E'.
I am using the following command line but it gives some error:
plot(t,a,'MarkerFace',[0 0.4470 0.7410],t,b,'MarkerFace',[0.8500 0.3250 0.0980],t,c,'MarkerFace',[0.9290 0.6940 0.1250],t,d,'MarkerFace',[0.4940 0.1840 0.5560],'LineWidth',2)
It is also not clear in any MATLAB documentation stating that how to use such RGB triplet color except color which have names associated with it? Can anyone know how to plot those variable with those above color codes?
0 Commenti
Risposta accettata
Les Beckham
il 29 Mar 2022
I don't think you can specify name/value pairs in the plot command with multiple x/y values.
This isn't very elegant but it does what you want by first doing the plot command and then changing the colors. Note that the RGB color specs that you provided happen to be the first 4 default colors that Matlab will use in a plot so I've changed them in this example so that we could see that the colors actually change.
t = 0:.1:1; % Random sample data
a = rand(11,1);
b = rand(11,1);
c = rand(11,1);
d = rand(11,1);
plot(t,a, t,b, t,c, t,d); % plot with default colors
figure
h = plot(t,a, t,b, t,c, t,d); % plot again
colors = {[1 0.4470 0.7410], [0.1500 0.3250 0.9980], [0.0290 0.6940 0.6250], [0.2940 0.5840 0.2560]};
[h(1).Color h(2).Color h(3).Color h(4).Color] = colors{:}; % change the colors
3 Commenti
Più risposte (1)
Voss
il 29 Mar 2022
Modificato: Voss
il 29 Mar 2022
The problem is not the colors; the problem is trying to specify line properties for multiple lines in one call to plot().
Line properties specified in (Name,Value) pairs, e.g., 'MarkerFace',[0 0.4470 0.7410], as you have in this case, must be the last arguments to the plot() function, and those properties apply to all lines created by that call to plot().
You can specify different line properties for different lines in a single plot() call using LineSpec arguments, e.g., '-' or '.-k', and those do go in the order that you are trying, i.e., plot(x1, y1, LineSpec1, x2, y2, LineSpec2, ...) but the properties you can set that way are more limited than with (Name,Value) pairs of arguments.
It's not exactly clear what your intended result is here because you are specifying the 'MarkerFaceColor' but no 'Marker' (so that 'MarkerFaceColor' has no effect), but here are some different ways you can use plot to try to do what you want (see this answer if you want to use hexadecimal color codes):
t = 0:5;
a = t;
b = t+1;
c = t+2;
d = t+3;
% plot(t,a,'MarkerFace',[0 0.4470 0.7410], ...
% t,b,'MarkerFace',[0.8500 0.3250 0.0980], ...
% t,c,'MarkerFace',[0.9290 0.6940 0.1250], ...
% t,d,'MarkerFace',[0.4940 0.1840 0.5560], ...
% 'LineWidth',2);
% plot all lines at once by putting the y-values in a matrix (they all have
% the same x-values, which is t):
figure()
plot(t,[a(:) b(:) c(:) d(:)],'LineWidth',2);
% plot one line per plot() call, specifying any set of properties
% you want, for each one:
figure()
hold on % hold on or equivalent is necessary here
plot(t,a,'MarkerFace',[0 0.4470 0.7410],'LineWidth',2,'Marker','s');
plot(t,b,'MarkerFace',[0.8500 0.3250 0.0980],'LineWidth',2,'Marker','o');
plot(t,c,'MarkerFace',[0.9290 0.6940 0.1250],'LineWidth',2,'Marker','d','LineStyle','--');
plot(t,d,'MarkerFace',[0.4940 0.1840 0.5560],'LineWidth',2);
% create all lines in one call to plot() using (x1,y1,x2,y2,...), and
% setting some properties afterward:
figure();
% get the handles to the lines returned from plot():
h = plot(t,a,t,b,t,c,t,d,'LineWidth',2);
% set some of their properties individually:
set(h(1),'Color',[0 0.6 0])
set(h(2),'Color',[0.8500 0.3250 0.0980],'Marker','v','MarkerFaceColor','g')
set(h(3),'MarkerFace',[0 0.6940 0.1250],'Marker','h','MarkerEdgeColor',[0.2 0.2 0.2])
% create all lines in one call to plot() using (x1,y1,LineSpec1,x2,y2,LineSpec2,...),
% and setting some properties afterward:
figure();
% get the handles to the lines returned from plot():
h = plot(t,a,'--',t,b,':rx',t,c,t,d,'LineWidth',2);
% set some of their properties individually:
set(h(3),'MarkerFace',[0 0.6940 0.1250],'Marker','h','MarkerEdgeColor',[0.2 0.2 0.2])
4 Commenti
Voss
il 29 Mar 2022
Yes, there are so many ways to use plot(), it can be tricky when you run into a way that is not allowed.
Vedere anche
Categorie
Scopri di più su Line Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!