Python 更改默认的seaborn热图打印
我已经将一个大型矩阵从Matlab导出到一个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
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
键控制配色方案。有关可用的颜色贴图,请参见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
键控制配色方案。有关可用的颜色贴图,请参见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()
导致