gpt4 book ai didi

python - Matplotlib 得到干净的图(去除所有装饰)

转载 作者:太空宇宙 更新时间:2023-11-04 00:48:06 25 4
gpt4 key购买 nike

作为引用,同样的问题但针对 imshow():Matplotlib plots: removing axis, legends and white spaces

由于 stackoverflow 页面背景为白色,因此在所选答案的嵌入图像中,图周围有较宽的白色边距并不明显。

@unutbu 的以下回答适用于 imshow() 但不适用于一般的 plot()。此外,aspect='normal 自 1.2 版以来已弃用。

那么如何将 plot() 保存为图像,不带任何装饰?

最佳答案

ax.set_axis_off(),或等效地,ax.axis('off') 关闭轴线和标签。要删除更多空格,您可以使用

fig.savefig('/tmp/tight.png', bbox_inches='tight', pad_inches=0.0)

to remove all whitespace up to the axis' boundaries , 使用

extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig('/tmp/extent.png', bbox_inches=extent)

这些命令将同样适用于 ax.imshow(data)ax.plot(data)


例如,

import numpy as np
import matplotlib.pyplot as plt

data = np.arange(1,10).reshape((3, 3))
fig, ax = plt.subplots()
ax.plot(data)
ax.axis('off')
# https://stackoverflow.com/a/4328608/190597 (Joe Kington)
# Save just the portion _inside_ the axis's boundaries
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig('/tmp/extent.png', bbox_inches=extent)
fig.savefig('/tmp/tight.png', bbox_inches='tight', pad_inches=0.0)

extent.png (504x392):

enter image description here

tight.png (521x414):

enter image description here

关于python - Matplotlib 得到干净的图(去除所有装饰),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38411226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com