matplotlib 如何更改Patch的zorder [复制]

xe55xuns  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(106)

此问题已在此处有答案

How to keep plotted line under the patch?(1个答案)
Drawing rectangle on top of data using matplotlib and patches(1个答案)
3天前关闭。
我试图用matplotlib画一些完全不透明的圆,但画出来的圆是透明的。我怎样才能画出完全不透明的蓝色圆圈?

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

输出如下:

1sbrub3j

1sbrub3j1#

这并不是说画出来的圆圈是透明的(它们实际上是不透明的)--而是它们是先画出来的,然后在它们上面画出了线。
在内部,元素是根据它们的zorder---粗略地按顺序绘制的,默认情况下,补丁(如圆圈)在线条之前绘制,线条在文本之前绘制。你可以用下面的方法来改变这一点(我所做的只是在你的add_patch中添加了一个zorder参数)。

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

这将生成以下图像.

相关问题