pandas 如何在散点图顶部绘制附加点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44505762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:46:40  来源:igfitidea点击:

How to plot additional points on the top of scatter plot?

pythonpandasmatplotlib

提问by talos1904

I have panda dataframe as df with two attributes df.one (=x) and df.two (=y). Now, I want to plot scatter plot for these data points. I used

我有Pandas数据框作为 df 有两个属性 df.one (=x) 和 df.two (=y)。现在,我想为这些数据点绘制散点图。我用了

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? I tried:

现在,我想绘制 C 给出的数据点的质心。我应该如何在上面的散点图上叠加质心?我试过:

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

回答by Bubble Bubble Bubble Gut

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

enter image description here

在此处输入图片说明

if you pass a list to x and y, multiple points can be added to the plot. Also in case you need to add some annotation beside the point, try

如果将列表传递给 x 和 y,则可以将多个点添加到图中。另外,如果您需要在要点旁边添加一些注释,请尝试

ax.annotate("Some explanation", x, y)

回答by Aryamaan Goswamy

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

数据结果