문제

I have some data that I want to plot in MATLAB. The data consists of x and y coordinates of the points that I want. At the moment, I am using the plot function to plot these points.

The data has a lot of points with the same coordinates. Now, what I want is that points with the same coordinates do not plot as a single dot, but a thicker dot.

For example, suppose the points are

1,1

2,1

2,1

3,2

2,1

2,1

1,1

Then, the plot should have a single dot at 3,2, but a thicker dot at 1,1 and even a thicker dot at 2,1

Can someone tell me how to do this?

도움이 되었습니까?

해결책

You can use a bit of creativity and the scatter function to do this.

First, you need to reduce your data to a list of points, plus a count of the number of points at each location.

For example if you have some data:

xy = [...
    1,1; ...
    2,1; ...
    2,1; ...
    3,2; ...
    2,1; ...
    2,1; ...
    1,1];

Get the unique points, and the unique indexes:

[xyUnique, ignore, ixs] = unique(xy,'rows')

This is pretty painful, but we can count the number of occurrences of each unique row using the ixs vector (there is probably a better way).

counts = zeros(size(xyUnique,1),1);
for ix = 1:size(counts,1);
    counts(ix) = sum(ixs == ix);
end

Now use scatter to make a plot as you want

scatter(...
    xyUnique(:,1), ...  %X values
    xyUnique(:,2), ...  %Y values
    counts*20, ...      %Individual marker sizes, note scale factor to make this visible
    'b', ...            %Marker colors
    'filled');          %I think these look better filled 

다른 팁

To avoid looping, building on the previous example, try this:

xy = [...
    1,1; ...
    2,1; ...
    2,1; ...
    3,2; ...
    2,1; ...
    2,1; ...
    1,1];

[xyUnique, ignore, ixs] = unique(xy,'rows')

Will result in

xyUnique =
     1     1
     2     1
     3     2

Next, we use the function hist

[nRows, nCols] = size(xyUnique)
xyCount = hist(ixs,nRows)

Which results in

xyCount =
     2     4     1

Each value of xyCount is number of occurrences of each row of xyUnique.

Use the scatter command of the form:

scatter(X,Y,S)

You will have to determine how many times the coordinates are repeated to set the right vector for S.

Description:

scatter(X,Y,S) draws the markers at the specified sizes (S) with a single color. This type of graph is also known as a bubble plot.

S determines the area of each marker (specified in points^2). S can be a vector the same length as X and Y or a scalar. If S is a scalar, MATLAB draws all the markers the same size. If S is empty, the default size is used.

For more information see documentation.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top