fitting a graph and coloring each fit

i wrote the following code % Color settings
r_220_w = [10.78, 20.33, 24, 27.7, 35.86, 32.5, 33.2, 30.75, 36.83, 33.44, 32.8, 45.66, 33.04, 39, 50.45, 34.78, 4.96];
r_220_aw = [0.01, 0.02, 0.02, 0.03, 0.02, 0.02, 0.02, 0.01, 0.02, 0.02, 0.02, 0.02, 0.02, 0.1, 0.05, 0.03, 0.03];
r_220_v = [1.02, 1.42, 1.82, 2.62, 5.92, 7, 8.24, 4.48, 4.64, 8.44, 7.5, 1.2, 8.01, 2.91, 0.87, 7.72, 0.973];
r_220_av = [0.01, 0.01, 0.02, 0.01, 0.01, 0.05, 0.01, 0.01, 0.05, 0.05, 0.1, 0.05, 0.05, 0.01, 0.01, 0.1, 0.001];
r_10_w = [33.05, 33.14, 33.1, 33.3, 31.1, 32.7, 35.9, 37.2, 22.9, 20.3, 33.7, 40.4, 27.07, 51.1, 34.45, 10.7, 4.97];
r_10_aw = [0.1, 0.05, 0.1, 0.1, 0.1, 0.1, 0.1, 0.3, 0.2, 0.1, 0.1, 0.1, 0.05, 0.1, 0.01, 0.1, 0.02];
r_10_v = [12.9, 13.5, 13.2, 13.8, 5.5, 10.8, 6.9, 4.57, 1.76, 1.52, 14.2, 2.4, 2.56, 0.77, 11.8, 1.02, 0.973];
r_10_av = [0.2, 0.2, 0.1, 0.1, 0.3, 0.1, 0.2, 0.02, 0.01, 0.01, 0.1, 0.1, 0.1, 0.01, 0.1, 0.1, 0.01];
r_2000_w = [52.67, 46.18, 39.33, 35.12, 34.12, 33.55, 10.91, 15.81, 20.77, 25.39, 29.21, 31.21, 42.96, 37.35, 4.98];
r_2000_v = [0.608, 0.888, 1.44, 1.8, 1.84, 1.88, 1.02, 1.13, 1.31, 1.56, 1.8, 1.88, 1.13, 1.62, 0.973];
r_2000_aw = [0.01, 0.02, 0.03, 0.01, 0.01, 0.03, 0.01, 0.01, 0.03, 0.02, 0.01, 0.02, 0.03, 0.02, 0.02];
r_2000_av = [0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.02, 0.01, 0.01, 0.01, 0.02, 0.01, 0.02, 0.01, 0.05];
r_1000_w = [34.71, 33.4, 32.5, 33, 35.56, 47.05, 37.6, 30.01, 10.53, 15.84, 20.75, 25, 31.97, 40.34, 44.03, 52.78, 4.96];
r_1000_v = [3.2, 3.35, 3.25, 3.3, 3.05, 0.976, 2.5, 2.74, 1.02, 1.18, 1.42, 1.82, 3.16, 1.86, 1.28, 0.664, 0.973];
r_1000_aw = [0.1, 0.02, 0, 0.1, 0.03, 0.02, 0.05, 0.03, 0.02, 0.01, 0.05, 0.03, 0.03, 0.02, 0.02, 0.02, 0.03];
r_1000_av = [0.01, 0.1, 0.01, 0.01, 0.03, 0.001, 0.01, 0.02, 0.01, 0.01, 0.02, 0.01, 0.01, 0.02, 0.01, 0.1, 0.001];
data = {
r_1000_w, r_1000_v, r_1000_av, r_1000_av, r_1000_aw, r_1000_aw;
r_2000_w, r_2000_v, r_2000_av, r_2000_av, r_2000_aw, r_2000_aw;
r_220_w, r_220_v, r_220_av, r_220_av, r_220_aw, r_220_aw;
r_10_w, r_10_v, r_10_av, r_10_av, r_10_aw, r_10_aw
};
color = [1 0.38 0.53]; % Specify your desired color
lineWidth = 1.5; % Specify the line width
% % Loop through each dataset and plot error bars
for i = 1:size(data, 1)
errorbar(data{i, 1}, data{i, 2}, data{i, 3}, data{i, 4}, data{i, 5}, data{i, 6}, ...
'.', 'Color', color, 'LineWidth', lineWidth);
hold on;
end
it is really hard to see since the same color is being used for all of them i thought it would switch in the range given with each graph , and also if i want to do a fit using cftool and then import it and add it to a sepreat graph without the error bars so that each fit has a color how can i possibly do that

