我想在散点图中绘制距平均值的距离散布。
这是我的代码:
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中的标签名称为圆圈着色。
有人可以帮我这个吗?
答案 0 :(得分:3)
只需传递s
参数以获取点的大小,然后注释点。您可以更多地使用annotate功能。 (我只是将标签放在点的中心,但你可以让它看起来不同......)
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))