不改变数据形状的极坐标转换这应是原创,网上能找到的都是会改变数据形状的,比如正方形的xy图变为圆形的极坐标图。

而我的程序则是直接投影,即正方形的xy图变为正方形的数据区块投影在极坐标图上。

一共四个文件:zjfunc_pol2cart.m,zjfunc_cart2pol.m,demozj.m,polarPcolor.m。使用时放于同一个文件夹下,运行demozj.m即可得到下图。

极坐标(左)xy坐标(右)

原理

1.极坐标转xy坐标

利用interp2函数,即interp2(theta,R,v_p',theta_now,rho_now)。首先对待转换的极坐标下的矩阵进行插值重构为xy坐标矩阵,将所需xy坐标对应的theta和rho计算出来存为极坐标下的theta_now和rho_now,通过查询theta_now和rho_now在插值重构后的xy坐标矩阵中对应位置的值获得xy坐标的结果。

2.xy坐标转极坐标

同样的,利用interp2函数,即interp2(p,q,v,x,y)。首先对待转换的xy坐标下的矩阵进行插值重构为极坐标矩阵,将所需极坐标对应的x和y计算出来存为xy坐标下的x和y,通过查询x和y在插值重构后的极坐标矩阵中对应位置的值获得极坐标的结果。其中涉及对不同象限数据的分类讨论,较xy坐标转极坐标略复杂。其中涉及对不同象限数据的分类讨论,较极坐标转xy坐标略复杂。

使用方法

修改demozj.m中的v矩阵为需要转换的矩阵。

局限性

由于时间问题,目前程序只能转换正方形数据,长宽不一致的数据需要适当修改后使用喔。

代码

1.极坐标转xy坐标(zjfunc_pol2cart.m)

function v_p = zjfunc_pol2cart(v)[nx,ny] = size(v);rn = round((min(nx,ny)-1)/2*sqrt(2));tn = 360;theta = 0:.1:359;x0 = (nx-1)/2+1;y0 = (ny-1)/2+1;for i = 1:rnx(:,i) = x0 + (i)*cosd(theta);y(:,i) = y0 + (i)*sind(theta);endp = 1:nx;%flip(1:rn);q = 1:ny;%1:rn;v_p = interp2(p,q,v,x,y);end

2.xy坐标转极坐标(zjfunc_cart2pol.m)

  function [v_xy,R] = zjfunc_cart2pol(v_p)nx = round(size(v_p,2)*sqrt(2)+1);ny=nx;rn = (min(nx,ny)-1)/2;R=1:round(rn*sqrt(2));x=1:nx;x=x-rn;y=1:ny;y=y-rn;theta = 0:.1:359;for i=1:length(x)for j=1:length(y)if y(j)>=0if x(i)>=0theta_now(i,j)=round(atan(y(j)/x(i))*360/2/pi);elsetheta_now(i,j)=round(atan(y(j)/x(i))*360/2/pi)+180;endelseif x(i)>=0theta_now(i,j)=round(atan(y(j)/x(i))*360/2/pi);elsetheta_now(i,j)=round(atan(y(j)/x(i))*360/2/pi)-180;endendrho_now(i,j)=round(sqrt(x(i)^2+y(j)^2));if x(i)==0if y(j)==0theta_now(i,j)=0;rho_now(i,j)=0;elseif y(j)>0theta_now(i,j)=90;elsetheta_now(i,j)=-90;endelseif y(j)==0if x(i)>0theta_now(i,j)=0;elsetheta_now(i,j)=-180;endendendendendtheta_now=theta_now+180;theta_now(theta_now==360)=0;v_xy=interp2(theta,R,v_p',theta_now,rho_now);v_xy(isnan(v_xy))=nanmean(nanmean(v_xy));v_xy=rot90(v_xy,2);end

3.演示(demozj.m)

    load earth.matv=X(1:250,:);v_p=zjfunc_cart2pol(v);[v_xy,R]=zjfunc_pol2cart(v_p);figuretheta = 0:.1:359;polarPcolor(R,theta,v_p)figurecontourf(v_xy)

4.MATLAB自带绘图程序(polarPcolor.m)

 function [varargout] = polarPcolor(R,theta,Z,varargin)% [h,c] = polarPcolor1(R,theta,Z,varargin) is a pseudocolor plot of matrix% Z for a vector radius R and a vector angle theta.% The elements of Z specify the color in each cell of the% plot. The goal is to apply pcolor function with a polar grid, which% provides a better visualization than a cartesian grid.%%% Syntax%% [h,c] = polarPcolor(R,theta,Z)% [h,c] = polarPcolor(R,theta,Z,'Ncircles',10)% [h,c] = polarPcolor(R,theta,Z,'Nspokes',5)% [h,c] = polarPcolor(R,theta,Z,'Nspokes',5,'colBar',0)% [h,c] = polarPcolor(R,theta,Z,'Nspokes',5,'labelR','r (km)')%% INPUT% * R :%        - type: float%        - size: [1 x Nrr ] where Nrr = numel(R).%        - dimension: radial distance.% * theta :%        - type: float%        - size: [1 x Ntheta ] where Ntheta = numel(theta).%        - dimension: azimuth or elevation angle (deg).%        - N.B.: The zero is defined with respect to the North.% * Z :%        - type: float%        - size: [Ntheta x Nrr]%        - dimension: user's defined .% * varargin:%        - Ncircles: number  of circles for the grid definition.%        - autoOrigin: 'on' (the first circle of the plar grid has a radius%          equal to the lowest value of R) or 'off'.%        - Nspokes: number of spokes for the grid definition.%        - colBar: display the colorbar or not.%        - labelR: title for radial axis.%        - RtickLabel: Tick label for the radial axis.%        - colormap: Colormap for the pcolor function%        - ncolor: Number of colors in the colorbar and pcolor%        - circlesPos: position of the circles with respect to the origin%        (it overwrites Ncircles if necessary)%%% OUTPUT% h: returns a handle to a SURFACE object.% c: returns a handle to a COLORBAR object.%%% Examples% R = linspace(3,10,100);% theta = linspace(0,180,360);% Z = linspace(0,10,360)'*linspace(0,10,100);% figure% polarPcolor(R,theta,Z,'Ncircles',3)%%% Author% Etienne Cheynet, University of Stavanger, Norway. 23/10/2019% see also pcolor%%%  InputParseerp = inputParser();p.CaseSensitive = false;p.addOptional('Ncircles',5);p.addOptional('autoOrigin','on');p.addOptional('Nspokes',8);p.addOptional('labelR','');p.addOptional('RtickLabel',[]);p.addOptional('colBar',1);p.addOptional('Rscale','linear');p.addOptional('colormap','parula');p.addOptional('ncolor',[]);p.addOptional('typeRose','meteo'); % 'meteo' or 'default'p.addOptional('circlesPos',[]);p.parse(varargin{:});Ncircles = p.Results.Ncircles;Nspokes = p.Results.Nspokes ;labelR = p.Results.labelR ;RtickLabel = p.Results.RtickLabel ;colBar = p.Results.colBar ;Rscale = p.Results.Rscale ;autoOrigin = p.Results.autoOrigin ;myColorMap = p.Results.colormap ;ncolor = p.Results.ncolor ;circPos = p.Results.circlesPos ;typeRose = p.Results.typeRose ;if ~isempty(circPos)Origin = max([min(circPos),min(R)]);circPos(circPos<min(R))=[];circPos(circPos>max(R))=[];elseif strcmpi(autoOrigin,'on')Origin = min(R);elseif strcmpi(autoOrigin,'off')Origin = 0;elseerror(' ''autoOrigin'' must be ''on'' or ''of'' ')endif Origin==0 && strcmpi(Rscale,'log')warning(' The origin cannot be set to 0 if R is expressed on a logarithmic axis. The value ''Rmin'' is used instead')Origin = min(R);endif isempty(circPos)if ~isempty(RtickLabel)if numel(RtickLabel)~=Ncircleserror(' The radial ticklabel must be equal to Ncircles');endif any(cellfun(@ischar,RtickLabel)==0)error(' The radial ticklabel must be a cell array of characters');endendendif ~isempty(circPos)circPos = unique([min(R),circPos,max(R)]);end%% Preliminary checks% case where dimension is reversedNrr = numel(R);Noo = numel(theta);if isequal(size(Z),[Noo,Nrr]) && Noo~=Nrr,Z=Z';end% case where dimension of Z is not compatible with theta and Rif ~isequal(size(Z),[Nrr,Noo])fprintf('\n')fprintf([ 'Size of Z is : [',num2str(size(Z)),'] \n']);fprintf([ 'Size of R is : [',num2str(size(R)),'] \n']);fprintf([ 'Size of theta is : [',num2str(size(theta)),'] \n\n']);error(' dimension of Z does not agree with dimension of R and Theta')end%% data plotrMin = min(R);rMax = max(R);thetaMin=min(theta);thetaMax =max(theta);if strcmpi(typeRose,'meteo')theta = theta;elseif strcmpi(typeRose,'default')theta = 90-theta;elseerror('"type" must be "meteo" or "default" ');end% Definition of the meshcax = newplot;Rrange = rMax - rMin; % get the range for the radius[rNorm] = getRnorm(Rscale,Origin,R,Rrange); % getRnorm is a nested functionYY = (rNorm)'*cosd(theta);XX = (rNorm)'*sind(theta);h = pcolor(XX,YY,Z,'parent',cax);if ~isempty(ncolor)cmap = feval(myColorMap,ncolor);colormap(gca,cmap);elsecolormap(gca,myColorMap);end% disp([max(R/Rrange),max(rNorm)])shading flatset(cax,'dataaspectratio',[1 1 1]);axis off;if ~ishold(cax);% make a radial gridhold(cax,'on')% Draw circles and spokescreateSpokes(thetaMin,thetaMax,Ncircles,circPos,Nspokes);createCircles(rMin,rMax,thetaMin,thetaMax,Ncircles,circPos,Nspokes)end%% PLot colorbar if specifiedif colBar==1,c =colorbar('location','WestOutside');caxis([quantile(Z(:),0.01),quantile(Z(:),0.99)])elsec = [];end%% Outputsnargoutchk(0,2)if nargout==1,varargout{1}=h;elseif nargout==2,varargout{1}=h;varargout{2}=c;end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Nested functions%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function createSpokes(thetaMin,thetaMax,Ncircles,circlesPos,Nspokes)spokeMesh = round(linspace(thetaMin,thetaMax,Nspokes));if isempty(circlesPos)circleMesh = linspace(rMin,rMax,Ncircles);elsecircleMesh  =  circlesPos;endcontourD = abs((circleMesh - circleMesh(1))/Rrange+R(1)/Rrange);if strcmpi(typeRose,'meteo')cost = cosd(90-spokeMesh); % the zero angle is aligned with Northsint = sind(90-spokeMesh); % the zero angle is aligned with Northelseif strcmpi(typeRose,'default')cost = cosd(spokeMesh); % the zero angle is aligned with eastsint = sind(spokeMesh); % the zero angle is aligned with eastelseerror('"type" must be "meteo" or "default" ');endfor kk = 1:NspokesX = cost(kk)*contourD;Y = sint(kk)*contourD;if  Origin==0X(1)=Origin;Y(1)=Origin;endplot(X,Y,'color',[0.5,0.5,0.5],'linewidth',0.75,...'handlevisibility','off');% plot graduations of angles% avoid superimposition of 0 and 360if and(thetaMin==0,thetaMax == 360),if spokeMesh(kk)<360,text(1.05.*contourD(end).*cost(kk),...1.05.*contourD(end).*sint(kk),...[num2str(spokeMesh(kk),3),char(176)],...'horiz', 'center', 'vert', 'middle');endelsetext(1.05.*contourD(end).*cost(kk),...1.05.*contourD(end).*sint(kk),...[num2str(spokeMesh(kk),3),char(176)],...'horiz', 'center', 'vert', 'middle');endendendfunction createCircles(rMin,rMax,thetaMin,thetaMax,Ncircles,circlePos,Nspokes)if isempty(circlePos)if Origin ==0 % if the origin is set at rMincontourD = linspace(0,1+R(1)/Rrange,Ncircles);else % if the origin is automatically centered at 0contourD = linspace(0,1,Ncircles)+R(1)/Rrange;endelsecontourD = circlePos-circlePos(1);contourD = contourD./max(contourD)*max(R/Rrange);contourD =[contourD(1:end-1)./contourD(end),1]+R(1)/Rrange;endif isempty(circlePos)if strcmpi(Rscale,'linear')||strcmpi(Rscale,'lin'),tickMesh = linspace(rMin,rMax,Ncircles);elseif strcmpi(Rscale,'log')||strcmpi(Rscale,'logarithmic'),tickMesh  = logspace(log10(rMin),log10(rMax),Ncircles);elseerror('''Rscale'' must be ''log'' or ''linear'' ');endelsetickMesh  = circlePos;Ncircles = numel(tickMesh);end% define the grid in polar coordinatesif strcmpi(typeRose,'meteo')angleGrid = linspace(90-thetaMin,90-thetaMax,100);elseif strcmpi(typeRose,'default')angleGrid = linspace(thetaMin,thetaMax,100);elseerror('"type" must be "meteo" or "default" ');endxGrid = cosd(angleGrid);yGrid = sind(angleGrid);spokeMesh = linspace(thetaMin,thetaMax,Nspokes);% plot circlesfor kk=1:length(contourD)X = xGrid*contourD(kk);Y = yGrid*contourD(kk);plot(X,Y,'color',[0.5,0.5,0.5],'linewidth',1);end% radius tick labelposition = 0.51.*(spokeMesh(min(Nspokes,round(Ncircles/2)))+...spokeMesh(min(Nspokes,1+round(Ncircles/2))));if strcmpi(typeRose,'meteo'),position = 90-position; endif strcmpi(typeRose,'default') && min(90-theta)<5,position = 0; endif min(round(theta))==90 && strcmpi(typeRose,'meteo'),  position = 0; endif max(round(theta))==90 && strcmpi(typeRose,'meteo'),  position = 0; endfor kk=1:Ncirclesif isempty(RtickLabel),rtick = num2str(tickMesh(kk),2);elsertick = RtickLabel(kk);end% radial graduationst = text(contourD(kk).*cosd(position),...(contourD(kk)).*sind(position),...rtick,'verticalalignment','BaseLine',...'horizontalAlignment', 'right',...'handlevisibility','off','parent',cax);if min(round(abs(90-theta)))<5 && strcmpi(typeRose,'default'),t.Position =  t.Position - [0,0.1,0];t.Interpreter = 'latex';clear t;endif min(round(theta))==90 && strcmpi(typeRose,'meteo')t.Position =  t.Position + [0,0.02,0];t.Interpreter = 'latex';clear t;elseif max(round(theta))==90 && strcmpi(typeRose,'meteo')t.Position =  t.Position - [0,0.05,0];t.Interpreter = 'latex';clear t;end% annotate spokesif max(theta)-min(theta)>180,t = text(contourD(end).*1.3.*cosd(position),...contourD(end).*1.3.*sind(position),...[labelR],'verticalalignment','bottom',...'horizontalAlignment', 'right',...'handlevisibility','off','parent',cax);elset = text(contourD(end).*0.6.*cosd(position),...contourD(end).*0.6.*sind(position),...[labelR],'verticalalignment','bottom',...'horizontalAlignment', 'right',...'handlevisibility','off','parent',cax);endt.Interpreter = 'latex';if min(round(theta))==90 && strcmpi(typeRose,'meteo'),t.Position =  t.Position + [0,0.05,0];clear t;elseif max(round(theta))==90 && strcmpi(typeRose,'meteo'),t.Position =  t.Position + [0,0.05,0];clear t;end%                 if min(round(abs(90-theta)))<5 && strcmpi(typeRose,'default'),%                     t.Position =  t.Position - [0,0.12,0];%                     t.Interpreter = 'latex';%                     clear t;%                 endendendfunction [rNorm] = getRnorm(Rscale,Origin,R,Rrange)if strcmpi(Rscale,'linear')||strcmpi(Rscale,'lin')rNorm = R-R(1)+Origin;rNorm = (rNorm)/max(rNorm)*max(R/Rrange);elseif strcmpi(Rscale,'log')||strcmpi(Rscale,'logarithmic')if rMin<=0error(' The radial vector cannot be lower or equal to 0 if the logarithmic scale is used');endrNorm = log10(R); %normalized radius [0,1]rNorm =rNorm-rNorm(1);rNorm = (rNorm)/max(rNorm)*max(R/Rrange);elseerror('''Rscale'' must be ''log'' or ''linear'' ');endendend

MATLAB极坐标与xy坐标互相转换_不改变数据形状_极坐标变量v_p(theta,r)的平面图相关推荐

  1. 双纽线通过matlab绘图输出xy坐标,Matlab 6 (Advanced_Plotting)

    Polar Chart(极坐标图): Snip20180214_15.png 例:蜗牛线(蚌线.蚶线) theta = linspace(0, 2*pi); r = 1 + 2*cos(theta); ...

  2. qt改变tab形状_生命吗哪QT灵修 11月18日

    出门在外口罩常带 特别声明 1 督徒QT灵修 本教材仅用于本教会内部灵修使用!版权归<生命吗哪> 灵修方法 题目: 单单高举神 传讲天上奥秘事的人 今日读经 今日读经:但以理书2:24-3 ...

  3. 写一段jdbc连oracle的程序java类_并实现数据查询_一段Jdbc连Oracle的程序,并实现数据查询....

    一段Jdbc连Oracle的程序,并实现数据查询. 程序如下: package hello.ant; import java.sql.*; public class jdbc { String dbU ...

  4. 政府公开数据可视化_公开演讲如何帮助您设计更好的数据可视化

    政府公开数据可视化 What do good speeches and good data visualisation have in common? More than you may think. ...

  5. 地理坐标(经纬度)转换成投影坐标(XY坐标)

    前言:限于需求,项目中所有涉及到经纬度的字段都要转换成XY坐标,面向度娘之后发现都没有可用的,琢磨了之后在以为大佬博客中发现了宝藏 使用工具:Proj4 Proj4是一个JavaScript类库,其主 ...

  6. 经纬度转换XY坐标,并计算距离

    import pandas as pd import numpy as np from pyproj import Transformer import matplotlib.pyplot as pl ...

  7. 道路测量xy坐标表示什么_三坐标对外测量服务

    三坐标对外测量服务,针对全球最大的航空.汽车.核能和可再生能源.化工.食品.药品及化妆品.医疗器械行业和全球顶级的科学研究实验室提供了一系列的产品.技术和创新的解决方案. 三坐标对外测量服务, 由于光 ...

  8. 《地理坐标(经纬度坐标)和屏幕坐标(xy坐标)间的转换》的读后笔记

    今天读了一下<地理坐标(经纬度坐标)和屏幕坐标(xy坐标)间的转换>,觉得内容很好,特摘录和附上自己的解释. 背景 在我们的屏幕上,有一张地图,这张地图经过缩放.平移.旋转,最终地理坐标和 ...

  9. Halcon极坐标转换,图文解说,含点坐标的转换

    极坐标的用途 把圆形的图片转换为矩形,便于字符识别和关键区域的提取. 在halcon中对应的算子 (1)极坐标的展开:polar_trans_image_ext(Image : PolarTransI ...

最新文章

  1. CentOS6.2 KVM 虚拟机命令行安装配置
  2. hdu-You can Solve a Geometry Problem too
  3. 【ESSD技术解读】ESSD Auto PL规格,引领IO性能弹性新方向
  4. 基于ECI+FaaS构建游戏战斗结算服最佳实践
  5. 这份书单,给那些想学Hadoop大数据、人工智能的人
  6. mysql查询自定义数据_实现自定义查询的数据库设计及实现(一)
  7. javascript基础与编写习惯
  8. 08-可滚动Widget
  9. android 添加注释,向Android Saripaar添加自定义注释
  10. 记录自己装AMD黑苹果安装
  11. 7-3 jmu-python-凯撒密码加密算法 (10分):编写一个凯撒密码加密程序,接收用户输入的文本和密钥k,对明文中的字母a-z和字母A-Z替换为其后第k个字母。
  12. Python获取屏幕坐标,自动发送信息
  13. 微信屏蔽网址的解决办法:366API轻松实现被微信屏蔽的网址在微信内正常访问
  14. 奇数分频电路—5分频(verilog实现)
  15. FIL在十月份的ICO流通减产
  16. SitePoint播客#158:饮酒与技术
  17. 《算法笔记》3.6小节——入门模拟->字符串处理 问题 B: 首字母大写
  18. 【mcuclub】模数转换ADC0832
  19. 变量命名规范--匈牙利命名法,骆驼命名法,帕斯卡命名法
  20. “Win10 UAP 开发系列”之主题模式切换

热门文章

  1. 身份验证协议和java安全框架
  2. python变量名可以包括_python变量名有哪些
  3. 商业智能为王 云计算更需要智慧系统
  4. Yahoo.com.cn邮箱的OutLook设置
  5. Python NLTK库安装Error:Resource u*corpora/gutenberg* not found.
  6. 速腾M1激光雷达调试
  7. 数字电路(三)电路化简
  8. 对话系统的历史(聊天机器人发展)
  9. echarts实现中国地图-地图板块颜色修改
  10. Oracle EBS 11i 表结构