Main Content

polarhistogram

Histogram chart in polar coordinates

  • Polar histogram

Description

Vector and Matrix Data

polarhistogram(theta) creates a histogram plot in polar coordinates by sorting the values in theta into equally spaced bins. Specify the values in radians.

example

polarhistogram(theta,nbins) uses the number of bins specified by the positive integer, nbins.

example

polarhistogram(theta,edges) sorts theta into bins with bin edges specified by the vector, edges. All bins include the left edge, but only the last bin includes the right edge. In other words, the last bin includes both edges.

polarhistogram(BinEdges=edges,BinCounts=counts) uses the manually specified bin edges and associated bin counts. The polarhistogram function does not do any data binning.

Table Data

polarhistogram(tbl,datavar) creates a histogram using the variable datavar from the table tbl. The variable you specify can contain values of any numeric type. (since R2026a)

example

polarhistogram(tbl,datavar,nbins) uses table data and the specified number of bins. (since R2026a)

example

polarhistogram(tbl,datavar,edges) uses table data and the specified bin edges. (since R2026a)

example

Additional Options

polarhistogram(___,Name=Value) specifies additional options using one or more name-value arguments. For example, you can use semitransparent bars by specifying FaceAlpha as a scalar value between 0 and 1.

example

polarhistogram(pax,___) plots into the polar axes specified by pax instead of into the current axes.

h = polarhistogram(___) returns the Histogram object. Use h to modify the histogram after it is created. For a list of properties, see Histogram Properties.

example

Examples

collapse all

Create a vector of values between 0 and 2π. Create a histogram chart that shows the data sorted into six bins.

theta = [0.1 1.1 5.4 3.4 2.3 4.5 3.2 3.4 5.6 2.3 2.1 3.5 0.6 6.1];
polarhistogram(theta,6)

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type histogram. This object represents theta.

Before R2022a, polar axes do not include degree symbols by default. To add them, get the polar axes using pax = gca. Then modify the tick labels using pax.ThetaTickLabel = string(pax.ThetaTickLabel) + char(176).

Create a histogram plot from 100,000 values between -π and π, and sort the data into 25 bins.

theta = atan2(rand(100000,1)-0.5,2*(rand(100000,1)-0.5));
polarhistogram(theta,25);

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type histogram. This object represents theta.

Since R2026a

Create a table with a variable named ThetaVals that has 14 angle values in radians. Create a histogram of the angle values.

ThetaVals = [0.1 1.1 5.4 3.4 2.3 4.5 3.2 3.4 5.6 2.3 2.1 3.5 0.6 6.1]';
tbl = table(ThetaVals);
polarhistogram(tbl,"ThetaVals")

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type histogram.

Create a histogram of the same table variable with the data sorted into six bins.

polarhistogram(tbl,"ThetaVals",6)

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type histogram.

Using the same table variable, create a histogram and specify the bin edges.

Edges = [0 0.5 1 1.5 2] * pi;
polarhistogram(tbl,"ThetaVals",Edges)

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type histogram.

Create a histogram chart in polar coordinates, and then change its appearance. Specify the bar colors by setting the FaceColor property to a character vector of a color name, such as 'red', or an RGB triplet. Specify the transparency by setting the FaceAlpha property to a value between 0 and 1.

theta = atan2(rand(100000,1)-0.5,2*(rand(100000,1)-0.5));
polarhistogram(theta,25,'FaceColor','red','FaceAlpha',.3);

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type histogram. This object represents theta.

Create a histogram chart in polar coordinates. Assign the histogram object to the variable h.

theta = atan2(rand(100000,1)-0.5,2*(rand(100000,1)-0.5));
h = polarhistogram(theta,25)

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type histogram. This object represents theta.

h = 
  Histogram with properties:

             Data: [100000×1 double]
           Values: [6232 7236 4699 2717 1908 1641 1622 1755 2207 3463 6471 6806 6429 6741 6380 3422 2233 1777 1570 1633 1899 2768 4755 7273 6363]
          NumBins: 25
         BinEdges: [-3.1416 -2.8903 -2.6389 -2.3876 -2.1363 -1.8850 -1.6336 -1.3823 -1.1310 -0.8796 -0.6283 -0.3770 -0.1257 0.1257 0.3770 0.6283 0.8796 1.1310 1.3823 1.6336 1.8850 2.1363 2.3876 2.6389 2.8903 3.1416]
         BinWidth: 0.2513
        BinLimits: [-3.1416 3.1416]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0.1294 0.1294 0.1294]

  Show all properties

Use h to access and modify properties of the histogram object after it is created. For example, show just the histogram outline by setting the DisplayStyle property of the histogram object.

h.DisplayStyle = 'stairs';

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type histogram. This object represents theta.

Input Arguments

collapse all

Data to distribute among bins, specified as a vector or a matrix. polarhistogram creates one histogram, regardless of whether you specify a vector or a matrix. Specify the values in radians. To convert degrees to radians, use deg2rad.

Values that correspond to the same angle direction differ by exactly 2π, and are sorted into the same bin. polarhistogram does not include NaN, Inf, and -Inf values in any bin.

Example: theta = [0 0.4 0.5 0.7 2.3 3.0 1.7 0.3];

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

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

