MATLAB Answers

How can I show radial ticks and axis on top of a polar plot?

46 views (last 30 days)
Taro
Taro on 14 Jul 2020 at 9:44
Commented: Adam Danz on 15 Jul 2020 at 13:47
Although not a MWE, this is what the code looks like:
figure;
polaraxes; hold on;
polarscatter(vara,varb,[],varc,'.');
c = colorbar;
colormap(map);
In this case, the plot completely hides the ticks and most of the axes in the radial direction.
Is there any way to bring these two elements to the front of the plot?
I tried what's suggested in this answer by a MATLAB Staff, but the function findobj returns an empty array for both line and text.

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 14 Jul 2020 at 16:17
Edited: Adam Danz on 14 Jul 2020 at 16:38
The older answer you pointed to refers to the much older polar() function which uses a cartesian plot to create the polas axes and line objects to create the grid. The new and improved polaraxes(), polarplot() and friends are completely different objects with different properties.
I don't think there's a way to put the grid or the rho tick labels on top.
Here's a workaround.
Method 1: Plot the grid and rho tick labels manually.
See inline comments for details.
Demo:
% Create main axes
fig = figure();
h = polaraxes(fig);
% Plot the data
polarscatter(h, rand(1,4000)*2*pi, rand(1,4000)*10,'.')
% [Do axis modifications here if needed]
% Recreate grid
th = (0:359)*pi/180;
hold(h,'on')
polarplot(h,th,repmat(h.RTick(:),1,numel(th)), 'k-') % Rho grid
polarplot(h, h.ThetaTick.*[1;1]*pi/180, h.RTick([1,end])', 'k-') %Theta grid
% Remove old grid
h.RGrid = 'off';
h.ThetaGrid = 'off';
% Recreate rho ticks
rTickTheta = 80*pi/180 * ones(size(h.RTick));
th = text(rTickTheta, h.RTick, h.RTickLabel, 'Color','r', ...
'HorizontalAlignment','center','VerticalAlignment','Middle');
% Place transparent white background behind rho tick labels for increased visibility
set(th, 'BackgroundColor', [1 1 1 .5]) % the 4th value controls transparency level
% Remove original rho tick labels
h.RTickLabel = {};
Method 2: Copy the polar axes and lay it on top of the original
This method is a bit more clumsy than method 1 but it also does the job. It copies the empty polaraxes and plots it on top of the orginal polar axes. It then alters the copied grid and labels to increase visibility. The outcome is the same as the image above except this method does not include the BackgroundColor option that method 1 provides. I recommend using method 1.
See inline comments for more details.
Demo:
% Create main axes
fig = figure();
h = polaraxes(fig);
% Before plotting, make copy of axes
h2 = copyobj(h,fig);
h2.Color = 'none'; % see through the top axes
% Plot to the *original* axes which is under the copied axes.
polarscatter(h, rand(1,4000)*2*pi, rand(1,4000)*10,'.')
% Make the grid & tick labels on the copied axies more visible
h2.RColor = 'r';
h2.GridColor = 'k';
h2.GridAlpha = .75;
% Make sure the axis limits and other properties match between the two axes
linkprop([h,h2],'RLim','ThetaLim','Position',...
'ThetaAxisUnits','ThetaDir','ThetaZeroLocation', ...
'ThetaTick','RTick') % Add whatever else you need

  2 Comments

Taro
Taro on 15 Jul 2020 at 5:00
Beautiful! Thank you so much, Adam. I am using method 1 in order to have a fully opaque white background behind the numbers.
Adam Danz
Adam Danz on 15 Jul 2020 at 13:47
Sounds good! Just a heads up; the transparency of the background is controlled by an undocumented method (the 4rd element of an RGB triplet). In the past, the transparency was lost when saving the figures and re-opening them. I'm not sure if that's been fixed. But if you're using a fully opaque backround (=1) then this won't be a problem at all.

Sign in to comment.

More Answers (0)