【问题标题】:add frame and remove background colour and grids using seaborn.kdeplot使用 seaborn.kdeplot 添加框架并删除背景颜色和网格
【发布时间】:2014-09-12 14:45:51
【问题描述】:

我写了一个类来绘制一些数据点。我使用seaborn 制作kernel density 情节,这导致(1)框架消失了,我想要一个刚性框架,(2)情节中有网格,(3)我想要的背景颜色摆脱他们。应该怎么做?另外,我怎样才能得到散点图的星形和多边形标记?

import seaborn
import pandas
import pylab as P
import numpy as np
class PlotLocus(object):
      def __init__(self, colorX, colorY, colorpX, colorpY ,excluded_points,lcolorx1,lcolorx2,lcolory1,lcolory2,correspondence_Matrix):
          self.exarr=excluded_points #scatter points excluded by kde
          self.colorx=colorX
          self.colory=colorY
          self.colorpx=colorpX
          self.colorpy=colorpY
          r=np.arange(self.colorx.shape[0])
          self.arr=np.setxor1d(r,self.exarr)
          self.lx1=lcolorx1 
          self.lx2=lcolorx2 
          self.ly1=lcolory1 
          self.ly2=lcolory2 
          correspondence_indicies = np.where(M > 0.99)
          self.colorx_corr=self.colorx[correspondence_indicies[0]]
          self.colory_corr=self.colory[correspondence_indicies[0]]
          self.colorpx_corr=self.colorpx[correspondence_indicies[1]]
          self.colorpy_corr=self.colorpy[correspondence_indicies[1]]      
      def plot_before_colors(self):
          fig=P.figure(1, figsize=(8,8), dpi=100)
          ax = fig.add_subplot(111)
          X=np.vstack((self.colorx, self.colory)).T
          data = pandas.DataFrame(X, columns=["X", "Y"])
          seaborn.kdeplot(data.X,data.Y,bw='scott',shade=False, cmap="Purples") 
          ax.tick_params(axis='both', which='major', direction='in', length=6, width=2)
          ax.scatter(self.colorx[self.exarr], self.colory[self.exarr], s=30, c='g', marker='o', edgecolors='k',facecolors='none')
          ax.scatter(self.colorx, self.colory ,marker='.',s=15,color='b')
          ax.scatter(self.colorpx, self.colorpy, s=15, c='r', marker='d', edgecolor='r')
          for i in range(len(self.colorx_corr)):
              ax.annotate("",
                          xy=(self.colorpx_corr[i], self.colorpy_corr[i]), xycoords='data',
                          xytext=(self.colorx_corr[i], self.colory_corr[i]), textcoords='data',
                          arrowprops=dict(arrowstyle="->",
                          connectionstyle="arc3"), 
                          color='0.3'
                          )
          ax.set_xlabel("%s - %s"%(self.lx1,self.lx2), size='medium')
          ax.set_ylabel("%s - %s"%(self.ly1,self.ly2), size='medium')
          ax.set_aspect('auto')

if __name__ == "__main__":
    colorx=np.array([0.4,0.5,-0.3,1.5,0.91,0.66,0.59,-0.11,-0.08,0.12])
    colory=np.array([0.22,-1.15,0.44,0.7,-0.65,-0.21,0.8,-1.1,1.01,0.8])
    colorpx=np.array([0.48,0.45,-0.38,0.5,0.98,0.62,0.77,-0.15,-0.12,0.8])
    colorpx=np.array([0.48,0.45,-0.38,0.5,0.98,0.62,0.77,-0.15,-0.12,0.8,1.8])
    colorpx=np.array([0.48,0.45,-0.38,0.5,0.98,0.62,0.77,-0.15,-0.12,0.8,1.8,2.4])
    colorpy=np.array([0.26,-0.98,-0.1,0.66,-0.7,-0.5,0.84,-0.88,-1.2,0.9,-2.1,1.3])
    lcolorx1='u'
    lcolorx2='i'
    lcolory1='i'
    lcolory2='g'
    M=np.zeros((10,12),float)
    M[1,4]=1
    M[3,5]=1
    M[9,7]=1
    M[0,2]=1
    M[4,10]=1
    p=PlotLocus(colorx,colory,colorpx,colorpy,np.array([2,6,8]),lcolorx1,lcolorx2,lcolory1,lcolory2,M)
    p.plot_before_colors()
    P.show()

【问题讨论】:

  • 顺便说一下,您可能想使用Purples_d 颜色图而不是Purples,它会稍微限制范围,这样最低的轮廓就不会被冲掉。

标签: python matplotlib seaborn


【解决方案1】:

您可以在代码开头使用seaborn.set_style(style='white') 删除灰色背景和白色网格。这也会为您的绘图添加一个刚性黑色边框。

对于标记,您可以在scatter 调用中使用marker='*' 或使用matplotlib.markers api 获得星形。

如果我将seaborn.set_style 调用添加到您的代码中,我得到的图如下,我没有更改标记,因为我不知道您希望更改哪些标记。

【讨论】:

  • 我收到此错误:AttributeError: 'module' object has no attribute 'set_style'
  • 您的seaborn 版本是什么?我的是0.2.1
  • 很公平!也许这就是原因!
  • 是的,检查 Github 上的源代码似乎已添加 0.3。如果可以的话,我建议你升级到 0.4。
  • Ffisegydd 顺便说一句,你知道我如何改变seaborn 中等高线图的粗细吗?
【解决方案2】:

接受的答案不适用于最新的 seaborn 版本。
使用ax.set_facecolor('white') 而不是seaborn.set_style(style='white')

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2014-01-10
  • 2019-02-02
  • 2012-06-07
  • 2013-03-27
  • 2012-12-11
  • 1970-01-01
  • 2021-07-26
  • 2019-08-30
  • 2017-11-06
相关资源
最近更新 更多