当每个点都有特定的半径大小时,如何为每个点散点图绘制一个圆 [英] How to plot a circle for each point scatter plot while each has particular radius size

查看:200
本文介绍了当每个点都有特定的半径大小时,如何为每个点散点图绘制一个圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有距离矩阵的熊猫镜架,我使用PCA进行调暗.该距离矩阵的数据框具有每个点的标签和大小.

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 make each scattered point become a circle with a size dependent on the size from the dataframe

````
pca = PCA(n_components=2)
pca.fit(dist)
mds5 = pca.components_

fig = go.Figure()
fig.add_scatter(x = mds5[0],
       y = mds5[1],
       mode = 'markers+text',
       marker= dict(size = 8,
             color= 'blue'
            ),
text= dist.columns.values,
textposition='top right')
````

我需要使散点图看起来像此示例,但是,当我在相关答案中为每个点添加大小时,我无法使圆重叠,并且当它们重叠时,我可以放大,然后将它们不再重叠

I need to have the scatter plot looks something like this example, however, when I add the size for each point in related answers, I cant get the circles to overlap, and when they do, I can zoom in, then they dont overlap anymore

听起来很奇怪,但是我需要创建一个逻辑,即如果两个圆重叠,则半径较小的一个圆将消失,所以:

sounds strange, but I need to create a logic, that if two circles overlap, the one with smaller radius will dissapear, so:

  1. 无论缩放如何如何保持圆形大小不变
  2. 如何在python中创建逻辑以消除较小的重叠圆?

推荐答案

我仍然不确定要在圆环尺寸中反映哪个PCA参数,但是:要么想要

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

  • 使用散点图(即ax.scatter()),其size=反映了您选择的PCA参数;重新缩放图形时,此尺寸将(并且不应)重新缩放;它也未给出,以(x,y)单位
  • 使用多个plt.Circle((x,y), radius=radius, **kwargs)色块,其半径以(x,y)单位给出;然后,点重合在重新缩放时保持一致,但这可能会导致点变形
  • 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:

我想您想要基于plt.Circle的解决方案,因为它保持距离 static ,然后您需要手动"预先计算两个点是否重叠并手动"删除它们.您应该能够通过比较点大小(即radius,您的PCA参数)和数据点之间的欧式距离(即np.sqrt(dx**2 + dy**2))来自动执行此操作.

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

,然后使用可选参数(即x和y坐标,颜色等)进行调用:

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')

我使用fig,ax=plt.subplots()分别创建图形和子图的地方.

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

这篇关于当每个点都有特定的半径大小时,如何为每个点散点图绘制一个圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