更改图形层叠顺序的方法

huangapple go评论74阅读模式
英文:

How to change the zorder of a patch

问题

I'm trying to draw some completely opaque circles with matplotlib but the drawn circles are transparent. How can I draw completely opaque circles filled with a blue color?

from numpy import *
import matplotlib.pyplot as plt
import matplotlib.patches as pat
fig, ax = plt.subplots()
a=2
b=1.5
def cell(x,y):
    X=array([x,x+a/6,x,x+(2/6)*a,x+a/6,x+a/3,x+a/2,x+a/6,x+a/2,x+a/3,x+a/2,x+a/2,x+(4/6)*a,
             x+(5/6)*a,x+a/2,x+a/2,x+(2/3)*a,x+(5/6)*a,x+a/2])
    Y=array([y+b/4,y+b/2,y+0.75*b,y+b,y+b/2,y,y+b/4,y+b/2,y+0.75*b,y+b,y+0.75*b,y+b/4,y,y+b/2,y+b/4,y+0.75*b,y+b,y+b/2,
             y+0.75*b])
    plt.plot(X,Y,c='black')
    plt.plot([x+(2/3)*a,x+a,x+(5/6)*a,x+a,x+(2/3)*a,x+a/3,x,x],[y+b,y+0.75*b,y+b/2,y+b/4,y,y,y+b/4,y+(3/4)*b],c='black')
    plt.plot([x+a/3,x+(2/3)*a],[y+b,y+b],c='black')
    plt.plot([x+a,x+a],[y+b/4,y+0.75*b],c='black')
    plt.plot([x+a/2,x+a/2],[y+0.75*b,y+b],c='black')
    plt.plot([x+(5/6)*a,x+a],[y+b/2,y+b/2],c='black')
    plt.plot([x,x+a/6],[y+b/2,y+b/2],c='black')
    plt.plot([x+a/2,x+a/2],[y,y+b/4],c='black')
    plt.plot([x+a,x+a],[y+0.75*b,y+b],c='black')
    plt.plot([x+a,x+a],[y,y+0.25*b],c='black')
    plt.plot([x,x],[y,y+0.25*b],c='black')
    plt.plot([x,x],[y+0.75*b,y+b],c='black')
    ax.add_patch(plt.Circle((x,y+0.75*b),radius=0.1,fc='blue'))
    ax.add_patch(plt.Circle((x,y+0.25*b),radius=0.1,fc='blue'))
plt.arrow(-a/4,0.75*b,1.75,0,head_width=0.05,color='red')
plt.arrow(-a/4,0.75*b,0,1.75,head_width=0.05,color='red')
#C1=plt.Circle((0,0),radius=0.2,color='red')
#ax.add_patch(C1)
cell(0,0)
cell(-2,0)
cell(0,1.5)
cell(-2,1.5)
plt.text(-a/4-0.1,0.75*b-0.1,'O',color='red')
plt.text(-a/4+0.05,2.6,'$k_y$',color='red')
plt.text(0.6,1,'$k_x$',color='red')
英文:

I'm trying to draw some completely opaque circles with matplotlib but the drawn circle are transparent. How can I draw completely opaque circles filled with blue color?

