matlab polar2.m,[转载]如何在Matlab极坐标polar绘图上增加刻度单位

如何在Matlab极坐标polar绘图上增加刻度单位

一直以来,想在极坐标上面增加刻度,不知道该如何更改,经常是手动加入,费时费力。今天突然想起来为何不能通过修改Matlab的已知源代码,去更改呢?百度了之后,找到了以下的文章:

先不看文章,先来看Matlab默认绘制出来的效果图:

命令如下:

t = 0 : .01 : 2 * pi;

polar(t, sin(2 * t) .* cos(2 * t), '--r');

a4c26d1e5885305701be709a3d33442f.png

那么问题来了,我想在0到360上面增加度的标记,即“°”,在0.1,0.2……上面增加微米标记,即μm。那么我们查找Maltab的代码。在命令窗口敲出polar,然后双击选中,右键选择打开该命令。

a4c26d1e5885305701be709a3d33442f.png

打开后,找到第153(int2str(i*30))和161(loc)行,即为我们想要增加的度的标示部分:

a4c26d1e5885305701be709a3d33442f.png

更改为如下程序:

a4c26d1e5885305701be709a3d33442f.png

第二部分的修改,找到第136行:

a4c26d1e5885305701be709a3d33442f.png

修改为:

a4c26d1e5885305701be709a3d33442f.png

大功告成。注意,我们尽量不去修改Matlab原始的代码,所以我们这里将整个修改后的程序另存为:Mypolar.m。下面我们看一下修改后的绘图效果:

命令如下:

t = 0 : .01 : 2 * pi;

Mypolar(t, sin(2 * t) .* cos(2 * t), '--r');

a4c26d1e5885305701be709a3d33442f.png

同理,我们还可以用类似的方法修改相应代码,得到我们想要的效果。

最后,还是那句话:多看Matlab Help!多看Matlab

Help!多看Matlab

Help!

参考:

matlab极坐标图改变坐标系标注

作者

这两个命令的角度是定死的,没有提供任何接口去修改。你一定要改,就只能改这两个命令的源代码了

a4c26d1e5885305701be709a3d33442f.png打开polar.m后可以找到下面这段语句% plot spokes

th =

(1:6)*2*pi/12;

cst =

cos(th); snt = sin(th);

cs = [-cst;

cst];

sn = [-snt;

snt];

line(rmax*cs,rmax*sn,'linestyle',ls,'color',tc,'linewidth',1,...

'handlevisibility','off','parent',cax)把th改成别的值就可以绘制不同的角度了,比如改成th

= (1:4)*2*pi/8;就是45度的分割线了。当然下面还要继续改显示的度数% 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把两个30全部改为45即可。---------------------------------------------------------------------------------

t = 0:.01:2*pi;

polar(t,sin(2*t).*cos(2*t),'--r')

set(gca,'xdir','reverse')

次函数可简单的将x轴反向。。。

---------------------------------------------------------------------------------

基本上,

若是使用polar画图好像就无法更改其相关属性!虽然可以使用set(gca,...)更改其相关属性,

但你会发现并没有更动! 原因为误认那些标示为'XTickLabel',

其实都不是,

切入polar.m看仔细就会发现其为text产生,

所以外部更改都无效!我试了直接更改polar.m来实现即可% draw radial circles

c82 = cos(82*pi/180);

s82 = sin(82*pi/180);

rinc = (rmax-rmin)/rticks;

ppp=(rmin+rinc):rinc:rmax; [color=red]ppp=fliplr(ppp);

iip=0;[/color]

for

i=(rmin+rinc):rinc:rmax

hhh =

plot(xunit*i,yunit*i,ls,'color',tc,'linewidth',1,...

'handlevisibility','off');[color=red] iip=iip+1;

[/color] text((i+rinc/20)*c82,(i+rinc/20)*s82,

...

[' '

num2str([color=red]ppp(iip[/color][color=red])[/color])],'verticalalignment','bottom',...

'handlevisibility','off')

end

set(hhh,'linestyle','-') %

Make outer circle solid

------------------------

t=0:.01:2*pi; hh=polar77(20*t,200*sin(2*t).*cos(2*t),'--r');

-----------------------

此方法提供者应该是个高手,可惜我没有尝试成功

---------------------------------------------------------------------------------

官网上提供polarlabels.m

