首页 文章

使用幅度向量在极坐标中创建绘图

提问于
浏览
2

当我解释我在做什么时,请耐心等待一会儿 . 我有24个角度,介于0和360度之间 . 在每个角度,我都有一个值 . 我想把它描绘成极地情节的一条线 . 因此,我的最终结果应该是一个带有24个条形的极坐标图,每个条形指向角度的方向,以弧度对应 . 这可以在MATLAB或其他绘图工具中完成吗?

2 回答

  • 1

    我想你是在polarplot之后:

    ang_deg = sort(rand(1,24)*360); % some random angles
    ang_rad = ang_deg*pi/180; % convert degrees to radians for polarplot
    values = rand(1,24); % random values to plot
    polarplot(ang_rad, values);
    

    唉,这个功能是在2016a版本上引入的 .
    对于旧版本,请改用polar(感谢Thales指出这一点) .

  • 1

    您可以使用compass

    ang = deg2rad(linspace(0,360,24));% angles
    vals = 1:24; % values
    % convert the values to vector components
    U = vals.*cos(ang);
    V = vals.*sin(ang);
    % plot:
    hp = compass(U,V);
    

    你得到:

    compass1

    但是,如果你想要酒吧而不是箭头,那就更棘手了 . 从上面绘制 hp 后,您应该执行以下操作:

    % get all X and Y data from the plot:
    arrowsX = cell2mat(get(hp,'XData'));
    arrowsY = cell2mat(get(hp,'YData'));
    % delete all arrows head values:
    set(hp,{'XData'},num2cell(arrowsX(:,1:2),2));
    set(hp,{'YData'},num2cell(arrowsY(:,1:2),2));
    % make the lines look like bars:
    set(hp,{'LineWidth'},num2cell(ones(24,1)*6));
    

    compass2

    If you have Matlab R2016b 你可以使用polarhistogram

    ang = deg2rad(linspace(0,360,25));% angles
    vals = 1:24; % values
    polarhistogram('BinEdges',ang,'BinCounts',vals)
    

    但是在这里指定 BinEdges 以便将二进制位移到 ang 是一个不太直接的,需要一些操作:

    ang = rand(24,1)*2*pi; % angles
    vals = rand(24,1); % values
    % assuming your data is like 'ang' and 'vals' above:
    data = sortrows([ang vals],1); % sort the data
    % set the width of the bars by the smallest one:
    w = min(diff(sort(ang,'ascend')))*0.5; 
    % define the bins location:
    low = max(w,data(:,1)-w);
    high = min(2*pi,data(:,1)+w);
    binEdge = [low high].';
    % set zeros to all the 'spare' bins:
    counts = [data(:,2) zeros(size(data,1),1)].';
    counts = counts(:);
    % plot:
    polarhistogram('BinEdges',binEdge(:),'BinCounts',counts(1:end-1))
    

    结果(对于一些随机数据):

    compass3

相关问题