How to plot a circle for each point scatter plot while each has particular radius size

前端 未结 1 822

I have a pandas frame with distance matrix, I use PCA to do the dim reduction. The the dataframe of this distance matrix has label for each point, and size.

How can I m

相关标签:
1条回答
  • 2021-01-28 09:20

    I'm still not sure which PCA parameter you want to be reflected in the circle size, but: either you want to

    • use a scatter plot (i.e. ax.scatter()) whose size= is reflecting your chosen PCA parameter; this size will (and should not) rescale when you rescale the figure; it is also not given in (x,y)-units
    • use multiple plt.Circle((x,y), radius=radius, **kwargs) patches, whose radii are given in (x,y)-units; the point overlap is then consistent on rescale, but this will likely cause deformed points

    The following animation will emphasise the issue at hand:

    I suppose you want the plt.Circle-based solution, as it keeps the distance static, and then you need to "manually" calculate beforehand whether two points overlap and delete them "manually". You should be able to do this automatically via a comparison between point size (i.e. radius, your PCA parameter) and the euclidian distance between your data points (i.e. np.sqrt(dx**2 + dy**2)).

    To use Circles, you could e.g. define a shorthand function:

    def my_circle_scatter(ax, x_array, y_array, radius=0.5, **kwargs):
        for x, y in zip(x_array, y_array):
            circle = plt.Circle((x,y), radius=radius, **kwargs)
            ax.add_patch(circle)
        return True
    

    and then call it with optional parameters (i.e. the x- and y-coordinates, colors, and so on):

    my_circle_scatter(ax, xs, ys, radius=0.2, alpha=.5, color='b')
    

    Where I've used fig,ax=plt.subplots() to create the figure and subplot individually.

    0 讨论(0)
提交回复
热议问题