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

标签 python matplotlib zooming transparent

目前,我绘制的图形是全透明的,如下所示,这使得它可以区分缩放部分和原始部分。 enter image description here

另一件事是缩放部分的位置,“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)

下面是这个简单代码的情节。 enter image description here

最佳答案

在调用 savefig 之前,执行:

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

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

然后,请确保不要使用选项 transparent=True 调用 savefig,因为这会删除所有背景。只需在该调用中设置 transparent=False,这也是 savefig 的默认设置。

关于python - 如何在matplotlib中使主轴透明,同时使zoomed_inset_axes不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36147035/

相关文章:

python - 了解 "tail -f in python"

python - 搁置:无法确定数据库类型

3D 中的 Python 频谱图(类似于 matlab 的频谱图函数)

python - 如何解决 sns.pairplot 错误 Selected KDE bandwidth is 0. Cannot estimate density”?

python - Keras:如何为每个时代改变数据

python - Imagine 模块没有 modeler 属性

python - 无法使用 Qt5Agg 后端渲染 matplotlib 图形

java - 如何在 Android 中将 Google map 的最大缩放级别设置为 30?

ios - 可缩放的 UIImageView

ios - WkWebView 不允许在加载后缩放某些网站