Change axis in polar plots in matlab to radians

风格不统一 提交于 2019-12-24 00:39:00

问题


So matlab rightfully uses radians for trigonometric functions and in the actual plotting of polar plots. However annoyingly it puts the angular axis in degrees, is there any way to change this?


回答1:


The text objects representing angular values are created by the polar function in the following section of its code (Matlab R2010b):

% annotate spokes in degrees
rt = 1.1 * rmax;
for i = 1 : length(th)
    text(rt * cst(i), rt * snt(i), int2str(i * 30),...
        'HorizontalAlignment', 'center', ...
        'HandleVisibility', 'off', 'Parent', cax);
    if i == length(th)
        loc = int2str(0);
    else
        loc = int2str(180 + i * 30);
    end
    text(-rt * cst(i), -rt * snt(i), loc, 'HorizontalAlignment', 'center', ...
        'HandleVisibility', 'off', 'Parent', cax);
end

Since 'HandleVisibility' is set to 'off' by the code, those text objects are not visible from outside the function, and thus you can't modify their 'String' property.

You can create a modified version of polar (save it with a different name, in a user folder) in which the above section is replaced by this:

% annotate spokes in degrees
rt = 1.13 * rmax;
text_strings = {'\pi/6'  '\pi/3'  '\pi/2'  '2\pi/3' '5\pi/6'  '\pi' ...
                '7\pi/6' '4\pi/3' '3\pi/2' '5\pi/3' '11\pi/6' '0'};
for i = 1 : length(th)
    text(rt * cst(i), rt * snt(i), text_strings{i}, ...
        'HorizontalAlignment', 'center', ...
        'HandleVisibility', 'off', 'Parent', cax);
    text(-rt * cst(i), -rt * snt(i), text_strings{i+6}, ...
        'HorizontalAlignment', 'center', ...
        'HandleVisibility', 'off', 'Parent', cax);
end

Comments on this code:

  • Maybe you prefer to define text_strings without simplifying the fractions (i.e. '2\pi/6' instead of '\pi/3' etc).
  • The 1.13 in the first line (originally 1.1) is fine-tuned to match the length of the strings. You may need to change that value is you use different strings

Here's an example result (I've called the modified function polar_rad.m):

theta = 0:.01:2*pi;
rho = sin(theta).^2;
polar_rad(theta, rho)



来源:https://stackoverflow.com/questions/28853280/change-axis-in-polar-plots-in-matlab-to-radians

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!