python - matplotlib.image.NonUniformImage 在 pdf 导出期间超出绘图区域

标签 python matplotlib imshow

我正在使用 matplotlib.image.NonUniformImage 生成科学绘图。不幸的是,该图在 pdf 导出后看起来已损坏。颜色图延伸到绘图区域的轴之外。请参阅下面的示例。如何避免这个问题?

from matplotlib.image import NonUniformImage
import numpy as np

testdata = np.random.rand(100,50)

# limits of the plot in x and y
extent = [0,100,0,50]

# create figure
fig, ax = plt.subplots(figsize=(7.2,3.8))

# create color plot. 
im = NonUniformImage(ax, interpolation='bilinear', extent=extent)
im.set_data(range(100), range(50), testdata.T)
im.set_clim(0,1)

# add image to plot
ax.images.append(im)
ax.set_xlim(0, 100)
ax.set_ylim(0, 50)

# add color bar
cbar = ax.figure.colorbar(im, ax=ax)
cbar.ax.set_ylabel('counts', rotation=90, va="bottom",labelpad=20)

# crop to visible area and save
fig.subplots_adjust(left=0.08, bottom=.12, right=1, top=.93, wspace=0, hspace=0)
fig.savefig('./images/test.pdf')

结果(右下角):

enter image description here

该问题在我的 jupyter 笔记本中不可见,并且在使用 imshow 而不是 NonUniformImage 时不会发生。

最佳答案

使用ax.images.append(im) 您只需将图像附加到要绘制的图像列表中即可。为了让图像被轴剪切,您仍然需要设置其剪切路径

im.set_clip_path(ax.patch)

关于python - matplotlib.image.NonUniformImage 在 pdf 导出期间超出绘图区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57128706/

相关文章:

python - 在 apparmor 配置文件下加载 Django 应用程序的设置

c++ - cv::imshow 似乎损坏了一些内存区域

python - scipy.misc.imshow 运行时错误 ('Could not execute image view' )

python - 根据另一个键过滤字典列表以删除键中的重复项

Python矩阵索引

python - 属性错误 : 'module' object has no attribute 'TimeSeries' after python Validation. py

python - Matplotlib set_clip_path 需要绘制补丁

python - 如何确定 matplotlib 轴是否已使用 axes.axis ('off' 关闭)?

python - pandas,matplotlib,使用数据帧索引作为轴刻度标签

python - pyplot.imshow 用于矩形