英文:
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')
答案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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论