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

Discussion Overview

The discussion revolves around how to plot data into bins in MATLAB, specifically focusing on color coding based on the values of a variable (z). Participants explore different methods for visualizing binned data, including scatter plots, histograms, and color mapping techniques.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant shares their MATLAB code and expresses a desire to color code data points based on the value of z, specifically using red for z between 0 and 1 and blue for z between 1 and 2.
  • Another participant suggests using a pcolor chart or a heat map as potential solutions for visualizing the data.
  • A different participant reiterates the initial question and emphasizes the need for clarity on how to display the data effectively, referencing graphic object properties in MATLAB.
  • One participant points out that the histogram object can bin data and plot it, while also mentioning that other methods exist where binning and plotting are separate processes. They highlight the importance of understanding the conventions for bin edges when using different functions like histc, histcounts, and discretize.
  • Another participant provides an example of how to plot a histogram with differently colored bars by manipulating the FaceColor property and the CData matrix.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to approach the problem of binning and plotting data in MATLAB. There is no consensus on a single method, as different techniques are suggested and explored.

Contextual Notes

Participants mention various MATLAB functions and properties, but there are no explicit assumptions or limitations discussed regarding the data or the methods proposed.

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: 855
  • 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 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K