MATLAB How to Plot Data into Bins in MATLAB

  • Thread starter Thread starter quasarLie
  • Start date Start date
  • Tags Tags
    Data Matlab
AI Thread Summary
The discussion revolves around creating a MATLAB program to visualize data from a .dat file, specifically focusing on coloring data points based on the value of a variable z. The user seeks to assign colors to different ranges of z, such as red for z between 0 and 1 and blue for z between 1 and 2. Suggestions include using MATLAB functions like pcolor or heatmap for visualization. Additionally, it is recommended to utilize functions like histc, histcounts, and discretize for binning data, with options for customizing colors in plots using properties like FaceColor and CData. The importance of understanding bin edge conventions for accurate plotting is also emphasized.
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')
 
Physics news on Phys.org
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

2018-04-20_10-27-23.png
 

Attachments

  • 2018-04-20_10-27-23.png
    2018-04-20_10-27-23.png
    24.2 KB · Views: 821
  • Like
Likes olivermsun

Similar threads

Replies
12
Views
2K
Replies
8
Views
2K
Replies
5
Views
2K
Replies
9
Views
5K
Replies
1
Views
4K
Replies
2
Views
3K
Replies
1
Views
2K
Replies
12
Views
3K
Back
Top