How can a circle be drawn over a Seaborn plot?

橙三吉。 提交于 2021-02-10 14:27:50

问题


I have a Seaborn Joinplot on which I want to draw an empty circle which will mark a certain diameter around the (0,0) point. Something like this:

How can it be done?


回答1:


ax_joint.plot will do the job.

import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="white", color_codes=True)

tips = sns.load_dataset("tips")

a = sns.jointplot(x="total_bill", y="tip", data=tips)
a.ax_joint.plot([15],[3],'o',ms=60,mec='r',mfc='none')




回答2:


I found the answer:

a = sns.jointplot(x=var_x, y=var_y, data=my_df)

a.ax_joint.plot([0],[0],'o',ms=60 , mec='r', mfc='none')



回答3:


There is a dirty way to do it: generate the circle from equation and then plot that. I'm sure there are more sophisticated solutions, but I couldn't figure it out yet. This is by modifying the data of sns.JointGrid.

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

sns.set(style="ticks")

R = 8 # radius
d = np.linspace(0,2*np.pi, 400) # some data to draw circle

def circle(d, r):
    # x and y from the equation of a circle
    return r*np.cos(d), r*np.sin(d)


rs = np.random.RandomState(11)
x = rs.gamma(2, size=1000)
y = -.5 * x + rs.normal(size=1000)

#graph your data
graph = sns.jointplot(x, y, kind="hex", color="#4CB391")

# creating the circle
a, b = circle(d, R)

#graphing it
graph.x = a
graph.y = b
graph.plot_joint(plt.plot)
plt.show()


来源:https://stackoverflow.com/questions/58791567/how-can-a-circle-be-drawn-over-a-seaborn-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!