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.
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.
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π.
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
.
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)
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.
Examples
Create Polar Plot
Plot a line in polar coordinates.
theta = 0:0.01:2pi; rho = sin(2theta).cos(2theta); polarplot(theta,rho)
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)
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
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')
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)
Change the limits of the _r_-axis so it ranges from -1 to 1.
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')
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);
Change the line color and width and add markers.
p.Color = 'magenta'; p.Marker = 'square'; p.MarkerSize = 8;
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,'*')
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");
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";
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
Input Arguments
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:
rho = sqrt(x^2+y^2)
theta = atan(y/x)
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 | ![]() |
"--" | Dashed line | ![]() |
":" | Dotted line | ![]() |
"-." | Dash-dotted line | ![]() |
Marker | Description | Resulting Marker |
---|---|---|
"o" | Circle | ![]() |
"+" | Plus sign | ![]() |
"*" | Asterisk | ![]() |
"." | Point | ![]() |
"x" | Cross | ![]() |
"_" | Horizontal line | ![]() |
"|" | Vertical line | ![]() |
"square" | Square | ![]() |
"diamond" | Diamond | ![]() |
"^" | Upward-pointing triangle | ![]() |
"v" | Downward-pointing triangle | ![]() |
">" | Right-pointing triangle | ![]() |
"<" | Left-pointing triangle | ![]() |
"pentagram" | Pentagram | ![]() |
"hexagram" | Hexagram | ![]() |
Color Name | Short Name | RGB Triplet | Appearance |
---|---|---|---|
"red" | "r" | [1 0 0] | ![]() |
"green" | "g" | [0 1 0] | ![]() |
"blue" | "b" | [0 0 1] | ![]() |
"cyan" | "c" | [0 1 1] | ![]() |
"magenta" | "m" | [1 0 1] | ![]() |
"yellow" | "y" | [1 1 0] | ![]() |
"black" | "k" | [0 0 0] | ![]() |
"white" | "w" | [1 1 1] | ![]() |
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.
pax
— PolarAxes
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.
- 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 from0
toF
. 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 hexadecimal color codes.
Color Name | Short Name | RGB Triplet | Hexadecimal Color Code | Appearance |
---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" | ![]() |
"green" | "g" | [0 1 0] | "#00FF00" | ![]() |
"blue" | "b" | [0 0 1] | "#0000FF" | ![]() |
"cyan" | "c" | [0 1 1] | "#00FFFF" | ![]() |
"magenta" | "m" | [1 0 1] | "#FF00FF" | ![]() |
"yellow" | "y" | [1 1 0] | "#FFFF00" | ![]() |
"black" | "k" | [0 0 0] | "#000000" | ![]() |
"white" | "w" | [1 1 1] | "#FFFFFF" | ![]() |
"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" | ![]() |
[0.8500 0.3250 0.0980] | "#D95319" | ![]() |
[0.9290 0.6940 0.1250] | "#EDB120" | ![]() |
[0.4940 0.1840 0.5560] | "#7E2F8E" | ![]() |
[0.4660 0.6740 0.1880] | "#77AC30" | ![]() |
[0.3010 0.7450 0.9330] | "#4DBEEE" | ![]() |
[0.6350 0.0780 0.1840] | "#A2142F" | ![]() |
Line style, specified as one of the options listed in this table.
Line Style | Description | Resulting Line |
---|---|---|
"-" | Solid line | ![]() |
"--" | Dashed line | ![]() |
":" | Dotted line | ![]() |
"-." | Dash-dotted line | ![]() |
"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 | ![]() |
"+" | Plus sign | ![]() |
"*" | Asterisk | ![]() |
"." | Point | ![]() |
"x" | Cross | ![]() |
"_" | Horizontal line | ![]() |
"|" | Vertical line | ![]() |
"square" | Square | ![]() |
"diamond" | Diamond | ![]() |
"^" | Upward-pointing triangle | ![]() |
"v" | Downward-pointing triangle | ![]() |
">" | Right-pointing triangle | ![]() |
"<" | Left-pointing triangle | ![]() |
"pentagram" | Pentagram | ![]() |
"hexagram" | Hexagram | ![]() |
"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.
- 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 from0
toF
. 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 hexadecimal color codes.
Color Name | Short Name | RGB Triplet | Hexadecimal Color Code | Appearance |
---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" | ![]() |
"green" | "g" | [0 1 0] | "#00FF00" | ![]() |
"blue" | "b" | [0 0 1] | "#0000FF" | ![]() |
"cyan" | "c" | [0 1 1] | "#00FFFF" | ![]() |
"magenta" | "m" | [1 0 1] | "#FF00FF" | ![]() |
"yellow" | "y" | [1 1 0] | "#FFFF00" | ![]() |
"black" | "k" | [0 0 0] | "#000000" | ![]() |
"white" | "w" | [1 1 1] | "#FFFFFF" | ![]() |
"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" | ![]() |
[0.8500 0.3250 0.0980] | "#D95319" | ![]() |
[0.9290 0.6940 0.1250] | "#EDB120" | ![]() |
[0.4940 0.1840 0.5560] | "#7E2F8E" | ![]() |
[0.4660 0.6740 0.1880] | "#77AC30" | ![]() |
[0.3010 0.7450 0.9330] | "#4DBEEE" | ![]() |
[0.6350 0.0780 0.1840] | "#A2142F" | ![]() |
Tips
- To convert data from degrees to radians, use deg2rad. To convert data from radians to degrees, use rad2deg.
- You can modify polar axes properties to customize the chart. For a list of properties, see PolarAxes Properties.
- To plot additional data in the polar axes, use the
hold on
command. However, you cannot plot data that requires Cartesian axes in a polar chart.
Version History
Introduced in R2016a
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 | ![]() |
R2022a | ![]() |
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.