python - 使用 seaborn.kdeplot 添加框架并删除背景颜色和网格

标签 python matplotlib seaborn

我写了一个类来绘制一些数据点。我使用 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()

enter image description here

最佳答案

您可以使用 seaborn.set_style(style='white') 移除灰色背景和白色网格在代码的开头。这还将为您的情节添加一个严格的黑色边框。

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

下面是我将 seaborn.set_style 调用添加到您的代码后得到的图,我没有更改标记,因为我不知道您想更改哪些标记。

Example Plot

关于python - 使用 seaborn.kdeplot 添加框架并删除背景颜色和网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25810971/

相关文章:

python - 如何使用 Django icontains 查询搜索包含非 ASCII 字符的文本?

python - Matplotlib 无对象 'Use' 或 '__version__'

python - 使用 seaborn distplot 设置轴最大值

python - cx_Freeze 和 seaborn - ImportError : No module named 'scipy.spatial.ckdtree'

python - matplotlib 和seaborn : ValueError: Supply a 'c' kwarg or a 'color' kwarg but not both; they differ but their functionalities overlap

python - SymPy 无法求解重写积分

python - 用python解析文本并映射到字典单词

c++ - 在 C++ Qt 应用程序中嵌入 Python/Numpy/Matplotlib?

python - 如何在 python 或 R 中以编程方式将多个 db 文件转换为 csv?

python - 如何在 IPython 循环期间内联显示 ndarray 图像?