【问题标题】:matplotlib.image.NonUniformImage extends beyond plot region during pdf exportmatplotlib.image.NonUniformImage 在 pdf 导出期间超出绘图区域
【发布时间】:2019-07-20 21:23:45
【问题描述】:

我正在使用 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 时不会发生。

【问题讨论】:

    标签: python matplotlib imshow


    【解决方案1】:

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

    im.set_clip_path(ax.patch)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      相关资源
      最近更新 更多