用python绘制一个八卦图代码
时间: 2025-02-23 22:32:53 浏览: 21
在Python中,我们可以使用matplotlib库来绘制一个八卦图,它也被称为八边形。下面是一个简单的示例代码:
import matplotlib.pyplot as plt
import math
# 设置图形大小和背景色
fig = plt.figure(figsize=(4, 4), facecolor='white')
# 定义八卦图的角度步长和边长
angle_step = 45
radius = 0.7
# 创建一个列表存储每个顶点的坐标
vertices = []
for i in range(8):
angle = (i * angle_step) % 360
x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))
vertices.append((x, y))
# 绘制八卦图
polygon = plt.Polygon(vertices, fill=True, color='blue', alpha=0.5)
plt.gca().add_patch(polygon)
# 添加中心圆
circle = plt.Circle((0, 0), radius / 2, color='red', fill=False)
plt.gca().add_artist(circle)
# 调整图形边界并显示
plt.xlim(-1, 1)
plt.ylim(-1, 1)
plt.axis('off')
plt.show()
这个代码首先创建了一个正八边形,并将其填充为蓝色。然后添加了一个红色的小圆圈代表中心。最后,设置了坐标轴范围并隐藏了网格,使得八卦图更清晰。
阅读全文
相关推荐

















