Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改默认的seaborn热图打印_Python_Latex_Seaborn - Fatal编程技术网

Python 更改默认的seaborn热图打印

Python 更改默认的seaborn热图打印,python,latex,seaborn,Python,Latex,Seaborn,我已经将一个大型矩阵从Matlab导出到一个data.dat文件中,该文件以制表符分隔。我正在将此数据导入iPython脚本,以使用seaborn使用以下MWE创建矩阵的热图: import numpy as np import seaborn as sns import matplotlib.pylab as plt uniform_data = np.loadtxt("data.dat", delimiter="\t") ax = sns.heatmap(uniform_data, lin

我已经将一个大型矩阵从Matlab导出到一个
data.dat
文件中,该文件以制表符分隔。我正在将此数据导入iPython脚本,以使用
seaborn
使用以下MWE创建矩阵的热图:

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

uniform_data = np.loadtxt("data.dat", delimiter="\t")
ax = sns.heatmap(uniform_data, linewidth=0.0)
plt.show()
此代码运行良好,并输出正确的热图,生成以下输出:

如何更改此输出的样式?具体来说,我想改变颜色方案,也有乳胶形式的字体。这是因为我想将此输出导出为.pdf文件并导入到LaTeX文档中

  • 您可以使用
    sns.heatmap()
    cmap
    键控制配色方案。有关可用的颜色贴图,请参见
  • 通常,要使绘图中的所有字体看起来像latex字体,您可以这样做
  • sns.set(rc={'text.usetex':True})

    它所做的是在绘图中的每个文本对象周围添加一个
    $
    ,以允许底层的tex环境“tex-ify”它。这对于颜色条很好,但是,正如您所看到的,似乎有一个(对我来说仍然没有解决的bug)使得它不适用于标签。作为一种解决方法,您可以在所有标记标签周围手动添加
    $
    ,以允许tex解释器再次将其识别为tex

    # Collecting all xtick locations and labels and wrapping the labels in two '$'
    plt.xticks(plt.xticks()[0], ['$' + label._text + '$' for label in plt.xticks()[1]])
    
    为了展示你的例子

    import matplotlib.pyplot as plt
    import numpy as np
    import seaborn as sns
    
    # Use tex for all labels globally in the plot
    sns.set(rc={'text.usetex': True})
    
    uniform_data = np.random.rand(30, 30)
    
    # Adjust colormap with the cmap key (here 'cubehelix')
    ax = sns.heatmap(uniform_data, linewidth=0.0, cmap='cubehelix')
    
    # workaround wrap '$' around tick labels for x and y axis
    # commenting the following two lines makes only the colorbar in latex font
    plt.xticks(plt.xticks()[0], ['$' + label._text + '$' for label in plt.xticks()[1]])
    plt.yticks(plt.yticks()[0], ['$' + label._text + '$' for label in plt.yticks()[1]])
    
    plt.show()
    
    导致

  • 您可以使用
    sns.heatmap()
    cmap
    键控制配色方案。有关可用的颜色贴图,请参见
  • 通常,要使绘图中的所有字体看起来像latex字体,您可以这样做
  • sns.set(rc={'text.usetex':True})

    它所做的是在绘图中的每个文本对象周围添加一个
    $
    ,以允许底层的tex环境“tex-ify”它。这对于颜色条很好,但是,正如您所看到的,似乎有一个(对我来说仍然没有解决的bug)使得它不适用于标签。作为一种解决方法,您可以在所有标记标签周围手动添加
    $
    ,以允许tex解释器再次将其识别为tex

    # Collecting all xtick locations and labels and wrapping the labels in two '$'
    plt.xticks(plt.xticks()[0], ['$' + label._text + '$' for label in plt.xticks()[1]])
    
    为了展示你的例子

    import matplotlib.pyplot as plt
    import numpy as np
    import seaborn as sns
    
    # Use tex for all labels globally in the plot
    sns.set(rc={'text.usetex': True})
    
    uniform_data = np.random.rand(30, 30)
    
    # Adjust colormap with the cmap key (here 'cubehelix')
    ax = sns.heatmap(uniform_data, linewidth=0.0, cmap='cubehelix')
    
    # workaround wrap '$' around tick labels for x and y axis
    # commenting the following two lines makes only the colorbar in latex font
    plt.xticks(plt.xticks()[0], ['$' + label._text + '$' for label in plt.xticks()[1]])
    plt.yticks(plt.yticks()[0], ['$' + label._text + '$' for label in plt.yticks()[1]])
    
    plt.show()
    
    导致