I want to draw a scatterplot but there's a problem about range of color

3 views (last 30 days)
I load 15 files by this same style code,
location1="C:\Users\Lee\Desktop\data\m000001_1_1"
data1=readtable(location1);
p=data1{:,3};
f1=data1{:,1};
[minf1,minfpos1]=min(p);
%% 2
location2="C:\Users\Lee\Desktop\data\m000001_1_2"
data2=readtable(location2);
p2=data2{:,3};
f2=data2{:,1};
[minf2,minfpos2]=min(p2);
...
%% 15
location15="C:\Users\Lee\Desktop\data\m000001_1_15"
data15=readtable(location15);
p15=data15{:,3};
f15=data15{:,1};
[minf15,minfpos15]=min(p15);
and 6 files have 101 rows and 1 file contains 201 rows and rest of files have 81 rows.
So 6 f groups(f1, f2, f4,f5, f10, f11) have 101 elements and f13 has 201 elements and rest of f groups have 81 elements for each f groups.
After that, I want to do scatterplot all the data elements of 15 files in just one scatterplot graph
with many colors so I made my code like this:
%% Plot
sz=25;
i=1:length(f);
j=1:length(f3);
k=1:length(f13);
c1=linspace(2000,4000,101);
c2=linspace(2000,4000,81);
c3=linspace(2000,4000,201)
while 1
scatter(b,f,sz,c1,"filled")
hold on
scatter(b2,f2,sz,c1,"filled")
hold on
scatter(b3,f3,sz,c2,"filled")%j
hold on
scatter(b4,f4,sz,c1,"filled")
hold on
scatter(b5,f5,sz,c1,"filled")
hold on
scatter(b6,f6,sz,c2,"filled")%j
hold on
scatter(b7,f7,sz,c2,"filled")%j
hold on
scatter(b8,f8,sz,c2,"filled")%j
hold on
scatter(b9,f9,sz,c2,"filled")%j
hold on
scatter(b10,f10,sz,c1,"filled")
hold on
scatter(b11,f11,sz,c1,"filled")
hold on
scatter(b12,f12,sz,c2,"filled")%j
hold on
scatter(b13,f13,sz,c3,"filled")%k
hold on
scatter(b14,f14,sz,c2,"filled")%j
hold on
scatter(b15,f15,sz,c2,"filled")%j
if i>101
break
end
i=i+1;
if j>81
break
end
j=j+1;
if k>201
break
end
k=k+1;
colorbar
title('FMR')
xlabel('magnetic field,B (G)')
ylabel('frequency,f (MHz)')
end
This is just one of example of color range settings.
b~b15 is just a number like b=123.1023, so I think this doesn't matter.
I tried several ways to set a color range approriately but nothing worked.
I want to draw scatterplot with colorful spots like this but I don't know how to set appropriate range of "c"
How may I change this scatterplot code to set a color range for each data elements appropriately?

Accepted Answer

Chris
Chris on 14 Sep 2022
Edited: Chris on 14 Sep 2022
To specify a custom range, you can use a pre-defined RGB colormap (replace 6 with 101 or whatever is appropriate)
c1 = parula(6)
c = 6×3
1.0000 0 0 1.0000 0.2000 0 1.0000 0.4000 0 1.0000 0.6000 0 1.0000 0.8000 0 1.0000 1.0000 0
If you only want to use some of that range, you can take a slice out of c, as long as the number of rows matches the number of plotted points (and c is larger than the slice).
scatter(b,f,sz,c1(20:80,:),"filled")
You could make up your own matrix of RGB values from 0 to 1 as well.
There are a few other options, but I think this is a good method for what you are doing.

More Answers (1)

