Dmi*_*nov 2 python matplotlib seaborn
我正在尝试保存我使用 seaborn 生成的图像。图像是 4x4 混淆矩阵('confmat' np.array)。我了解到,当我以矢量格式保存图像时,某些查看器会出现问题,导致颜色条上出现白线,引用自 matplotlib 参考:
众所周知,某些矢量图形查看器(svg 和 pdf)会在颜色条的各个部分之间呈现白色间隙。这是由于查看器中的错误而不是 matplotlib。作为一种解决方法,可以使用重叠段渲染颜色条:
cbar = 颜色条()
cbar.solids.set_edgecolor("face")
画()
但是,我无法执行建议的操作。
这是我所做的:
import seaborn as sns
import matplotlib.pyplot as plt
cmap=plt.cm.Blues
fig, ax = plt.subplots()
ax = sns.heatmap(confmat, annot=True, cmap=cmap)
ax.set_title('title')
ax.tick_params(
axis='both', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom='off', # ticks along the bottom edge are off
top='off', # ticks along the top edge are off
labelbottom='off', # labels along the bottom edge are off
labelleft='off',
right='off')
fig.savefig('confusion_matrix.svg', format='svg')
Run Code Online (Sandbox Code Playgroud)
我试图使用
cbar = ax.colorbar()
Run Code Online (Sandbox Code Playgroud)
但是得到一个错误 AttributeError: 'AxesSubplot' object has no attribute 'colorbar'。
我搜索了解决方案并在这里找到了一些建议使用 plt.imshow() 来获取颜色条对象的问题,但我对我现在正在做的事情感到完全困惑。有人可以建议,如果可能的话,解释为什么,实现 matplotlib 文档为颜色条提供的解决方案吗?
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
x = np.random.randn(10, 10)
f, ax = plt.subplots()
sns.heatmap(x)
cbar_ax = f.axes[-1]
cbar_solids = cbar_ax.collections[0]
cbar_solids.set_edgecolor("face")
f.savefig("heatmap.svg")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4079 次 |
| 最近记录: |