Example: polarhistogram([0 pi/2 pi/2 pi 3*pi/2 2*pi],3)

Bin edges, specified as a vector. The difference between the minimum and maximum edge values must be less than or equal to 2π.

Example: polarhistogram([0 pi/2 pi 3*pi/2 2*pi],[0 pi 2*pi])

Example: polarhistogram(BinEdges=[0 pi/3 pi 3*pi/2 2*pi],BinCounts=[5 3 4 6])

Bin counts, specified as a vector. Use this option if you perform the bin counts calculation separately and you do not want polarhistogram to do any data binning.

Example: polarhistogram(BinEdges=[0 pi/3 pi 3*pi/2 2*pi],BinCounts=[5 3 4 6])

Source table containing the data to plot, specified as a table or timetable.

Table variable containing the data to distribute among the bins, specified using one of the indexing schemes from the table. The variable you specify can contain values of any numeric type.

Indexing SchemeExamples

Variable name:

  • A string scalar or character vector.

  • A pattern object. The pattern object must refer to only one variable.

  • "A" or 'A' — A variable named A

  • "Var"+digitsPattern(1) — The variable with the name "Var" followed by a single digit

Variable index:

  • An index number that refers to the location of a variable in the table.

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 or false values.

  • 3 — The third variable from the table

  • [false false true] — The third variable

Variable type:

  • A vartype subscript that selects a table variable of a specified type. The subscript must refer to only one variable.

  • vartype("double") — The variable containing double values

Example: polarhistogram(tbl,"Angles") creates a histogram from the table variable named Angles.

PolarAxes object. If you do not specify the polar axes, then polarhistogram uses the current axes. polarhistogram does not support plotting into Cartesian axes.

Name-Value Arguments

collapse all

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.

Example: polarhistogram(theta,FaceAlpha=0.3) creates a histogram chart with semitransparent bars.

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

Bin limits, specified as a two-element vector of the form [bmin,bmax], where bmin is less than bmax.

This option plots a histogram using the input array values that fall between bmin and bmax inclusive. That is, theta(theta>=bmin & theta<=bmax).

Example: polarhistogram(theta,'BinLimits',[-pi/2 pi/2]) plots a histogram using only the values in theta that are between -pi/2 and pi/2 inclusive.

Width across the top of the bins, specified as a scalar less than 2π.

polarhistogram uses a maximum of 65,536 bins (or 216). If the specified bin width requires more bins, then polarhistogram uses the maximum number of bins and adjust the bin width accordingly.

Example: polarhistogram(theta,'BinWidth',pi) uses bins with a width of π.

Type of normalization, specified as one of the values in this table.

ValueDescription
'count'

Default normalization scheme. The height of each bar is the number of observations in each bin. The sum of the bar heights is numel(theta).

'probability'

The height of each bar is the relative number of observations. Each height is calculated as (number of observations in bin/total number of observations). The sum of the bar heights is 1.

'countdensity'

The height of each bar is the number of observations in bin/width of bin.

'pdf'

Probability density function estimate. The height of each bar is (number of observations in the bin)/(total number of observations * width of bin). The area of each bar is the relative number of observations. The sum of the bar areas is 1.

'cumcount'

The height of each bar is the cumulative number of observations in each bin and all previous bins. The height of the last bar is numel(theta).

'cdf'

Cumulative distribution function estimate. The height of each bar is equal to the cumulative relative number of observations in the bin and all previous bins. The height of the last bar is 1.

Example: polarhistogram(theta,'Normalization','pdf') plots an estimate of the probability density function for theta.

Histogram display style, specified as one of these values:

  • 'stairs' — Display the histogram outline only.

  • 'bar' — Show each individual bar with a filled interior.

Example: polarhistogram(theta,'DisplayStyle','stairs') plots the outline of the histogram.

Transparency of histogram bars, specified as a scalar value between 0 and 1 inclusive. polarhistogram uses the same transparency for all the bars of the histogram. A value of 1 means fully opaque and 0 means completely transparent (invisible).

Example: polarhistogram(theta,'FaceAlpha',.5) creates a histogram plot with semi-transparent bars.

Histogram bar color, specified as 'auto', an RGB triplet, a hexadecimal color code, a color name, or a short name. The default value of 'auto' lets the histogram choose the color.

For a custom color, specify an RGB triplet or a hexadecimal color code.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1], for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Therefore, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and the hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

"none"Not applicableNot applicableNot applicableNo color

This table lists the default color palettes for plots in the light and dark themes.

PalettePalette Colors

"gem" — Light theme default

Before R2025a: Most plots use these colors by default.

Sample of the "gem" color palette

"glow" — Dark theme default

Sample of the "glow" color palette

You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.

RGB = orderedcolors("gem");
H = rgb2hex(RGB);

Before R2023b: Get the RGB triplets using RGB = get(groot,"FactoryAxesColorOrder").

Before R2024a: Get the hexadecimal color codes using H = compose("#%02X%02X%02X",round(RGB*255)).

If you specify DisplayStyle as 'stairs', then polarhistogram does not use the FaceColor property.

Example: polarhistogram(theta,'FaceColor','g') creates a histogram plot with green bars.

Limitations

  • polarhistogram does not support creating histograms of categorical data in polar axes.

Version History

Introduced in R2016b

expand all