William Rose
William Rose on 14 Sep 2022
@SungJin Jang, I am not sure what color scheme you are trying to accomplish.
Your present code assigns color to each point with a linear ramp function. It appears that the x-axis values are also a linear ramp function. Therefore the color and the x-axis position seem to be providing the viewer with the same information.
Do you want b1,f1 alll in the one color, and b2,f2 all in one different color, and so on? So that you have 15 colors when the plot includes 15 data sets? If that is your wish, then you can let Matlab pick the colors. It will pick a different color for each data set. If you do not like the colors matlab picks by default, you can choose your own color for each data set.
%generate simulated data
b1=linspace(0,9,81); f1=1.5*cos(b1*2*pi/6.5)+.5-.3;
b2=linspace(.5,9.5,101); f2=1.5*cos(b2*2*pi/6.5+pi/6)-.1;
b3=linspace(1,10,201); f3=1.5*cos(b3*2*pi/6.5-pi/6);
b4=linspace(1.5,10.5,81); f4=1.5*cos(b4*2*pi/6.5+pi/4)+.1;
b5=linspace(2,11,101); f5=1.5*cos(b5*2*pi/6.5-pi/4)+.3;
%plot data
sz=25;
scatter(b1,f1,sz,'filled'); hold on;
scatter(b2,f2,sz,'filled'); scatter(b3,f3,sz,'filled');
scatter(b4,f4,sz,'filled'); scatter(b5,f5,sz,'filled');
title('FMR'), xlabel('Magnetic Field (G)'), ylabel('Frequency (MHz)')
legend('b1','b2','b3','b4','b5')
Example above allows matlab to pick the color.
figure;
%plot data
scatter(b1,f1,sz,'r','filled'); hold on;
scatter(b2,f2,sz,'g','filled'); scatter(b3,f3,sz,'b','filled');
scatter(b4,f4,sz,'c','filled'); scatter(b5,f5,sz,'m','filled');
title('FMR'), xlabel('Magnetic Field (G)'), ylabel('Frequency (MHz)')
legend('b1','b2','b3','b4','b5')
Example above: the color is specidfied in the code.
If you want 15 colors that are evenly spaced in hue, do
ncol=15; %number of colors
c=hsv2rgb([(0:1/ncol:(1-1/ncol))',ones(ncol,1),ones(ncol,1)]); %define colors
%make more simulated data sets
b6=b1+.3; b7=b2+.3; b8=b3+.3; b9=b4+.3; b10=b5+.3;
b11=b1+.6; b12=b2+.6; b13=b3+.6; b14=b4+.6; b15=b5+.6;
f6=f1; f7=f2; f8=f3; f9=f4; f10=f5; f11=f1; f12=f2; f13=f3; f14=f4; f15=f5;
%plot data
figure
scatter(b1,f1,sz,c(1,:),'filled'); hold on;
scatter(b2,f2,sz,c(2,:),'filled'); scatter(b3,f3,sz,c(3,:),'filled');
scatter(b4,f4,sz,c(4,:),'filled'); scatter(b5,f5,sz,c(5,:),'filled'); scatter(b6,f6,sz,c(6,:),'filled')
scatter(b7,f7,sz,c(7,:),'filled'); scatter(b8,f8,sz,c(8,:),'filled'); scatter(b9,f9,sz,c(9,:),'filled')
scatter(b10,f10,sz,c(10,:),'filled'); scatter(b11,f11,sz,c(11,:),'filled'); scatter(b12,f12,sz,c(12,:),'filled')
scatter(b13,f13,sz,c(13,:),'filled'); scatter(b14,f14,sz,c(14,:),'filled'); scatter(b15,f15,sz,c(15,:),'filled')
title('FMR'), xlabel('Magnetic Field (G)'), ylabel('Frequency (MHz)')
for i=1:15, legstr{i}=num2str(i); end %legends for plot
legend(legstr)
Try that. Good luck.
  1 Comment
SungJin Jang
SungJin Jang on 15 Sep 2022
Sorry, my explanation was bad.
What I meant was that I want to assign a unique color for each elements from such datas.
For example, data1 have 101 elements. Then I match a blue for the first elements and yellow for the second one, red for the third one, and so on.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!