Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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 修改matplotlib和seaborn中的颜色栏_Python_Matplotlib_Seaborn - Fatal编程技术网

Python 修改matplotlib和seaborn中的颜色栏

Python 修改matplotlib和seaborn中的颜色栏,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我正在尝试保存我使用seaborn生成的图像。图像为4x4混淆矩阵(“confmat”np.数组)。我了解到,当我以矢量格式保存图像时,某些查看器出现问题,导致颜色栏上出现白线,引用matplotlib参考: 众所周知,一些矢量图形查看器(svg和pdf)渲染 颜色条各段之间的白色间隙。这是由于程序中的错误造成的 查看器不是matplotlib。作为一种解决方法,颜色栏可以 使用重叠段渲染: cbar=colorbar() cbar.solids.set_edgecolor(“面”) 画() 然

我正在尝试保存我使用seaborn生成的图像。图像为4x4混淆矩阵(“confmat”np.数组)。我了解到,当我以矢量格式保存图像时,某些查看器出现问题,导致颜色栏上出现白线,引用matplotlib参考:

众所周知,一些矢量图形查看器(svg和pdf)渲染 颜色条各段之间的白色间隙。这是由于程序中的错误造成的 查看器不是matplotlib。作为一种解决方法,颜色栏可以 使用重叠段渲染:

cbar=colorbar()

cbar.solids.set_edgecolor(“面”)

画()

然而,我很难按照建议去做

以下是我所做的:

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')
我试着用它来制作颜色条

cbar = ax.colorbar()
但获取一个错误AttributeError:“AxeSubPlot”对象没有属性“colorbar”

我搜索了解决方案,在这里发现了一些问题,建议使用plt.imshow()来获取colorbar对象,但我完全不知道我现在在做什么。
是否有人可以建议(如果可能的话)实施matplotlib文档为colorbar提供的解决方案,并解释原因?

使用
cb.solid.set\u edgecolor(“面”)更改
colorbar
正如matplotlib文档中所建议的那样,要确保颜色栏上的元素之间没有白线,似乎有点麻烦。我认为seaborn的设计假设你应该能够通过kwargs(热图中的cbar_kws)完成你需要的一切。例如,您可以将
cb_kwargs
传递给
sns.heatmap
函数
cbar_kws={“draudges”:“False”}
但不幸的是,这并不能解决问题

由于
Seaborn
Heatmap
仅返回一个轴句柄,在该句柄上绘制
heatplot
colorbar
,因此您无法直接访问源代码中的可映射对象
cbar
。因此,您无法应用此黑客

一种解决方案是只使用
pcolormesh
colorbar
绘制此图。我认为seaborn实际上重新定义了matplotlib样式,所以看起来应该是一样的

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

cmap=plt.cm.Blues

fig, ax = plt.subplots()
confmat = np.random.rand(4, 4)

cb = ax.pcolormesh(confmat, 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')

cbar = plt.colorbar(cb)
cbar.solids.set_edgecolor("face")
plt.draw()

fig.savefig('confusion_matrix.svg', format='svg')

对我来说,放大后的结果看起来是消除了白线。

更好的解决方案是,我无法从图形中获得颜色条句柄。假设第二个轴上有一个颜色条对象,但在
matplotlib
中,颜色条似乎存储为类型
QuadMesh
,这有点令人困惑。我不得不使用
cbar\u实体。设置\u alpha(无)
删除条纹<代码>设置颜色(“面部”)在我的情况下不需要,也没有帮助。
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")