Seaborn绘图:如何在jointplot中突出显示单个数据点

5

我试图在Seaborn的jointplot上只突出显示一个点,但是我没有成功。虽然能够在matplotlib上显示该点,但它并未出现在同一张图表上。请问我做错了什么?谢谢!

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd

#make some sample data
test_x = [i for i in range(10)]
test_y = [j for j in range(10)]

df = pd.DataFrame({'xs':test_x,
                  'ys':test_y})

#make Seaborn chart
g = sns.jointplot(x="xs", y="ys", data = df)


#sort the table to find the top y value
df = df.sort_values('ys', ascending = False)

#find coordinates of this point
highlight_x = df.iloc[0,0]
highlight_y = df.iloc[0,1]

#this is wrong - I want it to be in the same chart
plt.scatter(highlight_x, highlight_y, color = 'red')

plt.show()

Output from this code

1个回答

5

plt.scatter会在当前的坐标轴上进行绘图。当前的坐标轴似乎是右边的那个,而不是中间的那个。因此,最好直接指定要绘制到哪个坐标轴。可以通过g.ax_joint获取JointGrid的中心坐标轴g

g = sns.jointplot(...)
# ...
g.ax_joint.scatter(highlight_x, highlight_y, color = 'red')

哇,不错呀 - 谢谢... 差点就成功了,感觉离胜利很遥远...非常感激! - user6142489

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接