如何使主轴透明,同时在 matplotlib 中使 zoomed_inset_axes 不透明

How to make the main axes transparent, while make the zoomed_inset_axes not transparent in matplolib

目前我画的图是全透明的,如下图,这样可以区分放大后的部分和原来的部分。

另外就是放大部分的位置,"loc"关键字只有1,...9, 9个选项,我可以指定我喜欢的位置,比如用坐标吗?

axins = zoomed_inset_axes(ax, 3, loc=5) # zoom = 6

我写了一个简单的代码你的修改目的。

from pylab import *
import re
rc('font',family='Arial')
matplotlib.rc('legend', fontsize=24)

from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
font = {'family' : 'Arial',
    'weight' : 'normal',
    'size'   : 24}
fig = figure(figsize=(8,8))
fig.set_alpha(0.0)
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
x=[0,1]
y=[0,1]
plot(x,y)
axins = zoomed_inset_axes(ax, 3, loc=5) # zoom = 6

axins.plot(x,y)


# sub region of the original image
x1, x2, y1, y2 = 0.3, 0.4,  0.3,0.4
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)

plt.xticks(visible=False)
plt.yticks(visible=False)

# draw a bbox of the region of the inset axes in the parent axes and
# connecting lines between the bbox and the inset axes area
mark_inset(ax, axins, loc1=2, loc2=3, fc="none", ec="0.5")

plt.draw()
plt.show()
fig.savefig('1.png', transparent=True)

下面是这段简单代码的情节。

就在您调用 savefig 之前,执行:

fig.patch.set_alpha(0)
ax.patch.set_alpha(0)
axins.patch.set_alpha(1)
axins.patch.set_facecolor('#909090')

这将使图形背景透明,主轴也透明,但缩放轴不透明。

然后,确保不要调用带有选项 transparent=Truesavefig,因为那样会删除所有背景。只需在该调用中设置 transparent=False,这也是 savefig.

的默认值