简体   繁体   English

如何在散点图的顶部绘制其他点?

[英]How to plot additional points on the top of scatter plot?

I have panda dataframe as df with two attributes df.one (=x) and df.two (=y). 我有panda数据帧作为df,有两个属性df.one(= x)和df.two(= y)。 Now, I want to plot scatter plot for these data points. 现在,我想绘制这些数据点的散点图。 I used 我用了

ax1 = fig.add_subplot(111)
ax1.scatter(df.one,df.two,c = 'g',marker = 'o',alpha = 0.2)

Now, I want to plot centroid of the data points give by C. How should I overlay centroid on the above scatter plot? 现在,我想绘制C给出的数据点的质心。我应该如何在上面的散点图上叠加质心? I tried: 我试过了:

ax1.scatter(C[:,0],C[:,1],c = 'r',marker = 'x')

But it overrides the scatter plot, I want to overlay on that. 但是它会覆盖散点图,我想覆盖它。 Is there any hold on option, similar to matlab ? 是否有任何保留选项,类似于matlab

If you need points overlaid on the original plot, use 如果您需要在原始图上覆盖点,请使用

ax.plot(x, y)

ex. 恩。

ax = plt.subplot(1, 1, 1)
ax.scatter([1, 2, 3], [1, 2, 3])
ax.plot(1.5, 1.5, "or")

在此输入图像描述

if you pass a list to x and y, multiple points can be added to the plot. 如果将列表传递给x和y,则可以将多个点添加到绘图中。 Also in case you need to add some annotation beside the point, try 如果你需要在点旁边添加一些注释,请尝试

ax.annotate("Some explanation", x, y)
from matplotlib import pyplot as plt
from statistics import *

bill = [34.00, 108.00, 64.00, 88.00, 99.00, 51.00]
tip = [ 5.00,  17.00,  11.00, 8.00,  14.00, 5.00]

bill.sort()
tip.sort()

print(mean(bill))
print(mean(tip))
plt.scatter(bill, tip)
plt.scatter([mean(bill)], [mean(tip)])
plt.show()

I wanted to plot the mean of the data too, so I used this format and got this result: 我也想绘制数据的平均值,所以我使用了这种格式并得到了这个结果:

Result of data 数据结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM