polarplot - Plot line in polar coordinates - MATLAB (original) (raw)

Plot line in polar coordinates

Syntax

Description

Vector and Matrix Data

polarplot([theta](#buyrm3v-theta),[rho](#buyrm3v-rho)) plots a line in polar coordinates, with theta indicating the angle in radians and rho indicating the radius value for each point. The inputs must be vectors of equal length or matrices of equal size. If the inputs are matrices, then polarplot plots columns ofrho versus columns of theta. Alternatively, one of the inputs can be a vector and the other a matrix as long as the vector is the same length as one dimension of the matrix.

example

polarplot([theta](#buyrm3v-theta),[rho](#buyrm3v-rho),[LineSpec](#buyrm3v%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)) sets the line style, marker symbol, and color for the line.

example

polarplot([theta](#buyrm3v-theta)1,[rho](#buyrm3v-rho)1,...,[theta](#buyrm3v-theta)N,[rho](#buyrm3v-rho)N) plots multiple rho,theta pairs.

polarplot([theta](#buyrm3v-theta)1,[rho](#buyrm3v-rho)1,[LineSpec](#buyrm3v%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)1,...,[theta](#buyrm3v-theta)N,[rho](#buyrm3v-rho)N,[LineSpec](#buyrm3v%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)N) specifies the line style, marker symbol, and color for each line.

polarplot([rho](#buyrm3v-rho)) plots the radius values in rho at evenly spaced angles between 0 and 2π.

example

polarplot([rho](#buyrm3v-rho),[LineSpec](#buyrm3v%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)) sets the line style, marker symbol, and color for the line.

polarplot([Z](#buyrm3v-Z)) plots the complex values in Z.

example

polarplot([Z](#buyrm3v-Z),[LineSpec](#buyrm3v%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)) sets the line style, marker symbol, and color for the line.

Table Data

polarplot([tbl](#buyrm3v%5Fsep%5Fmw%5F1f5b8358-45d8-4c4d-8a0d-17e2da07c841),[thetavar](#mw%5F15e2ef52-8832-4f3e-b4b7-71865563362a),[rhovar](#mw%5F0aebf570-790d-4d0f-8ae4-f593dae84154)) plots the variables thetavar and rhovar from the table tbl. To plot one data set, specify one variable for thetavar and one variable forrhovar. To plot multiple data sets, specify multiple variables for thetavar, rhovar, or both. If both arguments specify multiple variables, they must specify the same number of variables. (since R2022a)

example

polarplot([tbl](#buyrm3v%5Fsep%5Fmw%5F1f5b8358-45d8-4c4d-8a0d-17e2da07c841),[rhovar](#mw%5F0aebf570-790d-4d0f-8ae4-f593dae84154)) plots the radius values in rhovar at evenly spaced angles between 0 and 2π. Timetables are not supported for this syntax. (since R2022a)

Additional Options

polarplot([pax](#buyrm3v-pax),___) uses the PolarAxes object specified bypax, instead of the current axes.

polarplot(___,[Name,Value](#namevaluepairarguments)) specifies properties of the chart line using one or moreName,Value pair arguments. The property settings apply to all the lines. You cannot specify different property values for different lines using Name,Value pairs.

`p` = polarplot(___) returns one or more chart line objects. Use p to set properties of a specific chart line object after it is created. For a list of properties, see Line Properties.

example

Examples

collapse all

Create Polar Plot

Plot a line in polar coordinates.

theta = 0:0.01:2pi; rho = sin(2theta).cos(2theta); polarplot(theta,rho)

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

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).

Convert from Degrees to Radians Before Plotting

Create the data to plot.

theta = linspace(0,360,50); rho = 0.005*theta/10;

Convert the values in theta from degrees to radians. Then, plot the data in polar coordinates.

theta_radians = deg2rad(theta); polarplot(theta_radians,rho)

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

Plot Multiple Lines in Polar Coordinates

Plot two lines in polar coordinates. Use a dashed line for the second line.

theta = linspace(0,6*pi); rho1 = theta/10; polarplot(theta,rho1)

rho2 = theta/12; hold on polarplot(theta,rho2,'--') hold off

Figure contains an axes object with type polaraxes. The polaraxes object contains 2 objects of type line.

Plot Radius Values at Equally Spaced Angles

Specify only the radius values, without specifying the angle values. polarplot plots the radius values at equally spaced angles that span from 0 to 2π. Display a circle marker at each data point.

rho = 10:5:70; polarplot(rho,'-o')

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

Plot Negative Radius Values

Create a polar plot using negative radius values. By default, polarplot reflects negative values through the origin.

theta = linspace(0,2*pi); rho = sin(theta); polarplot(theta,rho)

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

Change the limits of the _r_-axis so it ranges from -1 to 1.

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

Specify Line Color for Polar Plot

Create a polar plot using a red line with circle markers.

theta = linspace(0,2pi,25); rho = 2theta; polarplot(theta,rho,'r-o')

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

Specify Line Color After Creation

Create a polar plot and return the chart line object.

theta = linspace(0,2pi,25); rho = 2theta; p = polarplot(theta,rho);

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

Change the line color and width and add markers.

p.Color = 'magenta'; p.Marker = 'square'; p.MarkerSize = 8;

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

Create Polar Plot with Complex Values

Plot complex values in polar coordinates. Display markers at each point without a line connecting them.

Z = [2+3i 2 -1+4i 3-4i 5+2i -4-2i -2+3i -2 -3i 3i-2i]; polarplot(Z,'*')

Figure contains an axes object with type polaraxes. The polaraxes contains a line object which displays its values using only markers.

Plot Coordinates from a Table

A convenient way to plot data from a table is to pass the table to the polarplot function and specify the variables to plot.

Create a table with two variables. Then display the first three rows of the table.

Angle = linspace(0,3*pi,50)'; Radius = (1:50)'; tbl = table(Angle,Radius); head(tbl,3)

 Angle     Radius
_______    ______

      0      1   
0.19234      2   
0.38468      3   

Plot the Angle and Radius variables. Return the Line object as p.

p = polarplot(tbl,"Angle","Radius");

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

To modify aspects of the line, set the LineStyle, Color, and Marker properties on the Line object. For example, change the line to a red dotted line with circle markers.

p.LineStyle = ":"; p.Color = "red"; p.Marker = "o";

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

Plot Multiple Data Sets from Table

Create a table with three variables. Then display the first three rows in the table.

Angle = linspace(0,3*pi,50)'; Radius1 = (1:50)'; Radius2 = Radius1/2; tbl = table(Angle,Radius1,Radius2); head(tbl,3)

 Angle     Radius1    Radius2
_______    _______    _______

      0       1         0.5  
0.19234       2           1  
0.38468       3         1.5  

Plot the Radius1 and Radius2 variables against the Angle variable. Add a legend. Notice that the legend labels match the variable names.

polarplot(tbl,"Angle",["Radius1" "Radius2"]) legend

Figure contains an axes object with type polaraxes. The polaraxes object contains 2 objects of type line.

Input Arguments

collapse all

theta — Angle values

vector | matrix

Angle values, specified as a vector or matrix. Specify the values in radians. To convert data from degrees to radians, use deg2rad.

To change the limits of the theta-axis, use thetalim.

Example: [0 pi/2 pi 3*pi/2 2*pi]

rho — Radius values

vector | matrix

Radius values, specified as a vector or matrix. By default, negative values are reflected through 0. A point is reflected by taking the absolute value of its radius, and adding 180 degrees to its angle.

To change the limits of the _r_-axis, use rlim.

Example: [1 2 3 4 5]

Z — Complex values

vector | matrix

Complex values, specified as a vector or matrix where each element is of the form rho*_e_i*theta, or x+iy, where:

Example: [1+2i 3+4i 3i]

LineSpec — Line style, marker, and color

string scalar | character vector

Line style, marker, and color, specified as a string scalar or character vector containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.

Example: "--or" is a red dashed line with circle markers.

Line Style Description Resulting Line
"-" Solid line Sample of solid line
"--" Dashed line Sample of dashed line
":" Dotted line Sample of dotted line
"-." Dash-dotted line Sample of dash-dotted line, with alternating dashes and dots
Marker Description Resulting Marker
"o" Circle Sample of circle marker
"+" Plus sign Sample of plus sign marker
"*" Asterisk Sample of asterisk marker
"." Point Sample of point marker
"x" Cross Sample of cross marker
"_" Horizontal line Sample of horizontal line marker
"|" Vertical line Sample of vertical line marker
"square" Square Sample of square marker
"diamond" Diamond Sample of diamond marker
"^" Upward-pointing triangle Sample of upward-pointing triangle marker
"v" Downward-pointing triangle Sample of downward-pointing triangle marker
">" Right-pointing triangle Sample of right-pointing triangle marker
"<" Left-pointing triangle Sample of left-pointing triangle marker
"pentagram" Pentagram Sample of pentagram marker
"hexagram" Hexagram Sample of hexagram marker
Color Name Short Name RGB Triplet Appearance
"red" "r" [1 0 0] Sample of the color red
"green" "g" [0 1 0] Sample of the color green
"blue" "b" [0 0 1] Sample of the color blue
"cyan" "c" [0 1 1] Sample of the color cyan
"magenta" "m" [1 0 1] Sample of the color magenta
"yellow" "y" [1 1 0] Sample of the color yellow
"black" "k" [0 0 0] Sample of the color black
"white" "w" [1 1 1] Sample of the color white

tbl — Source table

table | timetable

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

thetavar — Table variables containing theta values

string array | character vector | cell array | pattern | numeric scalar or vector | logical vector | vartype()

Table variables containing the theta values, specified using one of the indexing schemes from the table.

Indexing Scheme Examples
Variable names: A string, character vector, or cell array.A pattern object. "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Variable type: A vartype subscript that selects variables of a specified type. vartype("categorical") — All the variables containing categorical values

The table variables you specify can contain any real numeric data type. Ifthetavar and rhovar both specify multiple variables, the number of variables must be the same.

Example: polarplot(tbl,["th1","th2"],"rho") specifies the table variables named th1 and th2 for the _theta_-coordinates.

Example: polarplot(tbl,2,"rho") specifies the second variable for the _theta_-coordinates.

Example: polarplot(tbl,vartype("numeric"),"rho") specifies all numeric variables for the_theta_-coordinates.

rhovar — Table variables containing rho values

string array | character vector | cell array | pattern | numeric scalar or vector | logical vector | vartype()

Table variables containing the rho values, specified using one of the indexing schemes from the table.

Indexing Scheme Examples
Variable names: A string, character vector, or cell array.A pattern object. "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Variable type: A vartype subscript that selects variables of a specified type. vartype("categorical") — All the variables containing categorical values

The table variables you specify can contain any real numeric data type. Ifthetavar and rhovar both specify multiple variables, the number of variables must be the same.

Example: polarplot(tbl,"theta",["rho1","rho2"]) specifies the table variables named rho1 andrho2 for the radius values.

Example: polarplot(tbl,"theta",2) specifies the second variable for the radius values.

Example: polarplot(tbl,"theta",vartype("numeric")) specifies all numeric variables for the radius values.

paxPolarAxes object

PolarAxes object

PolarAxes object. You can modify the appearance and behavior of a PolarAxes object by setting its properties. For a list of properties, see PolarAxes Properties.

Name-Value Arguments

Specify optional pairs of arguments asName1=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: 'LineWidth',3

Name,Value pair settings apply to all the lines plotted. You cannot specify different Name,Value pairs for each line using this syntax. Instead, return the chart line objects and use dot notation to set the properties for each line.

The properties listed here are only a subset. For a full list, see Line Properties.

Color — Line color

[0 0.4470 0.7410] (default) | RGB triplet | hexadecimal color code | 'r' | 'g' | 'b' | ...

Line color, specified as an RGB triplet, a hexadecimal color code, a color name, or a short name.

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

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

Color Name Short Name RGB Triplet Hexadecimal Color Code Appearance
"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 applicable Not applicable Not applicable No color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.

RGB Triplet Hexadecimal Color Code Appearance
[0 0.4470 0.7410] "#0072BD" Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue
[0.8500 0.3250 0.0980] "#D95319" Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange
[0.9290 0.6940 0.1250] "#EDB120" Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow
[0.4940 0.1840 0.5560] "#7E2F8E" Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple
[0.4660 0.6740 0.1880] "#77AC30" Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green
[0.3010 0.7450 0.9330] "#4DBEEE" Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue
[0.6350 0.0780 0.1840] "#A2142F" Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

Line style, specified as one of the options listed in this table.

Line Style Description Resulting Line
"-" Solid line Sample of solid line
"--" Dashed line Sample of dashed line
":" Dotted line Sample of dotted line
"-." Dash-dotted line Sample of dash-dotted line, with alternating dashes and dots
"none" No line No line

Line width, specified as a positive value in points, where 1 point = 1/72 of an inch. If the line has markers, then the line width also affects the marker edges.

The line width cannot be thinner than the width of a pixel. If you set the line width to a value that is less than the width of a pixel on your system, the line displays as one pixel wide.

Marker symbol, specified as one of the values listed in this table. By default, the object does not display markers. Specifying a marker symbol adds markers at each data point or vertex.

Marker Description Resulting Marker
"o" Circle Sample of circle marker
"+" Plus sign Sample of plus sign marker
"*" Asterisk Sample of asterisk marker
"." Point Sample of point marker
"x" Cross Sample of cross marker
"_" Horizontal line Sample of horizontal line marker
"|" Vertical line Sample of vertical line marker
"square" Square Sample of square marker
"diamond" Diamond Sample of diamond marker
"^" Upward-pointing triangle Sample of upward-pointing triangle marker
"v" Downward-pointing triangle Sample of downward-pointing triangle marker
">" Right-pointing triangle Sample of right-pointing triangle marker
"<" Left-pointing triangle Sample of left-pointing triangle marker
"pentagram" Pentagram Sample of pentagram marker
"hexagram" Hexagram Sample of hexagram marker
"none" No markers Not applicable

Marker size, specified as a positive value in points, where 1 point = 1/72 of an inch.

Marker fill color, specified as "auto", an RGB triplet, a hexadecimal color code, a color name, or a short name. The "auto" option uses the same color as the Color property of the parent axes. If you specify"auto" and the axes plot box is invisible, the marker fill color is the color of the figure.

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

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

Color Name Short Name RGB Triplet Hexadecimal Color Code Appearance
"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 applicable Not applicable Not applicable No color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB Triplet Hexadecimal Color Code Appearance
[0 0.4470 0.7410] "#0072BD" Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue
[0.8500 0.3250 0.0980] "#D95319" Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange
[0.9290 0.6940 0.1250] "#EDB120" Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow
[0.4940 0.1840 0.5560] "#7E2F8E" Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple
[0.4660 0.6740 0.1880] "#77AC30" Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green
[0.3010 0.7450 0.9330] "#4DBEEE" Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue
[0.6350 0.0780 0.1840] "#A2142F" Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

Tips

Version History

Introduced in R2016a

expand all

R2022b: Plots created with tables preserve special characters in legend labels

When you pass a table and one or more variable names to the polarplot function, the legend labels now display any special characters that are included in the table variable names, such as underscores. Previously, special characters were interpreted as TeX or LaTeX characters.

For example, if you pass a table containing a variable named Sample_Number to the polarplot function and then display a legend, the underscore appears in the legend label. In R2022a and earlier releases, the underscore is interpreted as a subscript.

Release Label for Table Variable "Sample_Number"
R2022b Label for the variable name "Sample_Number" in R2022b. The underscore is included in the label.
R2022a Label for the variable name "Sample_Number" in R2022a. The N character displays as a subscript.

To display legend labels with TeX or LaTeX formatting, call the legend function with the desired label strings after plotting. For example:

legend(["Sample_Number" "Another_Legend_Label"])

R2022a: Pass tables directly to polarplot

Create plots by passing a table to the polarplot function followed by the variables you want to plot. When you specify your data as a table, the axis labels and the legend (if present) are automatically labeled using the table variable names.