matplotlib.image.ununiformimage文件在pdf exp期间扩展到绘图区域之外

2024-04-20 03:10:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用matplotlib.image.ununiformimage文件产生一个科学的情节。不幸的是,这个数字在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而不是ununiformimage时也不会发生。你知道吗


Tags: andimageimportpdfplotmatplotlibnpfig
1条回答
网友
1楼 · 发布于 2024-04-20 03:10:57

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

im.set_clip_path(ax.patch)

相关问题 更多 >