matplotlib.image.NonUniformImage 在 pdf 导出期间超出绘图区域
matplotlib.image.NonUniformImage extends beyond plot region during pdf export
我正在使用 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 notebook 中不可见,并且在使用 imshow 而不是 NonUniformImage 时也不会出现。
使用ax.images.append(im)
您只需将图像附加到要绘制的图像列表中。为了让图像被坐标轴裁剪,你仍然需要设置它的裁剪路径
im.set_clip_path(ax.patch)
我正在使用 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 notebook 中不可见,并且在使用 imshow 而不是 NonUniformImage 时也不会出现。
使用ax.images.append(im)
您只需将图像附加到要绘制的图像列表中。为了让图像被坐标轴裁剪,你仍然需要设置它的裁剪路径
im.set_clip_path(ax.patch)