繁体   English   中英

如何将 matplotlib.pypot 图形保存为具有所需像素大小的图像?

[英]How to save a matplotlib.pypot figure as an image with required pixel size?

我已经绘制matplotlib.pyplot plot。 我已经删除了 plot 的轴和标题,这样在 jupyter notebook 中它看起来就像一个图像。

但我需要将 plot 作为图像保存到我的本地磁盘,并具有所需的像素分辨率。 就我而言,它是 40 X 98。

我试过plt.savefig但我无法准确地得到测量值。 我在下面提供了我的代码片段。 (spectrum) 是我的二维数组,它被绘制为 x 和 y 轴的函数。

spect = 20 * np.log10(spectrum)
fig, ax = plt.subplots(figsize=(1,1))
ax = sns.heatmap(spect,cmap='viridis',cbar=False,xticklabels=False, yticklabels=False)
ax.invert_yaxis()
plt.savefig('sample.png',bbox_inches = 'tight', pad_inches = 0)

尝试调整savefig中的dpi参数。 来自文档:“dpi:以每英寸点数为单位的分辨率”。 https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html

实际像素数可能因您的屏幕分辨率而异。

有关更详细的说明,请参阅此答案: Specifying and save a figure with exact size in pixels

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM