Python Matplotlib:双轴打印隐藏在主轴背景后面

Python Matplotlib:双轴打印隐藏在主轴背景后面,python,matplotlib,tkinter,z-order,Python,Matplotlib,Tkinter,Z Order,我正在努力显示双轴图和主轴图。以下是我正在做的: 我创建了主轴和双轴 我将主轴的zorder设置为高于双轴的值,因为我希望事件处理程序为主轴拾取事件(即,在工具栏中跟踪Y轴) 然后,我绘制了两个图形,位于双轴的面片隐藏在主轴的背景后面。(我知道这一点,因为如果我取消注释a.patch.set\u visible(False)line,我可以看到双轴图) 但是,我想保留跟踪主轨迹Y轴的能力,也保留背景,因为它们具有不同的比例。 这里有一个简单的例子: 导入matplotlib matplotli

我正在努力显示双轴图和主轴图。以下是我正在做的:

  • 我创建了主轴和双轴
  • 我将主轴的zorder设置为高于双轴的值,因为我希望事件处理程序为主轴拾取事件(即,在工具栏中跟踪Y轴)
  • 然后,我绘制了两个图形,位于双轴的面片隐藏在主轴的背景后面。(我知道这一点,因为如果我取消注释
    a.patch.set\u visible(False)
    line,我可以看到双轴图)
  • 但是,我想保留跟踪主轨迹Y轴的能力,也保留背景,因为它们具有不同的比例。 这里有一个简单的例子:


    导入matplotlib
    matplotlib.use('TkAgg')
    将numpy作为np导入
    来自numpy import arange,sin,pi
    从matplotlib.backends.backend_tkagg导入图CAVASTKAGG,导航工具栏2TKAGG
    从matplotlib.figure导入图形
    导入系统
    如果系统版本信息[0]<3:
    将Tkinter作为Tk导入
    其他:
    将tkinter作为Tk导入
    root=Tk.Tk()
    root.wm_标题(“嵌入TK”)
    f=图(figsize=(5,4),dpi=100)
    a=f.add_子批次(111)
    a、 网格(真)
    a、 设置颜色(“灰色”)
    aTwin=a.twinx()
    aTwin.grid(错误)
    #这里的问题是#每当我取消注释时,“aTwin”轴仍然隐藏。
    #将“a”轴zorder设置为更高的值,以便事件处理程序从“a”而不是从“aTwin”跟踪y轴
    a、 set_zorder(aTwin.get_zorder()+1)
    #a、 patch.set_visible(False)#取消注释以显示音量,但我失去了主轴的背景。我不能使用twinx轴的背景/网格,因为它们的比例不同
    x=np.arange(0.0,2,0.01)
    y1=abs(np.sin(2*np.pi*x))
    y2=10.2*np.sin(4*np.pi*x)
    aTwin.set_ylim(0,3*max(y1))
    a、 绘图(x,y2)
    在(x,0,y1,alpha=0.4)之间填充
    a、 设置_ylabel('介于y1和0'之间)
    #拉文加地区
    canvas=FigureCanvasTkAgg(f,master=root)
    canvas.show()
    canvas.get_tk_widget().pack(side=tk.TOP,fill=tk.BOTH,expand=1)
    toolbar=NavigationToolbar2TkAgg(画布,根)
    toolbar.update()
    canvas.\u tkcanvas.pack(side=Tk.TOP,fill=Tk.BOTH,expand=1)
    def_quit():
    root.quit()
    root.destroy()
    button=Tk.button(master=root,text='Quit',command=\u Quit)
    按钮组件(侧面=底部)
    Tk.mainloop()
    

    提前感谢您花时间阅读此问题。:)

    似乎要打开第一个轴的栅格,将其面片设置为不可见,将双轴的面片设置为可见,并将此面片设置为灰色。那么,在前面移动原始轴是没有问题的

    import numpy as np
    import matplotlib.pyplot as plt
    
    f = plt.figure(figsize=(5, 4), dpi=100)
    ax = f.add_subplot(111)
    axTwin = ax.twinx()
    
    # Turn grid of a on.
    ax.grid(True)
    axTwin.grid(False)
    
    # Set ax's patch invisible
    ax.patch.set_visible(False)
    # Set axtwin's patch visible and colorize it in grey
    axTwin.patch.set_visible(True)
    axTwin.patch.set_facecolor('grey')
    
    # move ax in front
    ax.set_zorder(axTwin.get_zorder() + 1)
    
    x = np.arange(0.0, 2, 0.01)
    y1 = abs(np.sin(2*np.pi*x))
    y2 = 10.2*np.sin(4*np.pi*x)
    
    axTwin.set_ylim(0, 3*max(y1))
    
    ax.plot(x, y2)
    axTwin.fill_between(x, 0, y1, alpha = 0.4)
    ax.set_ylabel('between y1 and 0')
    
    plt.show()
    

    非常感谢。这显然解决了问题。我很困惑,没有想到这个简单的解决办法。无论如何,有没有办法在主轴的绘图和背景之间绘制双轴?我的意思是,我的理想顺序是,从上到下:正弦绘图(ax),绘图(axTwin)之间的填充(U),主轴的网格和面片(ax),axTwin的网格和面片都是非活动的。似乎你需要在背景中设置第三个轴。嗯,我明白了。好的,再次感谢您的时间和回答:)
    import numpy as np
    import matplotlib.pyplot as plt
    
    f = plt.figure(figsize=(5, 4), dpi=100)
    ax = f.add_subplot(111)
    axTwin = ax.twinx()
    
    # Turn grid of a on.
    ax.grid(True)
    axTwin.grid(False)
    
    # Set ax's patch invisible
    ax.patch.set_visible(False)
    # Set axtwin's patch visible and colorize it in grey
    axTwin.patch.set_visible(True)
    axTwin.patch.set_facecolor('grey')
    
    # move ax in front
    ax.set_zorder(axTwin.get_zorder() + 1)
    
    x = np.arange(0.0, 2, 0.01)
    y1 = abs(np.sin(2*np.pi*x))
    y2 = 10.2*np.sin(4*np.pi*x)
    
    axTwin.set_ylim(0, 3*max(y1))
    
    ax.plot(x, y2)
    axTwin.fill_between(x, 0, y1, alpha = 0.4)
    ax.set_ylabel('between y1 and 0')
    
    plt.show()