【发布时间】:2020-09-30 12:03:19
【问题描述】:
本网站上的其他答案讨论了在使用 fig.savefig() 将图形保存到磁盘时如何删除 matplotlib 图周围的“空白”边距。似乎没有人显示如何使用plt.show()显示图形而不是保存它。
如何去除下图周围的灰色(不保存到磁盘)?
import matplotlib.pyplot as plt
fig, ax = plt.subplots(facecolor='gray')
ax.set_facecolor('pink')
ax.scatter([5, 1, 3], [1, 2, 3], s=100, c='b')
ax.axis('on')
ax.margins(0)
ax.set_aspect('equal')
ax.tick_params(which="both", direction="in")
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
fig.subplots_adjust(0, 0, 1, 1)
fig.tight_layout()
plt.show()
在上面,无论我做什么,人形的灰色边缘都保留在粉红色的斧头周围。下面,我可以在保存到磁盘时将其删除:
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig('test.jpg', facecolor='gray', bbox_inches=extent)
这是我要“展示”的图像。但是我找不到在savefig 函数之外重新创建bbox_inches=extent 功能的东西。那么如何去除绘图周围的灰色,然后用plt.show 显示呢?
【问题讨论】:
标签: python matplotlib plot