Python Matplotlib:保存遮罩数组的绘图的eps时出现黑色方块,为什么?

Python Matplotlib:保存遮罩数组的绘图的eps时出现黑色方块,为什么?,python,matplotlib,eps,Python,Matplotlib,Eps,当我运行以下代码时: import numpy as np import matplotlib import matplotlib.pyplot as plt x_data = np.random.randn(10000) y_data = np.random.randn(10000) hist, xbins, ybins = np.histogram2d(x_data, y_data, bins=100) hist_masked = np.ma.masked_where(hist<1e

当我运行以下代码时:

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

x_data = np.random.randn(10000)
y_data = np.random.randn(10000)
hist, xbins, ybins = np.histogram2d(x_data, y_data, bins=100)
hist_masked = np.ma.masked_where(hist<1e-3, hist)

cmap = matplotlib.cm.jet
#cmap.set_bad('w',1.)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.imshow(hist_masked.T, interpolation = 'none', cmap = cmap)
plt.savefig('test.eps',transparent=False)
plt.show()
将numpy导入为np
导入matplotlib
将matplotlib.pyplot作为plt导入
x_data=np.random.randn(10000)
y_data=np.random.randn(10000)
hist,xbins,ybins=np.histogram2d(x_数据,y_数据,bins=100)

hist_masked=np.ma.masked_其中(hist掩码数组
hist_masked
将坏值设置为
np.nan
,matplotlib中的颜色映射具有预定义的nans值:

In [5]: plt.cm.jet._rgba_bad
Out[5]: (0.0, 0.0, 0.0, 0.0)
因此默认设置为黑色且完全透明。但是,当保存时设置
transparent=False
时,这些点实际上变得可见(因为alpha设置为1)

使用时:

cmap.set_bad('w',1.)
您将坏颜色设置为白色(
'w'
),因此在生成的eps文件中看不到它们


EPS文件无法处理透明度。但是,您可以考虑保存到<代码> PDF并删除标志<代码>透明度> false <代码>。这也解决了问题。

谢谢,回复。我没有意识到蒙版覆盖的值被视为NANS。但我仍然困惑,为什么它不是整个区域的。绘图变成黑色,但在边缘和个别点附近只有一些“像素”。整个区域不应该被遮罩,因此是黑色的吗?好的,你是对的。事实上,在仔细检查我得到的eps后,似乎每个非零像素的左下方都有一个黑色邻居。