How to Plot Data into Bins in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter quasarLie
  • Start date Start date
  • Tags Tags
    Data Matlab
Click For Summary
SUMMARY

This discussion focuses on plotting data into bins using MATLAB, specifically utilizing the functions histcounts, histogram, and bar for visual representation. Users can apply color coding to different bins by manipulating the CData property of the bar object. The conversation highlights the importance of understanding bin edge conventions when using these functions to ensure accurate data representation. Additionally, resources such as pcolor and heatmap are suggested for alternative visualization methods.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of data visualization concepts
  • Knowledge of histogram and bar chart plotting techniques
  • Basic understanding of color mapping in graphics
NEXT STEPS USEFUL FOR

Data analysts, MATLAB users, and researchers looking to visualize binned data effectively with customized color coding.

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: 853
  • Like
Likes   Reactions: olivermsun

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K