简体   繁体   English

在海洋散点图中绘制椭圆

[英]plot ellipse in a seaborn scatter plot

I have a data frame in pandas format (pd.DataFrame) with columns = [z1,z2,Digit], and I did a scatter plot in seaborn: 我有一个pandas格式的数据框(pd.DataFrame),其列= [z1,z2,Digit],并且在seaborn中做了一个散点图:

dataframe = dataFrame.apply(pd.to_numeric, errors='coerce')
sns.lmplot("z1", "z2", data=dataframe, hue='Digit', fit_reg=False, size=10)
plt.show()

隐藏的空间[![] What I want to is plot an ellipse around each of these points. 我要在每个点周围画一个椭圆。 But I can't seem to plot an ellipse in the same figure. 但我似乎无法在同一图中绘制椭圆。

I know the normal way to plot an ellipse is like: 我知道绘制椭圆的正常方法是:

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

elps = Ellipse((0, 0), 4, 2,edgecolor='b',facecolor='none')
a = plt.subplot(111, aspect='equal')
a.add_artist(elps)
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.show()

But because I have to do "a = plt.subplot(111, aspect='equal')", the plot will be on a different figure. 但是因为我必须执行“ a = plt.subplot(111,Aspect ='equal')”,所以绘图将位于不同的图形上。 And I also can't do: 而且我也做不到:

a = sns.lmplot("z1", "z2", data=rect, hue='Digit', fit_reg=False, size=10)
a.add_artist(elps)

because the 'a' returned by sns.lmplot() is of "seaborn.axisgrid.FacetGrid" object. 因为sns.lmplot()返回的“ a”属于“ seaborn.axisgrid.FacetGrid”对象。 Any solutions? 有什么办法吗? Is there anyway I can plot an ellipse without having to something like a.set_artist()? 无论如何,我不需要a.set_artist()之类的东西就能绘制椭圆?

Seaborn's lmplot() used a FacetGrid object to do the plot, and therefore your variable a = lm.lmplot(...) is a reference to that FacetGrid object. Seaborn的lmplot()使用FacetGrid对象进行绘图,因此变量a = lm.lmplot(...)是对该FacetGrid对象的引用。

To add your elipse, you need a refence to the Axes object. 要添加椭圆,您需要引用Axes对象。 The problem is that a FacetGrid can contain multiple axes depending on how you split your data. 问题是FacetGrid可以包含多个轴,具体取决于您拆分数据的方式。 Thankfully there is a function FacetGrid.facet_axis(row_i, col_j) which can return a reference to a specific Axes object. 值得庆幸的是,有一个函数FacetGrid.facet_axis(row_i, col_j)可以返回对特定Axes对象的引用。

In your case, you would do: 在您的情况下,您可以这样做:

a = sns.lmplot("z1", "z2", data=rect, hue='Digit', fit_reg=False, size=10)
ax = a.facet_axis(0,0)
ax.add_artist(elps)

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

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