polarlabels([0:360]*pi/180,abs(sin([0:360]*pi/180))*10,'b',-90,10)

可实现一些简单的变化。。。似乎实现的功能不够强大

---------------------------------------------------------------------------------

有人提议polarhg.m

polarhg(theta,rho,'tdir','clockwise','rlim',[0 10], ...'rtick',[0 3

6 9],'tstep',45,'torig','up', 'color','m','linestyle',':')。这样整个图形就旋转了90度。------------------

另外,polarhg还有个问题,当运行,比如,>> theta =

0:pi/5:pi;

>> rho =

10*rand(size(theta));

>> h =

polarhg(theta,rho,'torig','down','color','m','linestyle',':');

??? Output argument "H" (and maybe others) not assigned during call

to "D:matlabworkpolarhg.m (polarhg)".

Error in ==> polarhg at 56

N = nargin;

-----------------------

解决问题:改第286行的num2str(Rmax-i)。

暂时没有下到polarhg.m文件。。。

——-----------------------------------------------------------------------------

个人推荐直接改polar.m文档,简单易实现。。。。

附:

修改后的Mypolar程序如下:

function hpol =

Mypolar(varargin)

%POLAR Polar

coordinate plot.

% POLAR(THETA, RHO) makes a plot using polar coordinates

of

% the angle THETA, in radians, versus the radius RHO.

% POLAR(THETA, RHO, S) uses the linestyle specified in string

S.

% See PLOT for a description of legal linestyles.

%

% POLAR(AX, ...) plots into AX instead of GCA.

%

% H = POLAR(...) returns a handle to the plotted object in

H.

%

% Example:

% t = 0 : .01 : 2 * pi;

% polar(t, sin(2 * t) .* cos(2 * t), '--r');

%

% See also PLOT, LOGLOG, SEMILOGX, SEMILOGY.

% Copyright 1984-2010 The MathWorks, Inc.

% $Revision: 5.22.4.11 $ $Date: 2011/03/09 06:59:01

$

% Parse possible Axes

input

[cax, args, nargs] = axescheck(varargin{:});

error(nargchk(1, 3, nargs, 'struct'));

if nargs < 1 || nargs

> 3

