Set the Location of R-axis Labels using 'polar' in MATLAB R2016a
17 views (last 30 days)
Show older comments
MathWorks Support Team
on 20 Jun 2016
Edited: MathWorks Support Team
on 14 Apr 2023
"polar", automatically puts the R-axis label somewhere near the 80 degrees location. How can I change the R-axis label location or hide it?
Accepted Answer
MathWorks Support Team
on 13 Apr 2023
Edited: MathWorks Support Team
on 14 Apr 2023
Moving the R-axis with 'polarplot'
The 'polarplot' function was introduced in MATLAB R2016a as a replacement for the 'polar' function, which makes it easier to customize polar plots.
See the documentation linked below, which outlines the properties of 'polaraxes' for more information:
The 'RAxisLocation' property can be used to set the location of the R-axis labels in degrees:
% Create a polar plot.
pp = polarplot(sin(0:0.01:10*pi));
% Get a handle to the polar plot axes.
ax = gca;
% Set the location of the R-axis labels in degrees.
degrees = 300;
ax.RAxisLocation = degrees;
If you are unable to use the 'polarplot' function, you can follow the workarounds below to hide or move the R-axis labels using the 'polar' function.
Hiding the R-axis with 'polar'
1.) To hide the R-axis labels, first locate all of the 'Text' objects in the graphics object hierarchy using the 'findall' command. Then, filter the results using the 'HorizontalAlignment' property of the 'Text' objects.
PLEASE NOTE: This filtering method relies on the default value being set to 'left' for the 'HorizontalAlignment' property of the 'Text' objects created by the 'polar' command.
% Taken from 'doc polar' example:
% http://www.mathworks.com/help/matlab/ref/polar.html
theta = 0:0.01:2*pi;
rho = sin(2*theta).*cos(2*theta);
f = figure;
polar(theta,rho,'--r');
% Extract all of the 'Text' objects from the polar plot.
ax = findall(f.Children, 'Type', 'Axes');
% Filter the 'Text' objects by the 'HorizontalAlignment' property.
% PLEASE NOTE: This may not generalize to other versions of MATLAB
% where the default 'HorizontalAlignment' value for R-axis labels is not
% set to 'left'.
labels = findall(ax, 'Type', 'Text', 'HorizontalAlignment', 'left');
% Hide all the R-axis labels.
[labels.Visible] = deal('off');
Moving the R-Axis with 'polar'
2.) To move the R-axis labels, first locate the corresponding 'Text' objects in the graphics object hierarchy, as described above. Then, iterate over the appropriate set of 'Text' objects, and update the 'Position' property of each object.
The code below provides a simple method of setting the 'Position' property of each 'Text' object in degrees on the polar plot. You can modify the value of the 'degrees' variable to choose where you want to place the R-axis labels on the polar plot.
% Taken from 'doc polar' example:
% http://www.mathworks.com/help/matlab/ref/polar.html
theta = 0:0.01:2*pi;
rho = sin(2*theta).*cos(2*theta);
f = figure;
polar(theta,rho,'--r');
% Extract all of the 'Text' objects from the polar plot.
ax = findall(f.Children, 'Type', 'Axes');
% Filter the 'Text' objects by the 'HorizontalAlignment' property.
% PLEASE NOTE: This may not generalize to other versions of MATLAB
% where the default 'HorizontalAlignment' value for R-axis labels is not
% set to 'left'.
labels = findall(ax, 'Type', 'Text', 'HorizontalAlignment', 'left');
% Set the degrees of the R-axis Labels.
degrees = 300;
% Update the position of each R-axis label.
for label = labels'
currentX = label.Position(1);
currentY = label.Position(2);
radius = sqrt(currentX^2 + currentY^2);
newX = cos(degtorad(degrees)) * radius;
newY = sin(degtorad(degrees)) * radius;
label.Position = [newX, newY];
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Polar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!