3 Comments

You could simply remove the color setting and MATLAB would automatically choose different colours for each of the plots:
errorbar(data{i, 1}, data{i, 2}, data{i, 3}, data{i, 4}, data{i, 5}, data{i, 6}, ...
'.', 'LineWidth', lineWidth);
thank you but i am intersted in keeping the speciified range and ploting the same graph since the colors that i get from there seem more well toned , i tried before without the region yet it was messy with the region it is hard to see whats happening
but certainly a helpful comment it is just that i am also searching for either a possibility to choose the color for a fit in cftool such that each fit has a color or the color for each graph in error bar without manually entring colors for each additional input

Sign in to comment.

 Accepted Answer

dpb
dpb on 22 Jun 2024
Moved: dpb on 22 Jun 2024
It's hard to understand why you would think using a single color triplet would do anything other than use the one color for all? As @Ganesh points out, if you don't set a color at all, MATLAB will cycle through the default color map sequentially. Agreed, it's a pretty extreme set of colors one to the next that way but you can change and define your own colormap. Otherwise, you would need to explicitly define the color triplet for each plot in an array and iterate the color with the data in the loop.
r_220_w = [10.78, 20.33, 24, 27.7, 35.86, 32.5, 33.2, 30.75, 36.83, 33.44, 32.8, 45.66, 33.04, 39, 50.45, 34.78, 4.96];
r_220_aw = [0.01, 0.02, 0.02, 0.03, 0.02, 0.02, 0.02, 0.01, 0.02, 0.02, 0.02, 0.02, 0.02, 0.1, 0.05, 0.03, 0.03];
r_220_v = [1.02, 1.42, 1.82, 2.62, 5.92, 7, 8.24, 4.48, 4.64, 8.44, 7.5, 1.2, 8.01, 2.91, 0.87, 7.72, 0.973];
r_220_av = [0.01, 0.01, 0.02, 0.01, 0.01, 0.05, 0.01, 0.01, 0.05, 0.05, 0.1, 0.05, 0.05, 0.01, 0.01, 0.1, 0.001];
r_10_w = [33.05, 33.14, 33.1, 33.3, 31.1, 32.7, 35.9, 37.2, 22.9, 20.3, 33.7, 40.4, 27.07, 51.1, 34.45, 10.7, 4.97];
r_10_aw = [0.1, 0.05, 0.1, 0.1, 0.1, 0.1, 0.1, 0.3, 0.2, 0.1, 0.1, 0.1, 0.05, 0.1, 0.01, 0.1, 0.02];
r_10_v = [12.9, 13.5, 13.2, 13.8, 5.5, 10.8, 6.9, 4.57, 1.76, 1.52, 14.2, 2.4, 2.56, 0.77, 11.8, 1.02, 0.973];
r_10_av = [0.2, 0.2, 0.1, 0.1, 0.3, 0.1, 0.2, 0.02, 0.01, 0.01, 0.1, 0.1, 0.1, 0.01, 0.1, 0.1, 0.01];
r_2000_w = [52.67, 46.18, 39.33, 35.12, 34.12, 33.55, 10.91, 15.81, 20.77, 25.39, 29.21, 31.21, 42.96, 37.35, 4.98];
r_2000_v = [0.608, 0.888, 1.44, 1.8, 1.84, 1.88, 1.02, 1.13, 1.31, 1.56, 1.8, 1.88, 1.13, 1.62, 0.973];
r_2000_aw = [0.01, 0.02, 0.03, 0.01, 0.01, 0.03, 0.01, 0.01, 0.03, 0.02, 0.01, 0.02, 0.03, 0.02, 0.02];
r_2000_av = [0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.02, 0.01, 0.01, 0.01, 0.02, 0.01, 0.02, 0.01, 0.05];
r_1000_w = [34.71, 33.4, 32.5, 33, 35.56, 47.05, 37.6, 30.01, 10.53, 15.84, 20.75, 25, 31.97, 40.34, 44.03, 52.78, 4.96];
r_1000_v = [3.2, 3.35, 3.25, 3.3, 3.05, 0.976, 2.5, 2.74, 1.02, 1.18, 1.42, 1.82, 3.16, 1.86, 1.28, 0.664, 0.973];
r_1000_aw = [0.1, 0.02, 0, 0.1, 0.03, 0.02, 0.05, 0.03, 0.02, 0.01, 0.05, 0.03, 0.03, 0.02, 0.02, 0.02, 0.03];
r_1000_av = [0.01, 0.1, 0.01, 0.01, 0.03, 0.001, 0.01, 0.02, 0.01, 0.01, 0.02, 0.01, 0.01, 0.02, 0.01, 0.1, 0.001];
data = {
r_1000_w, r_1000_v, r_1000_av, r_1000_av, r_1000_aw, r_1000_aw;
r_2000_w, r_2000_v, r_2000_av, r_2000_av, r_2000_aw, r_2000_aw;
r_220_w, r_220_v, r_220_av, r_220_av, r_220_aw, r_220_aw;
r_10_w, r_10_v, r_10_av, r_10_av, r_10_aw, r_10_aw
};
color = [1 0.38 0.53]; % Specify your desired color
lineWidth = 1.5; % Specify the line width
% Loop through each dataset and plot error bars
hold on;
for i = 1:size(data, 1)
errorbar(data{i, 1}, data{i, 2}, data{i, 3}, data{i, 4}, data{i, 5}, data{i, 6}, ...
'.', 'LineWidth', lineWidth);
end
colormap(autumn)

