Matlab中破裂的玻璃状图

时间:2016-02-20 11:16:59

标签: matlab plot

我想制作一个如下图: enter image description here

即散点图,其中一系列重复通过线连接到质心。

我不确定我是否在某个地方看到它,或者我是否只是让字形图混乱。我不知道它叫什么,所以谷歌让我失望了 - "破解玻璃情节"我个人称之为。

在Matlab中,没有原生的单行方式来显示带有x和y错误条的散点图 - 即Excel可以。这让我觉得有更好的方法,比如这个破裂的玻璃情节。

它是否存在或我是否需要自己编写?

1 个答案:

答案 0 :(得分:1)

我不知道有任何原生函数可以做到这一点,但你可以很容易地利用line绘制符合你目的的东西,例如。

function scatterPlotToCentroid(scatterPoints)

    numScatters = size(scatterPoints,2);
    scatterX = scatterPoints(1,:);
    scatterY = scatterPoints(2,:);

    centroid = mean(scatterPoints,2);
    c1 = centroid(1);
    c2 = centroid(2);

    X = [repmat(c1,1,numScatters); scatterX];
    Y = [repmat(c2,1,numScatters); scatterY];

    hold on
    line(X,Y,'Color','k');
    scatter(scatterX, scatterY, 'r*');

end

示例电话

% example: scatter sample (two groups)
numPoints = 10;
scatterDiff = 5;
scatterPointsA = 4+scatterDiff*(rand(2,numPoints)-0.5);
scatterPointsB = 8+scatterDiff*(rand(2,numPoints)-0.5);

% for each scatter sample group, plot scatter points
% and lines to centroid
hold on, box on
scatterPlotToCentroid(scatterPointsA)
scatterPlotToCentroid(scatterPointsB)
axis([0 12 0 12])

示例图

enter image description here

下面是这个答案的初始版本,它忽略了问题的“质心”部分(错过了......),而是围绕给定的中心点生成随机散射;从后者绘制线到这些散点。

function scatterPlotAt(centerPoint, numScatters, maxScatterSideLength)

    c1 = centerPoint(1);
    c2 = centerPoint(2);

    scatterX = c1-maxScatterSideLength + ...
      randi(2*maxScatterSideLength,1,numScatters);
    scatterY = c2-maxScatterSideLength + ...
      randi(2*maxScatterSideLength,1,numScatters);
    X = [repmat(c1,1,numScatters); scatterX];
    Y = [repmat(c2,1,numScatters); scatterY];
    hold on
    line(X,Y,'Color','k');
    scatter(scatterX, scatterY, 'r*');

end

示例电话

hold on, box on
scatterPlotAt([4; 4], 6, 3)
scatterPlotAt([8; 8], 6, 3)
axis([0 12 0 12])

示例结果

enter image description here