# How to use MATLAB RGB triplets and hexadecimal color codes for a custom plots?

360 views (last 30 days)
Souarv De on 29 Mar 2022
Commented: Voss on 29 Mar 2022
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?

Les Beckham on 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 ##### 2 CommentsShowHide 1 older comment
Les Beckham on 29 Mar 2022
You are very welcome.

Voss on 29 Mar 2022
Edited: Voss on 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]) Voss on 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.

R2021a

### Community Treasure Hunt

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

Start Hunting!
{"ft_lang_label":"__label__en","ft_lang_prob":0.7279075,"math_prob":0.98026276,"size":4774,"snap":"2022-27-2022-33","text_gpt3_token_len":1509,"char_repetition_ratio":0.14318658,"word_repetition_ratio":0.074858755,"special_character_ratio":0.382279,"punctuation_ratio":0.26576218,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9712478,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,2,null,2,null,2,null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-05T16:48:08Z\",\"WARC-Record-ID\":\"<urn:uuid:18dc1d9d-a753-4922-a7a6-c8ab09bda62e>\",\"Content-Length\":\"188451\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:117eed52-e71f-43ae-a84c-6d5de8ffbc00>\",\"WARC-Concurrent-To\":\"<urn:uuid:e4f5cf38-e110-4cda-a1cd-354970f696ae>\",\"WARC-IP-Address\":\"23.1.9.244\",\"WARC-Target-URI\":\"https://se.mathworks.com/matlabcentral/answers/1683544-how-to-use-matlab-rgb-triplets-and-hexadecimal-color-codes-for-a-custom-plots?s_tid=prof_contriblnk\",\"WARC-Payload-Digest\":\"sha1:65FPJUIXOGDPUQOM2PUUHA7WAD3YU4KQ\",\"WARC-Block-Digest\":\"sha1:O7UK5I467YDA3U2XZBWQZSBRZC3MU6B3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104585887.84_warc_CC-MAIN-20220705144321-20220705174321-00720.warc.gz\"}"}