Specifying the order of matplotlib layers

前端 未结 3 1318
感情败类
感情败类 2020-12-01 05:59

Suppose I run the following script:

import matplotlib.pyplot as plt

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


        
3条回答
  •  隐瞒了意图╮
    2020-12-01 07:01

    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.

    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()
    

    , which results in this:

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

提交回复
热议问题