from numpy import *
import matplotlib.pyplot as plt
import matplotlib.patches as pat
fig, ax = plt.subplots()
a=2
b=1.5
def cell(x,y):
X=array([x,x+a/6,x,x+(2/6)*a,x+a/6,x+a/3,x+a/2,x+a/6,x+a/2,x+a/3,x+a/2,x+a/2,x+(4/6)*a,
x+(5/6)*a,x+a/2,x+a/2,x+(2/3)*a,x+(5/6)*a,x+a/2])
Y=array([y+b/4,y+b/2,y+0.75*b,y+b,y+b/2,y,y+b/4,y+b/2,y+0.75*b,y+b,y+0.75*b,y+b/4,y,y+b/2,y+b/4,y+0.75*b,y+b,y+b/2,
y+0.75*b])
plt.plot(X,Y,c='black')
plt.plot([x+(2/3)*a,x+a,x+(5/6)*a,x+a,x+(2/3)*a,x+a/3,x,x],[y+b,y+0.75*b,y+b/2,y+b/4,y,y,y+b/4,y+(3/4)*b],c='black')
plt.plot([x+a/3,x+(2/3)*a],[y+b,y+b],c='black')
plt.plot([x+a,x+a],[y+b/4,y+0.75*b],c='black')
plt.plot([x+a/2,x+a/2],[y+0.75*b,y+b],c='black')
plt.plot([x+(5/6)*a,x+a],[y+b/2,y+b/2],c='black')
plt.plot([x,x+a/6],[y+b/2,y+b/2],c='black')
plt.plot([x+a/2,x+a/2],[y,y+b/4],c='black')
plt.plot([x+a,x+a],[y+0.75*b,y+b],c='black')
plt.plot([x+a,x+a],[y,y+0.25*b],c='black')
plt.plot([x,x],[y,y+0.25*b],c='black')
plt.plot([x,x],[y+0.75*b,y+b],c='black')
ax.add_patch(plt.Circle((x,y+0.75*b),radius=0.1,fc='blue'))
ax.add_patch(plt.Circle((x,y+0.25*b),radius=0.1,fc='blue'))
plt.arrow(-a/4,0.75*b,1.75,0,head_width=0.05,color='red')
plt.arrow(-a/4,0.75*b,0,1.75,head_width=0.05,color='red')
#C1=plt.Circle((0,0),radius=0.2,color='red')
#ax.add_patch(C1)
cell(0,0)
cell(-2,0)
cell(0,1.5)
cell(-2,1.5)
plt.text(-a/4-0.1,0.75*b-0.1,'O',color='red')
plt.text(-a/4+0.05,2.6,r'$k_y$',color='red')
plt.text(0.6,1,r'$k_x$',color='red')

the output is as below:
更改图形层叠顺序的方法

答案1

得分: 1

不是画出的圆是透明的实际上是不透明的 --- 而是它们被**绘制然后线条被绘制在它们的上面

在内部元素大致按照它们的`zorder`顺序绘制 --- 默认情况下补丁如圆形在线条之前绘制在文本之前绘制你可以通过以下方式改变这个顺序我只是在你的`add_patch`中添加了一个`zorder`参数)。

