Same color for different subplots

10 views (last 30 days)
Ale_798
Ale_798 on 11 Sep 2023
Commented: Dyuman Joshi on 19 Sep 2023

I want to plot some data in two subplots. To each datum are associated two parameters, which are the ones I'd like to plot, one for each subplot. I want the subplots to have the same color for the two parameters associated to the same datum, but different from the other ones, like in the picture. How can I do that?

  3 Comments
Ale_798
Ale_798 on 11 Sep 2023
Ok thanks, I understood what you mean. Then I need to define an array of 10 different colors: can you help me with that please?
Dyuman Joshi
Dyuman Joshi on 19 Sep 2023
"can you help me with that please?"
@Ale_798 Please check my answer below.

Sign in to comment.

Answers (1)

Dyuman Joshi
Dyuman Joshi on 11 Sep 2023
"Then I need to define an array of 10 different colors:"
Yes.
RGB colors are a 3 element vectors with values in the range [0, 1].
There are multiple ways to do that.
1 - Manually defining colors. In the method, you have the choice of choosing which colors you want to use.
color = [0 0 0; %black
0 0 1; %blue
0 1 0; %green
0 1 1; %blue+green=cyan
1 0 0; %red
1 0 1; %red+blue=magenta
1 1 0; %rea+green=yellow
0.5 0.5 0.5; %gray
0.5 0 0; %maroon
0 0.5 0; %lime
]
color = 10×3
0 0 0 0 0 1.0000 0 1.0000 0 0 1.0000 1.0000 1.0000 0 0 1.0000 0 1.0000 1.0000 1.0000 0 0.5000 0.5000 0.5000 0.5000 0 0 0 0.5000 0
2 - Using rand and randi. Note - This method will generate colors randomly.
n = 10;
color = rand(n,3)
color = 10×3
0.0286 0.5651 0.7392 0.9416 0.7187 0.7692 0.3686 0.2638 0.6682 0.4783 0.1181 0.7678 0.0093 0.4585 0.2831 0.3646 0.6859 0.4248 0.9145 0.4302 0.2375 0.6649 0.6619 0.6372 0.1268 0.7734 0.8096 0.2958 0.6138 0.6917
color = (randi(256,n,3)-1)/255
color = 10×3
0.1882 0.6941 0.8627 0.7451 0.5529 0.5882 0.0941 0.9686 0.4392 0.9176 0.2314 0.9137 0.9294 0.3176 0.5216 0.0627 0.2314 0.5804 0.9922 0.6510 0.9059 0.6471 0.3529 0.1294 0.2667 0.1333 0.9098 0.0667 0.8510 0.0275
3 - Using in-built colormaps in MATLAB
color = hsv(n)
color = 10×3
1.0000 0 0 1.0000 0.6000 0 0.8000 1.0000 0 0.2000 1.0000 0 0 1.0000 0.4000 0 1.0000 1.0000 0 0.4000 1.0000 0.2000 0 1.0000 0.8000 0 1.0000 1.0000 0 0.6000

Community Treasure Hunt

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

Start Hunting!