python保留子_python – 在matplotlib中保存子图

虽然@Eli是非常正确的,通常没有太多的需要这样做,这是可能的。 savefig采用一个bbox_inches参数,可以用来选择性地将图形的一部分保存到图像中。

以下是一个简单的例子:

import matplotlib.pyplot as plt

import matplotlib as mpl

import numpy as np

# Make an example plot with two subplots...

fig = plt.figure()

ax1 = fig.add_subplot(2,1,1)

ax1.plot(range(10), 'b-')

ax2 = fig.add_subplot(2,1,2)

ax2.plot(range(20), 'r^')

# Save the full figure...

fig.savefig('full_figure.png')

# Save just the portion _inside_ the second axis's boundaries

extent = ax2.get_window_extent().transformed(fig.dpi_scale_trans.inverted())

fig.savefig('ax2_figure.png', bbox_inches=extent)

# Pad the saved area by 10% in the x-direction and 20% in the y-direction

fig.savefig('ax2_figure_expanded.png', bbox_inches=extent.expanded(1.1, 1.2))

全图:

第二个子图中的区域:

第二个子图在x方向上填充10%,在y方向上填充20%

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值