缩放框的填充框,颜色为mpl_toolkits.axes_grid1.inset_locator

时间:2017-10-28 13:27:12

标签: python matplotlib plot

我用缩放区域创建了一个带有以下代码的图:

import numpy as np
import matplotlib.pyplot as plt

from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes,mark_inset


#  random  walk read

random_walk = np.loadtxt("random_walk_for_pict.txt")


if __name__=='__main__':

    fig = plt.figure(figsize = (8,16))
    ax = plt.subplot(111) #whole path
    ax.plot(random_walk)
    ax.set_xlim(0,5000)
    ax.set_ylim(-130,55)

    axins = zoomed_inset_axes(ax,2,loc='lower right')
    axins.plot(random_walk)

    x1,x2,y1,y2 = 1000,2000, -60,-15
    axins.set_xlim(x1,x2)
    axins.set_ylim(y1,y2)

    mark_inset(ax,axins,loc1=1,loc2=3)
    plt.show()

基于http://matplotlib.org/1.3.1/mpl_toolkits/axes_grid/users/overview.html#insetlocator的文档,图片为:  randomwalk

我希望盒子(左下角的缩放部分和主图上的小盒子)用浅灰色填充,

但是当我写道:

mark_inset(ax,axins,loc1=1,loc2=3, fill=True)

根据文档(https://matplotlib.org/mpl_toolkits/axes_grid/api/inset_locator_api.html)应该完成工作,我收到错误消息:

  

追踪(最近一次呼叫最后一次):

     

文件" fractal_draw.py",第39行,在mark_inset中(ax,axins,loc1 = 1,loc2 = 3,fill = True)

     

文件" /usr/local/lib/python2.7/dist-packages/mpl_toolkits/axes_grid1/inset_locator.py" ;,第582行,在mark_inset

     

pp = BboxPatch(rect,fill = False,** kwargs)

     

TypeError:type object为关键字参数' fill'

获取了多个值

我不想手动创建另一个BboxPatch,我不知道如何覆盖它" fill = False"。

如何让我的身材看起来像我想要的那样?

1 个答案:

答案 0 :(得分:2)

要填充插入轴,您可以使用axes_kwargs参数

zoomed_inset_axes(ax,2,loc='lower right', axes_kwargs={"facecolor" : "lightgray"})

通过该函数的参数确实无法提供BboxPatch的{​​{1}}。解决方法是在创建mark_inset之后设置所需的属性

BboxPatch

完整示例:

pp,p1,p2 = mark_inset(ax,axins,loc1=1,loc2=3)
pp.set_fill(True)
pp.set_facecolor("lightgray")
pp.set_edgecolor("k")

enter image description here

<小时/> 您也可以修补源代码。为此,在python安装中找到文件import numpy as np; np.random.seed(43) import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes,mark_inset # random walk random_walk = np.cumsum(np.random.randn(5000))/2-50 fig = plt.figure(figsize = (13,8)) ax = plt.subplot(111) #whole path ax.plot(random_walk) ax.set_xlim(0,5000) ax.set_ylim(-130,55) axins = zoomed_inset_axes(ax,2,loc='lower right', axes_kwargs={"facecolor" : "lightgray"}) axins.plot(random_walk) x1,x2,y1,y2 = 1000,2000, -60,-15 axins.set_xlim(x1,x2) axins.set_ylim(y1,y2) pp,p1,p2 = mark_inset(ax,axins,loc1=1,loc2=3) pp.set_fill(True) pp.set_facecolor("lightgray") pp.set_edgecolor("k") plt.show() 。在此文件中,找到该行(几乎位于文件底部)

...\lib\site-packages\mpl_toolkits\axes_grid1\inset_locator.py

并用

替换此行
pp = BboxPatch(rect, fill=False, **kwargs)

完成此操作后,您可以创建fill = kwargs.pop("fill", False) pp = BboxPatch(rect, fill=fill, **kwargs)

mark_inset