我怎样才能在我的十字架上再加上四个圆形呢?

2024-03-28 21:12:08 发布

您现在位置:Python中文网/ 问答频道 /正文

到目前为止,在我的代码中,我有:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

# DATA
L = 0.1 #m, length of arm of cross
t = L/2 #m, thickness

# VECTORS OF COORDINATES OF THE CROSS
X = np.array([L, t/2, t/2, -t/2, -t/2, -L, -L, -t/2, -t/2, t/2, t/2, L, L])
Y = np.array([t/2, t/2, L, L, t/2, t/2, -t/2, -t/2, -L, -L, -t/2, -t/2, t/2])

# PLOT
fig, ax = plt.subplots(figsize=(8,8))
ax.plot(X,Y,color='C1', linewidth=2.5)
ax.axis('equal')

circle2 = plt.Circle((L - t/2, 0), t/4, color='b', fill=False)
ax.add_patch(circle2)

我希望在十字架上再加上四个相同的圆圈,一个在两端,一个在中间。我可以用python来做这个吗? 我尝试使用相同的圆2代码,不同的名称,如circle3、circle4等,并更改坐标,但它说找不到圆的名称

enter image description here


2条回答
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

# DATA
L = 0.1 #m, length of arm of cross
t = L/2 #m, thickness

# VECTORS OF COORDINATES OF THE CROSS
X = np.array([L, t/2, t/2, -t/2, -t/2, -L, -L, -t/2, -t/2, t/2, t/2, L, L])
Y = np.array([t/2, t/2, L, L, t/2, t/2, -t/2, -t/2, -L, -L, -t/2, -t/2, t/2])

# PLOT
fig, ax = plt.subplots(figsize=(8,8))
ax.plot(X,Y,color='C1', linewidth=2.5)
ax.axis('equal')

circle1 = plt.Circle((L - t/2, 0), t/4, color='b', fill=False)
ax.add_patch(circle1)

circle2= plt.Circle((3/2*-t, 0), t/4, color='b', fill=False)
ax.add_patch(circle2)

circle3 = plt.Circle((0,L-t/2), t/4, color='b', fill=False)
ax.add_patch(circle3)

circle4= plt.Circle((0, 3/2*-t), t/4, color='b', fill=False)
ax.add_patch(circle4)

enter image description here

如果我没弄错你的问题,这似乎有效

你说的很好,也许你做错了什么,打字错误。这是我试过的代码

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

# DATA
L = 0.1 #m, length of arm of cross
t = L/2 #m, thickness

# VECTORS OF COORDINATES OF THE CROSS
X = np.array([L, t/2, t/2, -t/2, -t/2, -L, -L, -t/2, -t/2, t/2, t/2, L, L])
Y = np.array([t/2, t/2, L, L, t/2, t/2, -t/2, -t/2, -L, -L, -t/2, -t/2, t/2])

# PLOT
fig, ax = plt.subplots(figsize=(8,8))
ax.plot(X,Y,color='C1', linewidth=2.5)
ax.axis('equal')

circle2 = plt.Circle((L - t/2, 0), t/4, color='b', fill=False)
circle3 = plt.Circle((-L + t/2, 0), t/4, color='b', fill=False)
circle4 = plt.Circle((0, L-t/2), t/4, color='b', fill=False)
circle5 = plt.Circle((0, -L+t/2), t/4, color='b', fill=False)
circle6 = plt.Circle((0, 0), t/4, color='b', fill=False)

ax.add_patch(circle2)
ax.add_patch(circle3)
ax.add_patch(circle4)
ax.add_patch(circle5)
ax.add_patch(circle6)
plt.show()

output

相关问题 更多 >