指定 matplotlib 层的顺序 [英] Specifying the order of matplotlib layers

查看:45
本文介绍了指定 matplotlib 层的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我运行以下脚本:

import matplotlib.pyplot as plt

lineWidth = 20
plt.figure()
plt.plot([0,0],[-1,1], lw=lineWidth, c='b')
plt.plot([-1,1],[-1,1], lw=lineWidth, c='r')
plt.plot([-1,1],[1,-1], lw=lineWidth, c='g')
plt.show()

这会产生以下结果:

如何指定层从上到下的顺序而不是让 Python 为我选择?

How can I specify the top-to-bottom order of the layers instead of having Python pick for me?

推荐答案

我不知道为什么 zorder 有这种行为,这很可能是一个错误,或者至少是一个错误记录不当的功能.可能是因为在构建绘图(如网格、轴等...)以及尝试指定 zorder 时已经自动引用了 zorder> 对于您以某种方式重叠的元素.无论如何,这都是假设.

I don't know why zorder has that behavior and it's likely that might be a bug or, at the very least, a badly documented feature. It might be because there are already automatic references to zorder when you build a plot (like grid, axis, and so on...) and when you try to specify the zorder for elements you are somehow overlapping them. This is hypothetical in any case.

为了解决您的问题,只需将 zorder 中的差异夸大即可.例如,将 0,1,2 改为 0,5,10:

For you to solve your problem just make the differences in zorder exaggerated. For instance instead of 0,1,2, make it 0,5,10:

import matplotlib.pyplot as plt

lineWidth = 20
plt.figure()
plt.plot([0,0],[-1,1], lw=lineWidth, c='b',zorder=10)
plt.plot([-1,1],[-1,1], lw=lineWidth, c='r',zorder=5)
plt.plot([-1,1],[1,-1], lw=lineWidth, c='g',zorder=0)
plt.show()

,结果如下:

对于这个情节,我指定了您问题中显示的相反顺序.

For this plot I specified the opposite order shown in your question.

这篇关于指定 matplotlib 层的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