我正在使用 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')
结果(右下角):
该问题在我的 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/