Python绘制太极图
时间: 2024-05-27 14:06:51 浏览: 224
Python可以使用多个绘图库进行太极图的绘制,其中较为常用的是turtle库和matplotlib库。下面分别介绍这两种库的使用方法。
1. 使用turtle库绘制太极图
```python
import turtle
# 设置画布大小和背景色
turtle.setup(500, 500)
turtle.bgcolor('black')
# 绘制白色半圆
turtle.fillcolor('white')
turtle.begin_fill()
turtle.circle(100, 180)
turtle.end_fill()
# 绘制黑色半圆
turtle.fillcolor('black')
turtle.begin_fill()
turtle.circle(100, -180)
turtle.end_fill()
# 绘制白色小圆
turtle.penup()
turtle.goto(0, 50)
turtle.pendown()
turtle.fillcolor('white')
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
# 绘制黑色小圆
turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.fillcolor('black')
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 显示绘图窗口
turtle.done()
```
2. 使用matplotlib库绘制太极图
```python
import matplotlib.pyplot as plt
# 设置图形大小和分辨率
fig = plt.figure(figsize=(4, 4), dpi=100)
# 绘制白色半圆
theta1 = [i for i in range(0, 181)]
r1 = [100] * len(theta1)
plt.polar(theta1, r1, color='white')
# 绘制黑色半圆
theta2 = [i for i in range(180, 361)]
r2 = [100] * len(theta2)
plt.polar(theta2, r2, color='black')
# 绘制白色小圆
plt.polar(0, 50, marker='o', markersize=10, color='white')
# 绘制黑色小圆
plt.polar(0, -50, marker='o', markersize=10, color='black')
# 隐藏极轴和刻度标签
plt.axis('off')
# 显示绘图窗口
plt.show()
```
阅读全文
相关推荐
















