温馨提示×

python中circle如何使用

小亿
90
2024-01-12 11:34:26
栏目: 编程语言

在Python中,可以使用turtle模块或者matplotlib库来绘制圆形。

使用turtle模块绘制圆形的示例代码如下:

import turtle

# 创建画布和画笔
canvas = turtle.Screen()
pen = turtle.Turtle()

# 设置画笔的形状和颜色
pen.shape("turtle")
pen.color("green")

# 绘制圆形
radius = 100
pen.circle(radius)

# 关闭画布
canvas.exitonclick()

使用matplotlib库绘制圆形的示例代码如下:

import matplotlib.pyplot as plt

# 创建一个figure对象和一个坐标系对象
fig, ax = plt.subplots()

# 绘制圆形
circle = plt.Circle((0, 0), radius=1, color="green", alpha=0.5)
ax.add_patch(circle)

# 设置坐标系的范围
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)

# 显示图形
plt.show()

以上代码分别使用了turtle模块和matplotlib库来绘制圆形,你可以根据自己的需求选择其中的一种方式来绘制圆形。

0