8 Comments

It's hard to understand why you would think using a single color triplet would do anything other than use the one color for allIt's hard to understand why you would think using a single color triplet would do anything other than use the one color for all
the reason for my assumption is that when i use this color set and do hold on hold off on a normal plot it does change colors for each figure inside , so i found it rather unexpeted in this case ,
i did get the exact same graph you showed following the advice of @Ganesh (thanks again ) , yet it just seemed a bit too extreme so i wanted to see what other means i can possibliy use , i will go check color maps link now
"the reason for my assumption is that when i use this color set and do hold on hold off on a normal plot it does change colors for each figure inside , so i found it rather unexpeted in this case ,"
You must be comparing different pieces of code, then. There is no way you will get automatic color cycling if you provide the 'Color' optional argument with a fixed/constant value.
Post the code segment you believe does that and there's bound to be some action that will be causing the color change that you're not recognizing. Setting hold off will reset the color used to the first in the ColorOrder list but will also cause the existing lines on the axis to be deleted with the next call to a plotting routine. Your code above has only hold on which wil retain the current color order and, barring an explicit use of 'Color', continue cycling the default colors. But, there would be no point in being able to specify a given color if, given a constant rgb triplet as you've done, the plotting routines went ahead and modified the used color anyway...then you couldn't set an aribtrary color at all.
"... following the advice of @Ganesh (thanks again ) , yet it just seemed a bit too extreme "
I again agree that the MATLAB default colormaps are garish at best and even the limited number of provided alternatives are, for the most part, pretty ugly. One place where MS and Excel default color ranges are far more pleasing to the eye than MATLAB's although the pain in modifying anything in Excel is beyond excruciating...
dareen
dareen on 22 Jun 2024
Edited: dareen on 22 Jun 2024
you are right in those the plot was using the custom colors blue and yellow i seem to have assumed it is generated by the color i gave previously since i thought it was actually a range of colors not the color in rbg ( learning somehting new everyday )i succeded at making a color map just trying to find a set that looks good for now , thank you a lot ,this was helpful
The color argument can be specified in one of several ways; the rgb triplet is just one (and it can be either 0-1 or 0-255 range as another permutation). Only a deep perusal of the documentation will uncover just how one can specify the color and how colors actually work (and the latter gets pretty involved quite quickly).
I have only made cursory attempts at trying to build more pleasing custom colormaps; it doesn't appear to be a trivial task. One would think there would be a "veritable plethora" of refinements available, but while I've not done a search recently (as in for several years since I gave up the consulting gig years ago and lost the paying incentive to produce more client-pleasing graphics :J), when I last did the results were pretty minimal.
dareen
dareen on 22 Jun 2024
Edited: dareen on 22 Jun 2024
@dpb pretty interesting , it is intriguing to see how a task that seems easy (with my level i would not say trivial though ) ,does need this much time investment knowledge and practice to apply it , i will try to see the documentation and investigate this ,but seeing how active this community is , i beilive if it was to be done as a group project it would reflect some worth while results
dpb
dpb on 22 Jun 2024
Edited: dpb on 23 Jun 2024
I also agree that while the top level tools and default values do allow for rapid deployment and initial results, the overhead in customizing those out-of-the-box graphics to be anything more than that for publication or even presentation to a valuable client is a great time waster and a very nonproductive use of manhours towards the end goal of either the research project or a customer deliverable.
It is probably worth a search on the File Exchange to see if anybody has posted any custom colormaps...

Sign in to comment.

More Answers (0)

Asked:

on 22 Jun 2024

Edited:

dpb
on 23 Jun 2024

Community Treasure Hunt

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

Start Hunting!