当前位置:网站首页>The use of histogram function in MATLAB

The use of histogram function in MATLAB

2022-06-11 17:07:00 jk_ one hundred and one

Catalog

grammar

explain

Example

Vector histogram ​

Specifies the of the histogram bin Number

Change the histogram bin Number

Specifies the of the histogram bin The border

Draw classification histogram

Histogram with specified normalization

Draw multiple histograms

Adjust histogram properties  

Determine the basic probability distribution

Save and load the histogram object


         histogram The histogram() function draws a histogram

         Histogram belongs to bar chart type of numerical data , Group data into bin. establish Histogram After the object , You can modify all aspects of the histogram by changing its attribute values . This pair of quick changes bin Properties or changing the display are particularly useful .

grammar

histogram(X)
histogram(X,nbins)
histogram(X,edges)
histogram('BinEdges',edges,'BinCounts',counts)
histogram(C)
histogram(C,Categories)
histogram('Categories',Categories,'BinCounts',counts)
histogram(___,Name,Value)
histogram(ax,___)
h = histogram(___)

explain

​histogram(X)  be based on  X  Create histograms .histogram  The function uses the auto divide bin Algorithm , Then return a uniform width bin, these bin It can cover  X  And display the basic shape of the distribution .histogram  take bin Show as rectangle , So the height of each rectangle represents bin The number of elements in .​

​histogram(X,nbins)  Use scalar  nbins  designated bin Number .

​histogram(X,edges)  take  X  Divided into vectors  edges  To specify the bin The boundary of the bin. Every bin Both contain the left boundary , But does not include the right boundary , Except for the last one that contains both boundaries bin Outside .

histogram('BinEdges',edges,'BinCounts',counts)  Specify manually bin Boundary and associated bin Count .histogram  Draw the specified bin Count , Without performing any data scoring bin.

​histogram(C)( among  C  For sorting arrays ) By providing  C  A bar is drawn for each category in the to draw a histogram .

histogram(C,Categories)  Draw only  Categories  A subset of the specified category .

histogram('Categories',Categories,'BinCounts',counts)  Manually specify categories and associated bin Count .histogram  Draw the specified bin Count , Without performing any data scoring bin.

​histogram(___,Name,Value)  Use any of the preceding syntax to specify that there is one or more  Name,Value  Other options for group parameters . for example , You can specify  'BinWidth'  And a scalar to adjust bin Width , Or specify  'Normalization'  And a valid option ('count'、'probability'、'countdensity'、'pdf'、'cumcount'  or  'cdf') To use different types of normalization .

histogram(ax,___)  Draw the graph to  ax  In the specified coordinate area , Instead of the current coordinate area (gca) in . Options  ax  It can precede any combination of input parameters in the previous syntax .

h = histogram(___)  return  Histogram  object . Use this syntax to check and adjust the properties of the histogram .

Example

Vector histogram ​

        ​ Generate 10,000 A random number and create a histogram .histogram Function automatically selects the appropriate bin Number , To cover x And display the shape of the basic distribution .

x = randn(10000,1);
h = histogram(x)