error('MATLAB:polar:InvalidInput',

'Requires 2 or 3 data

arguments.');

elseif

nargs ==

2

theta = args{1};

rho = args{2};

if ischar(rho)

line_style = rho;

rho = theta;

[mr, nr] = size(rho);

if

mr ==

1

theta = 1 : nr;

else

th = (1 : mr)';

theta = th(:, ones(1, nr));

end

else

line_style = 'auto';

end

elseif

nargs ==

1

theta = args{1};

line_style = 'auto';

rho = theta;

[mr, nr] = size(rho);

if mr == 1

theta = 1 : nr;

else

th = (1 : mr)';

theta = th(:, ones(1, nr));

end

else

% nargs ==

3

[theta, rho, line_style] = deal(args{1 : 3});

end

if ischar(theta) ||

ischar(rho)

error(message('MATLAB:polar:InvalidInputType'));

end

if ~isequal(size(theta),

size(rho))

error('MATLAB:polar:InvalidInput',

'THETA and RHO must be

the same size.');

end

% get hold

state

cax = newplot(cax);

next = lower(get(cax, 'NextPlot'));

hold_state = ishold(cax);

% get x-axis text

color so grid is in same color

tc = get(cax, 'XColor');

ls = get(cax, 'GridLineStyle');

% Hold on to current

Text defaults, reset them to the

% Axes' font

attributes so tick marks use them.

fAngle = get(cax, 'DefaultTextFontAngle');

fName = get(cax, 'DefaultTextFontName');

fSize = get(cax, 'DefaultTextFontSize');

fWeight = get(cax, 'DefaultTextFontWeight');

fUnits = get(cax, 'DefaultTextUnits');

set(cax, ...

'DefaultTextFontAngle',

get(cax, 'FontAngle'),

...

'DefaultTextFontName',

get(cax, 'FontName'),

...

'DefaultTextFontSize',

get(cax, 'FontSize'),

...

'DefaultTextFontWeight',

get(cax, 'FontWeight'),

...

'DefaultTextUnits',

'data');

% only do grids if

hold is off

if ~hold_state

% make a radial

grid

hold(cax, 'on');

% ensure that Inf

values don't enter into the limit calculation.

arho = abs(rho(:));

maxrho = max(arho(arho ~= Inf));

hhh = line([-maxrho, -maxrho, maxrho, maxrho], [-maxrho, maxrho,

maxrho, -maxrho], 'Parent', cax);

set(cax, 'DataAspectRatio',

[1, 1, 1], 'PlotBoxAspectRatioMode',

'auto');

v = [get(cax, 'XLim') get(cax,

'YLim')];

ticks = sum(get(cax, 'YTick') >=

0);

delete(hhh);

% check radial limits

and ticks

rmin = 0;

rmax = v(4);

rticks = max(ticks - 1, 2);

if rticks >

5 % see if we can reduce the

number

if rem(rticks, 2) ==

0

rticks = rticks / 2;

elseif

rem(rticks, 3) ==

0

rticks

= rticks / 3;

end

end

% define a

circle

th = 0 : pi / 50 : 2 * pi;

xunit = cos(th);

yunit = sin(th);

% now really force

points on x/y axes to lie on them exactly

inds = 1 : (length(th) - 1) / 4 : length(th);

xunit(inds(2 : 2 : 4)) = zeros(2, 1);

yunit(inds(1 : 2 : 5)) = zeros(3, 1);

% plot background if

necessary

if ~ischar(get(cax,

'Color'))

patch('XData', xunit *

rmax, 'YData', yunit *

rmax, ...

'EdgeColor', tc,

'FaceColor', get(cax,

'Color'),

...

'HandleVisibility',

'off', 'Parent', cax);

end

% draw radial

circles

c82 = cos(82 * pi / 180);

s82 = sin(82 * pi / 180);

rinc = (rmax - rmin) / rticks;

for

i = (rmin

+ rinc) : rinc : rmax

hhh = line(xunit * i, yunit * i, 'LineStyle', ls,

'Color', tc,

'LineWidth', 1,

...

'HandleVisibility',

'off', 'Parent', cax);

text((i + rinc / 20) * c82, (i + rinc / 20) * s82,

...

[' '

strcat(num2str(i),'mum')],

'VerticalAlignment',

'bottom', ...

'HandleVisibility',

'off', 'Parent', cax);

end

set(hhh, 'LineStyle', '-');

% Make outer circle

solid

% plot

spokes

th = (1 : 6) * 2 * pi / 12;

cst = cos(th);

snt = sin(th);

cs = [-cst; cst];

sn = [-snt; snt];

line(rmax * cs, rmax * sn, 'LineStyle', ls,

'Color', tc,

'LineWidth', 1,

...

'HandleVisibility',

'off', 'Parent', cax);

% annotate spokes in

degrees

rt = 1.1 * rmax;

for

i = 1 :

length(th)

text(rt * cst(i), rt * snt(i), strcat(int2str(i *

30),'circ'),...

'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), strcat(loc,'circ'),

'HorizontalAlignment',

'center', ...

'HandleVisibility',

'off', 'Parent', cax);

end

% set view to

2-D

view(cax, 2);

% set axis

limits

axis(cax, rmax * [-1, 1, -1.15, 1.15]);

end

% Reset

defaults.

set(cax, ...

'DefaultTextFontAngle',

fAngle , ...

'DefaultTextFontName',

fName , ...

'DefaultTextFontSize',

fSize, ...

'DefaultTextFontWeight',

fWeight, ...

'DefaultTextUnits',

fUnits );

% transform data to

Cartesian coordinates.

xx = rho .* cos(theta);

yy = rho .* sin(theta);

% plot data on top of

grid

if strcmp(line_style,

'auto')

q = plot(xx, yy, 'Parent', cax);

else

q = plot(xx, yy, line_style, 'Parent', cax);

end

if nargout ==

1

hpol = q;

end

if ~hold_state

set(cax,

'DataAspectRatio',

[1, 1, 1]), axis(cax, 'off');

set(cax, 'NextPlot', next);

end

set(get(cax, 'XLabel'),

'Visible',

'on');

set(get(cax, 'YLabel'),

'Visible',

'on');

if ~isempty(q)

&& ~isdeployed

makemcode('RegisterHandle',

cax, 'IgnoreHandle', q,

'FunctionName', 'polar');

end

end

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值