Color points by z value bins in MATLAB scatter plot

  • Topic: MATLAB 
  • Thread starter quasarLie
  • Start date
  • Tags Tags
    Data Matlab
4 replies · 4K views · last reply
quasarLie
Messages
50
Reaction score
0
Hello,

I am writing a program in MATLAB that reads a file.dat, I also want to draw by bin of z and degraded the color for example for z between 0 and 1 put the color red, for z between 1 and 2 put blue. But I do not know how to do it

Here is my code
Matlab:
Data2=importdata('fichier.dat');

%redshift2=Data2.data(:,1);u=Data2.data(:,2);
r=Data2.data(:,7);
delta1=u-r;
u=Data2.data(:,2);
g=Data2.data(:,6);
delta2=g-u;

%For i=1:length(delta1) 
%delta1(i)=delta1(i)+20; 
%End 
hold on;
plot(delta2,delta1,'*b');
xlabel('u-r')
ylabel('g-u')
 
quasarLie said:
Hello,

I am writing a program in MATLAB that reads a file.dat, I also want to draw by bin of z and degraded the color for example for z between 0 and 1 put the color red, for z between 1 and 2 put blue. But I do not know how to do it

Here is my code
Matlab:
Data2=importdata('fichier.dat');

%redshift2=Data2.data(:,1);u=Data2.data(:,2);
r=Data2.data(:,7);
delta1=u-r;
u=Data2.data(:,2);
g=Data2.data(:,6);
delta2=g-u;

%For i=1:length(delta1)
%delta1(i)=delta1(i)+20;
%End
hold on;
plot(delta2,delta1,'*b');
xlabel('u-r')
ylabel('g-u')

That depends on what you want to do and what the data is. If you just want to make a scatterplot or line plot and change the colors, this page shows you how to change graphic object properties
https://www.mathworks.com/help/matlab/examples/changing-line-properties.html

if your question is how should you best display the data, then you may need to consider the method posed by jedishrfu
 
Code:
>> help discretize

and then plot using some color map of your choice?
 
The histogram object can bin the data AND plot it. But there are other ways to do this where the calculation is split between binning/plotting functions. If you really want to color the bars differently, then you might need to do with the second method.

- histc, histcounts, and discretize are all capable of binning data, but have different conventions for the bin edges (histcounts agrees with histogram, though). Pay attention to the edge conventions since it affects how you do the plotting.
- For plotting you can use histogram or bar. histogram can bin AND plot the data, or just plot precomputed bin counts. bar requires that you do the data binning beforehand.

Here's an example of plotting a histogram and setting the bars to different colors. Change the color for a particular bar by setting the FaceColor property to 'flat'. Then change the corresponding row in the CData matrix to the new RGB triplet.

Code:
x = 2*rand(100,1) - 1;
edges = -1:0.25:1;

% Bin and plot separately, converting
% the 1x9 vector of bin edges to a
% 1x8 vector of bin centers.
y = histcounts(x,edges);
centers = diff(edges)/2 + edges(1:end-1);
b = bar(centers,y,'FaceColor','flat'); % return bar object b
b.CData(1,:) = [1 0 0]; % first bar is red
b.CData(2,:) = [0 1 0]; % second bar is green
b.CData(3,:) = [1 1 0]; % third bar is yellow
b.CData(4,:) = [0 0 1]; % fourth bar is blue
b.CData(5,:) = [1 0 1]; % fifth bar is magenta
b.CData(6,:) = [0 1 1]; % sixth bar is cyan
b.CData(7,:) = [1 1 1]; % seventh bar is white
b.CData(8,:) = [0 0 0]; % eighth bar is black

Screenshot of MATLAB code beside a bar chart of eight bars with colors red, green, yellow, blue, magenta, cyan, white and black, centered on x from -1 to 1 on the horizontal axis.
 
Attachments
  • Screenshot of MATLAB code beside a bar chart of eight bars with colors red, green, yellow, blue, magenta, cyan, white and black, centered on x from -1 to 1 on the horizontal axis.
    2018-04-20_10-27-23.png
    24.2 KB · Views: 892
  • Like
Likes   Reactions: olivermsun