2

我想在散点图中绘制与平均值距离的散点值。

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

x=[5,6,2,6,9]
y=[2,4,5,1,10]
x_mean=np.mean(x)
y_mean=np.mean(y)
x_dist_mean=x-x_mean
y_dist_mean=y-y_mean

my labels=['horse', 'cat' , 'dog', 'fish', 'ape']
plt.scatter(x_dist_mean, y_dist_mean ,alpha=0.5 )
plt.show()

但是,我希望散点图中的点与平均值的距离成正比,所以大的距离会产生一个大圆圈,而小距离会产生一个小圆圈。此外,我还想用 my_labels 中的标签名称为圆圈着色。

有人可以帮我解决这个问题吗?

4

1 回答 1

3

只需传入s点大小的参数,然后对点进行注释。您可以更多地使用注释功能。(我只是将标签放在点的中心,但你可以让它看起来不同......)

import numpy as np
import matplotlib.pyplot as plt

x=[5,6,2,6,9]
y=[2,4,5,1,10]
x_mean = np.mean(x)
y_mean = np.mean(y)
x_dist_mean = x - x_mean
y_dist_mean = y - y_mean

size = np.abs(x_dist_mean * y_dist_mean) * 100
labels=['horse', 'cat' , 'dog', 'fish', 'ape']

plt.scatter(x_dist_mean, y_dist_mean, s=size, alpha=0.5, label=labels)
for label, x, y in zip(labels, x_dist_mean, y_dist_mean):
    plt.annotate(label, xy = (x, y))
于 2013-09-16T09:57:53.063 回答