【问题标题】:Matplotlib: Overplot a line above another axes panelMatplotlib:在另一个轴面板上方绘制一条线
【发布时间】:2016-09-26 08:36:13
【问题描述】:

有两个轴面板,一个小的(灰色)在一个大的(白色)中。

使用以下代码,我希望蓝线位于灰色小轴面板的顶部,因为蓝线设置为具有高得离谱的zorder。但事实证明并非如此。

更改小面板的patch的zorder没有效果。

如果我将小面板的背景设置为透明(或使其不可见),那么蓝线会显得通畅,但这种解决方案并不理想,因为可能存在 IT IS REQUIRED 的情况 保持小面板的背景不透明。

或者,如果通过实现线的zorder 仅在相同的轴内有意义,那么这种要求可能无法以简单的方式实现?

f = figure()

ax1 = f.add_axes([0.1,0.1,0.8,0.8], zorder=1)
ax2 = f.add_axes([0.3,0.2,0.5,0.4], zorder=2)
ax2.patch.set_facecolor('gray')
ax2.patch.set_zorder(-9999999)
ax1.plot([0,1], [0,1], zorder=99999999, color='blue')
ax2.plot([0,1], [0,3], zorder=-99999, color='red')

# New Edit:
# To make the problem more to the point, what if someone
# also wants the background of the big panel to be green
# (with the following command)?  See the second figure.
ax1.patch.set_facecolor('green')
# This seems to mean that the small panel really has to
# somehow "insert" into the z-space between the big panel
# and the blue line.

【问题讨论】:

    标签: python python-2.7 matplotlib data-visualization


    【解决方案1】:

    看起来(如果 zorder 与您的示例相反)然后您的第一个(较大)轴的“不透明”白色背景与第二个(较小)轴重叠,因此一种方法是简单地设置面部颜色较大的轴透明。另外一定要设置整个人物的脸色为白色……

    f.set_facecolor('white')
    
    ax2 = f.add_axes([0.3,0.2,0.5,0.4], zorder=1)
    ax2.patch.set_facecolor('gray')
    ax2.plot([0,1], [0,3], color='red')
    
    ax1 = f.add_axes([0.1,0.1,0.8,0.8], zorder=2)
    ax1.plot([0,1], [0,1], color='blue')
    ax1.patch.set_alpha(0.0)#make background transparent
    

    【讨论】:

    • 是的,这是一个非常聪明的解决方案!而且我同意你的观点,它仍然不理想,尤其是当一个挑剔的人想要将大面板的背景颜色设置为某种非白色(例如绿色)颜色时。
    • 编辑了之前的帖子。这应该让你启动并运行。
    • 是的,您的解决方案有效,但是当要求大面板为绿色时,图形的 facecolor 将使整个图形变为绿色,而不仅仅是面板。
    • 啊,确实如此!对不起。
    【解决方案2】:

    我认为我们只能妥协,所以我想出的解决方案是添加第三层轴,它是透明的,是真正划出蓝线的那个。

    当然,我们需要进一步微调第一层 (ax1) 以抑制冗余元素(例如,默认情况下 ax1 也有轴标签和刻度;它们只是隐藏在下面)。

    f = figure()
    
    ax1 = f.add_axes([0.1,0.1,0.8,0.8], zorder=0)
    ax1.patch.set_facecolor('green')
    
    ax2 = f.add_axes([0.3,0.2,0.5,0.4], zorder=1)
    ax2.patch.set_facecolor('gray')
    
    ax2.plot([0,1], [0,3], color='red')
    
    ax3 = f.add_axes([0.1,0.1,0.8,0.8], zorder=2)
    ax3.patch.set_alpha(0)
    ax3.plot([0,1], [0,1], color='blue')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      相关资源
      最近更新 更多