Hướng dẫn dùng hystogram python

Nội dung chính

  • Description
  • Description
  • Input Arguments
  • X — Data to distribute among bins vector | matrix | multidimensional array
  • C — Categorical data categorical array
  • nbins — Number of bins positive integer
  • edges — Bin edges vector
  • counts — Bin counts vector
  • ax — Target axes Axes object | PolarAxes object
  • EdgeAlpha — Transparency of histogram bar edges 1 [default] | scalar value between 0 and 1 inclusive
  • EdgeColor — Histogram edge color [0 0 0] or black [default] | 'none' | 'auto' | RGB triplet | hexadecimal color code | color name
  • FaceAlpha — Transparency of histogram bars 0.6 [default] | scalar value between 0 and 1 inclusive
  • FaceColor — Histogram bar color 'auto' [default] | 'none' | RGB triplet | hexadecimal color code | color name
  • Object Functions
  • Histogram of Vector
  • Specify Number of Histogram Bins
  • Change Number of Histogram Bins
  • Specify Bin Edges of Histogram
  • Plot Categorical Histogram
  • Histogram with Specified Normalization
  • Plot Multiple Histograms
  • Adjust Histogram Properties
  • Determine Underlying Probability Distribution
  • Saving and Loading Histogram Objects
  • Extended Capabilities
  • Tall Arrays Calculate with arrays that have more rows than fit in memory.
  • GPU Arrays Accelerate code by running on a graphics processing unit [GPU] using Parallel Computing Toolbox™.
  • Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
  • Version History

Description

Histograms are a type of bar plot for numeric data that group the data into bins. After you create a Histogram object, you can modify aspects of the histogram by changing its property values. This is particularly useful for quickly modifying the properties of the bins or changing the display.

Creation

Syntax

Description

example

histogram[X] creates a histogram plot of X. The histogram function uses an automatic binning algorithm that returns bins with a uniform width, chosen to cover the range of elements in X and reveal the underlying shape of the distribution. histogram displays the bins as rectangles such that the height of each rectangle indicates the number of elements in the bin.

example

histogram[X,nbins] uses a number of bins specified by the scalar, nbins.

example

histogram[X,edges] sorts X into bins with the bin edges specified by the vector, edges. Each bin includes the left edge, but does not include the right edge, except for the last bin which includes both edges.

histogram['BinEdges',edges,'BinCounts',counts] manually specifies bin edges and associated bin counts. histogram plots the specified bin counts and does not do any data binning.

example

histogram[C], where C is a categorical array, plots a histogram with a bar for each category in C.

histogram[C,Categories] plots only the subset of categories specified by Categories.

histogram['Categories',Categories,'BinCounts',counts] manually specifies categories and associated bin counts. histogram plots the specified bin counts and does not do any data binning.

example

histogram[___,Name,Value] specifies additional options with one or more Name,Value pair arguments using any of the previous syntaxes. For example, you can specify 'BinWidth' and a scalar to adjust the width of the bins, or 'Normalization' with a valid option ['count', 'probability', 'countdensity', 'pdf', 'cumcount', or 'cdf'] to use a different type of normalization. For a list of properties, see Histogram Properties.

histogram[ax,___] plots into the axes specified by ax instead of into the current axes [gca]. The option ax can precede any of the input argument combinations in the previous syntaxes.

example

h = histogram[___] returns a Histogram object. Use this to inspect and adjust the properties of the histogram. For a list of properties, see Histogram Properties.

Input Arguments

expand all

X — Data to distribute among bins vector | matrix | multidimensional array

Data to distribute among bins, specified as a vector, matrix, or multidimensional array. If X is not a vector, then histogram treats it as a single column vector, X[:], and plots a single histogram.

histogram ignores all NaN and NaT values. Similarly, histogram ignores Inf and -Inf values, unless the bin edges explicitly specify Inf or -Inf as a bin edge. Although NaN, NaT, Inf, and -Inf values are typically not plotted, they are still included in normalization calculations that include the total number of data elements, such as 'probability'.

Note

If X contains integers of type int64 or uint64 that are larger than flintmax, then it is recommended that you explicitly specify the histogram bin edges. histogram automatically bins the input data using double precision, which lacks integer precision for numbers greater than flintmax.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | datetime | duration

C — Categorical data categorical array

Categorical data, specified as a categorical array. histogram does not plot undefined categorical values. However, undefined categorical values are still included in normalization calculations that include the total number of data elements, such as 'probability'.

Data Types: categorical

nbins — Number of bins positive integer

Number of bins, specified as a positive integer. If you do not specify nbins, then histogram automatically calculates how many bins to use based on the values in X.

Example: histogram[X,15] creates a histogram with 15 bins.

edges — Bin edges vector

Bin edges, specified as a vector. edges[1] is the left edge of the first bin, and edges[end] is the right edge of the last bin.

The value X[i] is in the kth bin if edges[k]X[i] < edges[k+1]. The last bin also includes the right bin edge, so that it contains X[i] if edges[end-1]X[i]edges[end].

For datetime and duration data, edges must be a datetime or duration vector in monotonically increasing order.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | datetime | duration

Note

This option only applies to categorical histograms.

Categories included in histogram, specified as a cell array of character vectors, categorical array, or string array.

  • If you specify an input categorical array C, then by default, histogram plots a bar for each category in C. In that case, use Categories to specify a unique subset of the categories instead.

  • If you specify bin counts, then Categories specifies the associated category names for the histogram.

Example: h = histogram[C,{'Large','Small'}] plots only the categorical data in the categories 'Large' and 'Small'.

Example: histogram['Categories',{'Yes','No','Maybe'},'BinCounts',[22 18 3]] plots a histogram that has three categories with the associated bin counts.

Example: h.Categories queries the categories that are in histogram object h.

Data Types: cell | categorical | string

counts — Bin counts vector

Bin counts, specified as a vector. Use this input to pass bin counts to histogram when the bin counts calculation is performed separately and you do not want histogram to do any data binning.

The length of counts must be equal to the number of bins.

  • For numeric histograms, the number of bins is length[edges]-1.

  • For categorical histograms, the number of bins is equal to the number of categories.

Example: histogram['BinEdges',-2:2,'BinCounts',[5 8 15 9]]

Example: histogram['Categories',{'Yes','No','Maybe'},'BinCounts',[22 18 3]]

ax — Target axes Axes object | PolarAxes object

Target axes, specified as an Axes object or a PolarAxes object. If you do not specify the axes and if the current axes are Cartesian axes, then the histogram function uses the current axes [gca]. To plot into polar axes, specify the PolarAxes object as the first input argument or use the polarhistogram function.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: histogram[X,'BinWidth',5]

The histogram properties listed here are only a subset. For a complete list, see Histogram Properties.

Note

This option only applies to histograms of categorical data.

Relative width of categorical bars, specified as a scalar value in the range [0,1]. Use this property to control the separation of categorical bars within the histogram. The default value is 0.9, which means that the bar width is 90% of the space from the previous bar to the next bar, with 5% of that space on each side.

If you set this property to 1, then adjacent bars touch.

Example: 0.5

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Bin limits, specified as a two-element vector, [bmin,bmax]. This option plots a histogram using the values in the input array, X, that fall between bmin and bmax inclusive. That is, X[X>=bmin & X

Chủ Đề