如何解决保存 matplotlib 子图会扭曲图像中对象的比例
我一直在研究一个使用 matplotlib 子图绘制 4 个显微镜图像的图。每个图像上都包含一个比例尺,我正在裁剪它。然后,我使用 AnchoredSizeBar 根据旧比例尺的尺寸(以像素为单位)添加新的比例尺。当我使用交互式 matplotlib (jupyter) 在没有裁剪掉旧比例尺的情况下绘制图形时,旧比例尺和我添加到图像中的 AnchoredSizeBar 的像素大小相同。然而,当我保存图形时,AnchoredSizeBar 比例尺最终比旧比例尺大 10%。在保存带有裁剪掉的旧比例尺的图形之前,我想确保这两个比例尺的像素长度相同。我已经在这里研究了一段时间,但还没有解决这个问题。任何帮助表示赞赏。
代码:
fig,ax = plt.subplots(1,4,figsize = (6.5,5),dpi=200)
fig.subplots_adjust(bottom = 0)
fig.subplots_adjust(top = 1)
fig.subplots_adjust(right = 1)
fig.subplots_adjust(left = 0)
ax[0].imshow(img0,cmap=cmap,aspect='equal',vmin = np.percentile(img0,pixel_min),vmax = np.percentile(img0,pixel_max))
ax[0].axis('off')
ax[0].set_xlim(0,int(img0.shape[1]))
ax[0].set_ylim(int(img0.shape[0]),0)
scalebar0 = AnchoredSizeBar(ax[0].transData,333,'5 μm','lower left',pad=0.5,color='white',frameon=False,size_vertical=5,fontproperties=fm.FontProperties(size=6,weight='bold'))
ax[0].add_artist(scalebar0)
ax[1].imshow(img1,vmin = np.percentile(img1,vmax = np.percentile(img1,pixel_max))
ax[1].axis('off')
ax[1].set_xlim(0,int(img1.shape[1]))
ax[1].set_ylim(int(img1.shape[0]),0)
scalebar1 = AnchoredSizeBar(ax[1].transData,weight='bold'))
ax[1].add_artist(scalebar1)
ax[2].imshow(img2,vmin = np.percentile(img2,vmax = np.percentile(img2,pixel_max))
ax[2].axis('off')
ax[2].set_xlim(0,int(img2.shape[1]))
ax[2].set_ylim(int(img2.shape[0]),0)
scalebar2 = AnchoredSizeBar(ax[2].transData,323,weight='bold'))
ax[2].add_artist(scalebar2)
ax[3].imshow(img3,vmin = np.percentile(img3,vmax = np.percentile(img3,pixel_max))
ax[3].axis('off')
ax[3].set_xlim(0,int(img3.shape[1]))
ax[3].set_ylim(int(img3.shape[0]),0)
scalebar3 = AnchoredSizeBar(ax[3].transData,weight='bold'))
ax[3].add_artist(scalebar3)
plt.tight_layout()
plt.savefig(fname = (root_dir + '/' + "fig5_final.png"),dpi = 1000,bbox_inches='tight',pad_inches=0,transparent=True)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。