```python
from numpy import *
import matplotlib.pyplot as plt
import matplotlib.patches as pat
fig, ax = plt.subplots()
a=2
b=1.5
def cell(x,y):
    X=array([x,x+a/6,x,x+(2/6)*a,x+a/6,x+a/3,x+a/2,x+a/6,x+a/2,x+a/3,x+a/2,x+a/2,x+(4/6)*a,
             x+(5/6)*a,x+a/2,x+a/2,x+(2/3)*a,x+(5/6)*a,x+a/2])
    Y=array([y+b/4,y+b/2,y+0.75*b,y+b,y+b/2,y,y+b/4,y+b/2,y+0.75*b,y+b,y+0.75*b,y+b/4,y,y+b/2,y+b/4,y+0.75*b,y+b,y+b/2,
             y+0.75*b])
    plt.plot(X,Y,c='black')
    plt.plot([x+(2/3)*a,x+a,x+(5/6)*a,x+a,x+(2/3)*a,x+a/3,x,x],[y+b,y+0.75*b,y+b/2,y+b/4,y,y,y+b/4,y+(3/4)*b],c='black')
    plt.plot([x+a/3,x+(2/3)*a],[y+b,y+b],c='black')
    plt.plot([x+a,x+a],[y+b/4,y+0.75*b],c='black')
    plt.plot([x+a/2,x+a/2],[y+0.75*b,y+b],c='black')
    plt.plot([x+(5/6)*a,x+a],[y+b/2,y+b/2],c='black')
    plt.plot([x,x+a/6],[y+b/2,y+b/2],c='black')
    plt.plot([x+a/2,x+a/2],[y,y+b/4],c='black')
    plt.plot([x+a,x+a],[y+0.75*b,y+b],c='black')
    plt.plot([x+a,x+a],[y,y+0.25*b],c='black')
    plt.plot([x,x],[y,y+0.25*b],c='black')
    plt.plot([x,x],[y+0.75*b,y+b],c='black')
    ax.add_patch(plt.Circle((x,y+0.75*b),radius=0.1,fc='blue', zorder=5))
    ax.add_patch(plt.Circle((x,y+0.25*b),radius=0.1,fc='blue', zorder=5))
plt.arrow(-a/4,0.75*b,1.75,0,head_width=0.05,color='red')
plt.arrow(-a/4,0.75*b,0,1.75,head_width=0.05,color='red')
#C1=plt.Circle((0,0),radius=0.2,color='red')
#ax.add_patch(C1)
cell(0,0)
cell(-2,0)
cell(0,1.5)
cell(-2,1.5)
plt.text(-a/4-0.1,0.75*b-0.1,'O',color='red')
plt.text(-a/4+0.05,2.6,r'$k_y$',color='red')
plt.text(0.6,1,r'$k_x$',color='red')
英文:

It's not that the circles drawn are transparent (they're actually being drawn opaquely) --- it's that they're being drawn first and then the lines are being drawn on top of them.

Internally, elements are roughly drawn in order based on their zorder --- and by default patches (like circles) are drawn before lines, which are drawn before text. You can change this with the following (where all I did was add a zorder argument to your add_patch).

from numpy import *
import matplotlib.pyplot as plt
import matplotlib.patches as pat
fig, ax = plt.subplots()
a=2
b=1.5
def cell(x,y):
    X=array([x,x+a/6,x,x+(2/6)*a,x+a/6,x+a/3,x+a/2,x+a/6,x+a/2,x+a/3,x+a/2,x+a/2,x+(4/6)*a,
             x+(5/6)*a,x+a/2,x+a/2,x+(2/3)*a,x+(5/6)*a,x+a/2])
    Y=array([y+b/4,y+b/2,y+0.75*b,y+b,y+b/2,y,y+b/4,y+b/2,y+0.75*b,y+b,y+0.75*b,y+b/4,y,y+b/2,y+b/4,y+0.75*b,y+b,y+b/2,
             y+0.75*b])
    plt.plot(X,Y,c='black')
    plt.plot([x+(2/3)*a,x+a,x+(5/6)*a,x+a,x+(2/3)*a,x+a/3,x,x],[y+b,y+0.75*b,y+b/2,y+b/4,y,y,y+b/4,y+(3/4)*b],c='black')
    plt.plot([x+a/3,x+(2/3)*a],[y+b,y+b],c='black')
    plt.plot([x+a,x+a],[y+b/4,y+0.75*b],c='black')
    plt.plot([x+a/2,x+a/2],[y+0.75*b,y+b],c='black')
    plt.plot([x+(5/6)*a,x+a],[y+b/2,y+b/2],c='black')
    plt.plot([x,x+a/6],[y+b/2,y+b/2],c='black')
    plt.plot([x+a/2,x+a/2],[y,y+b/4],c='black')
    plt.plot([x+a,x+a],[y+0.75*b,y+b],c='black')
    plt.plot([x+a,x+a],[y,y+0.25*b],c='black')
    plt.plot([x,x],[y,y+0.25*b],c='black')
    plt.plot([x,x],[y+0.75*b,y+b],c='black')
    ax.add_patch(plt.Circle((x,y+0.75*b),radius=0.1,fc='blue', zorder=5))
    ax.add_patch(plt.Circle((x,y+0.25*b),radius=0.1,fc='blue', zorder=5))
plt.arrow(-a/4,0.75*b,1.75,0,head_width=0.05,color='red')
plt.arrow(-a/4,0.75*b,0,1.75,head_width=0.05,color='red')
#C1=plt.Circle((0,0),radius=0.2,color='red')
#ax.add_patch(C1)
cell(0,0)
cell(-2,0)
cell(0,1.5)
cell(-2,1.5)
plt.text(-a/4-0.1,0.75*b-0.1,'O',color='red')
plt.text(-a/4+0.05,2.6,r'$k_y$',color='red')
plt.text(0.6,1,r'$k_x$',color='red')

This produces the following image.更改图形层叠顺序的方法

huangapple
  • 本文由 发表于 2023年5月25日 23:38:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定