h = 
  Histogram with properties:

             Data: [10000x1 double]
           Values: [2 2 1 6 7 17 29 57 86 133 193 271 331 421 540 613 ... ]
          NumBins: 37
         BinEdges: [-3.8000 -3.6000 -3.4000 -3.2000 -3 -2.8000 -2.6000 ... ]
         BinWidth: 0.2000
        BinLimits: [-3.8000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Show all properties

          Appoint histogram The output parameters of the function , It returns a binary histogram object . You can use this object to check the properties of the histogram , for example bin Quantity or width .

         Calculate the histogram bin Number .

nbins = h.NumBins

nbins = 37

Specifies the of the histogram bin Number

         To classify as 25 Equidistant bin Of 1,000 Draw a histogram with random numbers .

x = randn(1000,1);
nbins = 25;
h = histogram(x,nbins)

h = 
  Histogram with properties:

             Data: [1000x1 double]
           Values: [1 3 0 6 14 19 31 54 74 80 92 122 104 115 88 80 38 32 ... ]
          NumBins: 25
         BinEdges: [-3.4000 -3.1200 -2.8400 -2.5600 -2.2800 -2 -1.7200 ... ]
         BinWidth: 0.2800
        BinLimits: [-3.4000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Show all properties

         seek bin Count . 

counts = h.Values

counts = 1×25

     1     3     0     6    14    19    31    54    74    80    92   122   104   115    88    80    38    32    21     9     5     5     5     0     2

Change the histogram bin Number

         Generate 1,000 A random number and create a histogram .

X = randn(1000,1);
h = histogram(X)

h = 
  Histogram with properties:

             Data: [1000x1 double]
           Values: [3 1 2 15 17 27 53 79 85 101 127 110 124 95 67 32 27 ... ]
          NumBins: 23
         BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 ... ]
         BinWidth: 0.3000
        BinLimits: [-3.3000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Show all properties

          Use morebins Function rough adjustment bin Number .

Nbins = morebins(h);
Nbins = morebins(h)

Nbins = 29

         By explicitly setting bin The number is adjusted according to the fine particle level bin. 

h.NumBins = 31;

Specifies the of the histogram bin The border

         Generate 1,000 A random number and create a histogram . take bin The boundary is specified as a vector , Make wide bin On both sides of the histogram , To capture dissatisfaction  ∣x∣<2  Outliers of . The first vector element is the first bin The left border , And the last vector element is the last bin The right border of .

x = randn(1000,1);
edges = [-10 -2:0.25:2 10];
h = histogram(x,edges);

         take Normalization Property specified as 'countdensity' So that the bin flat . Now? , Every bin Region ( Not height ) It means that we should bin The frequency of the observed value .

h.Normalization = 'countdensity';

Draw classification histogram

         Create a category vector that represents the vote . The categories in this vector are 'yes'、'no' or 'undecided'.

A = [0 0 1 1 1 0 0 0 0 NaN NaN 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];
C = categorical(A,[1 0 NaN],{'yes','no','undecided'})
C = 1x27 categorical
  Columns 1 through 9

     no      no      yes      yes      yes      no      no      no      no 

  Columns 10 through 16

     undecided      undecided      yes      no      no      no      yes 

  Columns 17 through 25

     no      yes      no      yes      no      no      no      yes      yes 

  Columns 26 through 27

     yes      yes 

         Use relative bar widths  0.5  Draw the classification histogram of voting .

h = histogram(C,'BarWidth',0.5)

h = 
  Histogram with properties:

              Data: [no    no    yes    yes    yes    no    no    ...    ]
            Values: [11 14 2]
    NumDisplayBins: 3
        Categories: {'yes'  'no'  'undecided'}
      DisplayOrder: 'data'
     Normalization: 'count'
      DisplayStyle: 'bar'
         FaceColor: 'auto'
         EdgeColor: [0 0 0]

  Show all properties

Histogram with specified normalization

         Generate 1,000 Random numbers and use 'probability' Normalization creates histogram . 

x = randn(1000,1);
h = histogram(x,'Normalization','probability')

h = 
  Histogram with properties:

             Data: [1000x1 double]
           Values: [0.0030 1.0000e-03 0.0020 0.0150 0.0170 0.0270 0.0530 ... ]
          NumBins: 23
         BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 ... ]
         BinWidth: 0.3000
        BinLimits: [-3.3000 3.6000]
    Normalization: 'probability'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Show all properties

          Calculate the sum of the bar heights . By this normalization , The height of each bar is equal to that of the bin The probability of selecting observations within the interval , And the total height of all the bars is 1.

S = sum(h.Values)

S = 1

Draw multiple histograms

         Generate two random number vectors and draw a corresponding histogram for each vector in the same window .

x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);

         Due to the sample size and bin The width is different , It is difficult to compare them . These histograms are normalized , So all the bar heights add up and the result is 1 And use unified bin Width . 

h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;

Adjust histogram properties  

         Generate 1,000 A random number and create a histogram . Returns the histogram object to adjust the properties of the histogram , There is no need to recreate the entire drawing .

x = randn(1000,1);
h = histogram(x)

h = 
  Histogram with properties:

             Data: [1000x1 double]
           Values: [3 1 2 15 17 27 53 79 85 101 127 110 124 95 67 32 27 ... ]
          NumBins: 23
         BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 ... ]
         BinWidth: 0.3000
        BinLimits: [-3.3000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Show all properties

         Specify exactly what to use bin Number . 

h.NumBins = 15;

         Specify by vector bin The border . The first value in the vector is the first bin The left border . The last value is the last bin The right border of .

h.BinEdges = [-3:3];

         Change the color of the histogram bar . 

h.FaceColor = [0 0.5 0.5];
h.EdgeColor = 'r';

Determine the basic probability distribution

         Generate 5,000 The average is 5、 The standard deviation is 2 The normal distribution random number of . stay  Normalization  Set to  'pdf'  The histogram can generate the estimated value of the probability density function .

x = 2*randn(5000,1) + 5;
histogram(x,'Normalization','pdf')

         In this example , The basic distribution of normal distribution data is known . however , By comparing it with the known probability density function , have access to  'pdf'  The histogram determines the basic probability distribution of the data .

         The mean for  μ、 The standard deviation is  σ  And the variance is  σ^2  The probability density function of the normal distribution of is :

  For the mean value is 5、 The standard deviation is 2 Is a normal distribution , Superimpose a probability density function graph . 

hold on
y = -5:0.1:15;
mu = 5;
sigma = 2;
f = exp(-(y-mu).^2./(2*sigma^2))./(sigma*sqrt(2*pi));
plot(y,f,'LineWidth',1.5)

Save and load the histogram object

         Use savefig Function save histogram Picture window .

histogram(randn(10));
savefig('histogram.fig');
close gcf

         Use openfig Reload the histogram into MATLAB.openfig Also return to the picture window h The handle of .

h = openfig('histogram.fig');

         Use findobj Function to find the correct object handle from the window handle . such , You can continue to work on the original histogram object used to generate the window . 

y = findobj(h,'type','histogram')

y = 
  Histogram with properties:

             Data: [10x10 double]
           Values: [2 17 28 32 16 3 2]
          NumBins: 7
         BinEdges: [-3 -2 -1 0 1 2 3 4]
         BinWidth: 1
        BinLimits: [-3 4]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Show all properties

Tips

  • Use histogram The created histogram provides a context menu in plot edit mode , To allow interactive operation in the drawing window . for example , You can use the context menu to change... Interactively bin The number of 、 Align multiple histograms or change the display order .

  • When adding data hints to the histogram , They will show bin Boundary and bin Count .

原网站

版权声明
本文为[jk_ one hundred and one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111657510207.html