How to Plot Data into Bins in MATLAB: A Step-by-Step Guide

  • MATLAB
  • Thread starter quasarLie
  • Start date
  • Tags
    Data Matlab
In summary, Jedishrfu suggests plotting the data using a histogram and then setting the bars to different colors.
  • #1
quasarLie
51
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
  • #3
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
 
  • #4
Code:
>> help discretize

and then plot using some color map of your choice?
 
  • #5
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: 707
  • Like
Likes olivermsun

1. How do I create bins for my data in MATLAB?

To create bins for your data in MATLAB, you can use the "histogram" function. This function automatically creates bins based on the range of your data. You can also specify the number of bins you want by using the "NumBins" parameter.

2. Can I customize the bin edges in MATLAB?

Yes, you can customize the bin edges in MATLAB by using the "BinEdges" parameter in the "histogram" function. This allows you to specify the exact edges of each bin and can be useful for creating unevenly spaced bins or bins with specific boundaries.

3. How can I plot multiple data sets into bins on the same graph?

You can plot multiple data sets into bins on the same graph by using the "hold on" command after each "histogram" function. This allows you to plot each data set in succession without clearing the previous plot. Alternatively, you can use the "histogram" function with the "grouping" parameter to create a grouped histogram with each data set in its own bin.

4. Is there a way to add labels and titles to the histogram plot?

Yes, you can add labels and titles to the histogram plot by using the "xlabel", "ylabel", and "title" functions. These functions allow you to specify the text and formatting for each label and title. You can also use the "legend" function to add a legend for each data set.

5. Can I save the histogram plot as an image or a file?

Yes, you can save the histogram plot as an image or a file by using the "saveas" function. This function allows you to specify the file type and name for the saved plot. You can also use the "print" function to save the plot as an image or a file with more customization options.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
Back
Top