matlab: scatter plots with high number of datapoints

后端 未结 4 1271
栀梦
栀梦 2020-12-03 10:59

I\'m trying to plot scatter, something like:

scatter(coor(:, 2), coor(:, 3), 1, coor(:, 4));

The problem is, that I have quite big number o

相关标签:
4条回答
  • 2020-12-03 11:28

    You can use plot, but then all points have the same color. However, you can divide the set in different subsets and plot them each with their own color:

    N = 100000;
    x = rand(N,1);
    y = rand(N,1);
    C = sin(2*x)+y;
    
    cdivs = 10;
    [~, edges] = hist(C,cdivs-1);
    edges = [-Inf edges Inf]; % to include all points
    [Nk, bink] = histc(C,edges);
    
    figure;
    hold on;
    cmap = jet(cdivs);
    for ii=1:cdivs
        idx = bink==ii;
        plot(x(idx),y(idx),'.','MarkerSize',4,'Color',cmap(ii,:));
    end
    
    colormap(cmap)
    caxis([min(C) max(C)])
    colorbar
    

    enter image description here

    which responds already a lot better than scatter(x,y,1,C) which gives about the same plot, but with higher color resolution (which is adjustable in my code above).

    0 讨论(0)
  • 2020-12-03 11:31

    If you have too many points it might make sense to thin down the data.

    Basically you could do two approaches:

    1. simple - just select - say 10% of the points randomly.

    2. discard points that would not be visible, obviously those outside your range, but also if you have that many quite a few overlap - say a point should be 3px in diameter - so a point would cover say 9px. On my machine a plot like you posted would be - say 400x400px so at most ceil(400*400/9) < 20 000 data points would be visible.


    you could also try to separate the plot into smaller chunks - like plot 1000 points, issue a drawnow then the next 1000 till you are done. So you don't have to wait in front of a blank screen.

    0 讨论(0)
  • 2020-12-03 11:32

    Yes, use plot3

    plot3(coor(:, 2), coor(:, 3), coor(:, 4), '.')
    

    This will do the same as a 3d scatter plot (the points will be small dots, you can also use 'o' or 'x' if you want)

    0 讨论(0)
  • 2020-12-03 11:35

    My experience is that the most efficient plotting command in matlab is Patch, and I have used it to emulate the functionality of scatter or scatter3 with much higher efficiency.

    If you have a list of points, use each point to define a square patch (or octagons, or whatever) of reasonable edge length for your particular data, then plot the collection of patches with a single call to patch. After the graphic object is created, you can update its color data to individually color the squares.

    You can use the same concept in 3D by building cubes or 3D crosses from your data set.

    This snippet creates 1e5 randomly placed squares, with random colors in this case and runs in a little under a second on my four year old laptop. A similar call to scatter takes 40 seconds, and returns an unwieldy figure that is hard to manipulate.

    tic
    P=rand(1e5,2);
    Edge=.01;
    X=[P(:,1)'; P(:,1)'+Edge; P(:,1)'+Edge; P(:,1)'];
    Y=[P(:,2)'; P(:,2)'; P(:,2)'+Edge; P(:,2)'+Edge];
    figure;
    h=patch(X,Y,'r');
    set(h,'facevertexcdata',rand(size(X,2),3),'facecolor','flat','edgecolor','none')
    drawnow
    toc
    
    0 讨论(0)
提交回复
热议问题