【问题标题】:How to plot in circle instead of straight line axis in Matlab?如何在 Matlab 中绘制圆形而不是直线轴?
【发布时间】:2010-02-03 08:09:51
【问题描述】:

我有一组 3 个数据集,我想在 MATLAB 中绘制,但是“x”轴,我想以圆形而不是直线的形式给出。有什么想法吗?

示例图:

在 MATLAB 中绘图的常规命令是plot(x, data1, x data2, x, data3),其中 x 轴被视为直线。我希望将 x 轴视为一个圆圈。请问有谁知道它的命令。

【问题讨论】:

标签: matlab plot polar-coordinates


【解决方案1】:

@Alok 询问你是否想要一个极坐标图。我告诉你,你确实想要一个极地情节!有关函数 polar() 及其关系,请参见 Matlab 文档,例如 cart2pol。根据您的具体要求(我没有关注您的链接),您可能会发现制作您想要的情节相对容易或相当困难。

【讨论】:

    【解决方案2】:

    下面是一个完整的例子,展示了如何将数据从线轴映射到圆。

    我展示了两种实现目标的方法:

    • 三个数据系列重叠(即全部映射到同一范围)
    • 另一种选择是将它们叠加绘制(在不同的相邻范围上)

    基本思想:如果您有一个系列D,则将点映射到半径等于数据值的圆,使用:

    theta = linspace(0, 2*pi, N);    %# divide circle by N points (length of data)
    r = data;                        %# radius
    x = r.*cos(theta);               %# x-coordinate
    y = r.*sin(theta);               %# y-coordinate
    plot(x, y, '-');
    

    选项 1

    %# some random data
    K = 3;
    N = 30;
    data = zeros(K,N);
    data(1,:) = 0.2*randn(1,N) + 1;
    data(2,:) = 0.2*randn(1,N) + 2;
    data(3,:) = 0.2*randn(1,N) + 3;
    
    center = [0 0];                        %# center (shift)
    radius = [data data(:,1)];             %# added first to last to create closed loop
    radius = normalize(radius',1)'+1;      %# normalize data to [0,1] range
    
    figure, hold on
    
    %# draw outer circle
    theta = linspace(5*pi/2, pi/2, 500)';  %# 'angles
    r = max(radius(:));                    %# radius
    x = r*cos(theta)+center(1);
    y = r*sin(theta)+center(2);
    plot(x, y, 'k:');
    
    %# draw mid-circles
    theta = linspace(5*pi/2, pi/2, 500)';  %# 'angles
    num = 5;                               %# number of circles
    rr = linspace(0,2,num+2);              %# radiuses
    for k=1:num
        r = rr(k+1);
        x = r*cos(theta)+center(1);
        y = r*sin(theta)+center(2);
        plot(x, y, 'k:');
    end
    
    %# draw labels
    theta = linspace(5*pi/2, pi/2, N+1)';    %# 'angles
    theta(end) = [];
    r = max(radius(:));
    r = r + r*0.2;                           %# shift to outside a bit
    x = r*cos(theta)+center(1);
    y = r*sin(theta)+center(2);
    str = strcat(num2str((1:N)','%d'),{});   %# 'labels
    text(x, y, str, 'FontWeight','Bold');
    
    %# draw the actual series
    theta = linspace(5*pi/2, pi/2, N+1);
    x = bsxfun(@times, radius, cos(theta)+center(1))';
    y = bsxfun(@times, radius, sin(theta)+center(2))';
    h = zeros(1,K);
    clr = hsv(K);
    for k=1:K
        h(k) = plot(x(:,k), y(:,k), '.-', 'Color', clr(k,:), 'LineWidth', 2);
    end
    
    %# legend and fix axes
    legend(h, {'M1' 'M2' 'M3'}, 'location', 'SouthOutside', 'orientation','horizontal')
    hold off
    axis equal, axis([-1 1 -1 1] * r), axis off
    

    选项 2

    %# some random data
    K = 3;
    N = 30;
    data = zeros(K,N);
    data(1,:) = 0.2*randn(1,N) + 1;
    data(2,:) = 0.2*randn(1,N) + 2;
    data(3,:) = 0.2*randn(1,N) + 3;
    
    center = [0 0];                                %# center (shift)
    radius = [data data(:,1)];                     %# added first to last to create closed loop
    radius = normalize(radius',1)';                %# normalize data to [0,1] range
    radius = bsxfun( @plus, radius, (1:2:2*K)' );  %# 'make serieson seperate ranges by addition
    
    figure, hold on
    
    %# draw outer circle
    theta = linspace(5*pi/2, pi/2, 500)';   %# 'angles
    r = max(radius(:))+1;                   %# radius
    x = r*cos(theta)+center(1);
    y = r*sin(theta)+center(2);
    plot(x, y, 'k:');
    
    %# draw mid-circles
    theta = linspace(5*pi/2, pi/2, 500)';  %# 'angles
    r = 1.5;                               %# radius
    for k=1:K
        x = r*cos(theta)+center(1);
        y = r*sin(theta)+center(2);
        plot(x, y, 'k:');
        r=r+2;             %# increment radius for next circle
    end
    
    %# draw labels
    theta = linspace(5*pi/2, pi/2, N+1)';    %# 'angles
    theta(end) = [];
    r = max(radius(:))+1;
    r = r + r*0.2;                           %# shift to outside a bit
    x = r*cos(theta)+center(1);
    y = r*sin(theta)+center(2);
    str = strcat(num2str((1:N)','%d'),{});   %# 'labels
    text(x, y, str, 'FontWeight','Bold');
    
    %# draw the actual series
    theta = linspace(5*pi/2, pi/2, N+1);
    x = bsxfun(@times, radius, cos(theta)+center(1))';
    y = bsxfun(@times, radius, sin(theta)+center(2))';
    h = zeros(1,K);
    clr = hsv(K);
    for k=1:K
        h(k) = plot(x(:,k), y(:,k), '.-', 'Color', clr(k,:), 'LineWidth', 2);
    end
    
    %# legend and fix axes
    legend(h, {'M1' 'M2' 'M3'}, 'location', 'SouthOutside', 'orientation','horizontal')
    hold off
    axis equal, axis([-1 1 -1 1] * r), axis off
    


    我应该提到normalize() 是一个自定义函数,它只是执行最小最大归一化((x-min)/(max-min)),定义为:

    function newData = normalize(data, type)
        [numInst numDim] = size(data);
        e = ones(numInst, 1);
        minimum = min(data);
        maximum = max(data);
        range = (maximum - minimum);
    
        if type == 1
            %# minmax normalization: (x-min)/(max-min)   =>   x in [0,1]
            newData = (data - e*minimum) ./ ( e*(range+(range==0)) );
        end
    
        %# (...)
    end
    

    【讨论】:

    • 你有点在重新发明轮子......polar() 可以满足你的大部分需求。
    • 我认为我的意图是制作一个与 OP 给出的非常相似的情节。此外,即使您使用 POLAR(),您仍然需要弄清楚如何正确缩放/移动数据,因为 POLAR 总是将其映射到从中心到外圆的整个范围......
    【解决方案3】:

    您可以找到here 所有可用的 MATLAB 2-D 和 3-D 绘图函数。

    【讨论】:

      【解决方案4】:

      对不起,如果这可能不是您问题的正确答案(您已经有很多了)。我最近发现了一个非常强大的工具来绘制圆圈 - CIRCOS:http://mkweb.bcgsc.ca/circos/ 看看,数字真的很惊人。它不是基于 Matlab,而是基于 Perl,而且它是免费的。也许你会发现它很有用。

      【讨论】: