【问题标题】:How to make the main axes transparent, while make the zoomed_inset_axes not transparent in matplolib如何使主轴透明,同时在 matplotlib 中使 zoomed_inset_axes 不透明
【发布时间】:2016-07-08 21:44:46
【问题描述】:

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

另外就是放大部分的位置,“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)

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

【问题讨论】:

  • 如果你能给出一个最小的独立示例供我们研究,回答这个问题会容易得多。
  • 感谢,添加示例代码^^

标签: python matplotlib zooming transparent


【解决方案1】:

就在您致电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 的默认设置。

【讨论】:

  • 可以,放大部分的位置怎么样,“loc”关键字只有1,...9,9个选项,我可以指定我喜欢的位置吗,使用坐标?
  • 啊,请阅读那个(最好将每个问题限制在 一个 特定问题上,特别是如果标题中提到了该问题;-))。查看代码,不,在仍然使用函数zoomed_inset_axes 时是不可能的。不过,您可以通过创建另一个轴、自己定位并将AnchoredZoomLocator 添加到其中来复制该行为。如果您需要更多帮助,我建议您提出一个新问题,因为这太长了,无法在评论中解释。
  • 好的,你真好!
猜你喜欢
  • 2016-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 2013-08-12
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多