【问题标题】:How can a circle be drawn over a Seaborn plot?如何在 Seaborn 图上画一个圆?
【发布时间】:2020-03-06 13:30:00
【问题描述】:

我有一个 Seaborn Joinplot,我想在其上绘制一个空圆圈,该圆圈将标记 (0,0) 点周围的某个直径。像这样的东西:

怎么做?

【问题讨论】:

    标签: python seaborn


    【解决方案1】:

    我找到了答案:

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

    【讨论】:

    • 我们同时发布了同样的内容
    • 不知何故我们的代码看起来一模一样,即使是末尾不必要的逗号...(;
    • 确实,我按照这个 (stackoverflow.com/a/33544046/5025009) 来构建我的答案
    • 你能解释一下ms、mec和mfc是什么吗?我曾尝试寻找文档,但运气不佳... @seralouk 谢谢!
    • @ma7642 这三个都是控制 Line2D 属性的关键字参数(在 pyplot 中使用;msmarker sizemecmarker edge colormfc 是 @987654324 @.你可以在here看到所有其他kwargs的描述
    【解决方案2】:

    ax_joint.plot 将完成这项工作。

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

    【讨论】:

      【解决方案3】:

      有一种肮脏的方法可以做到这一点:从方程式生成圆,然后绘制它。我确信有更复杂的解决方案,但我还想不通。这是通过修改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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-15
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        • 2010-12-24
        • 1970-01-01
        相关资源
        最近更新 更多