python怎么画圆

2,673次阅读
没有评论

python怎么画圆

一、使用Turtle库

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

Turtle库用于绘制线、圆、其他形状或者文本。

显示小乌龟的爬行轨迹,初始小乌龟在(0, 0),前进方向为 x 轴正方向。

turtle.circle(radius,extent,step)

·radius 是必需的,表示半径,正值时逆时针旋转;

·extent 表示度数,用于绘制圆弧;

·step 表示边数,可用于绘制正多边形;

·extent 和 step 参数可有可无。

绘制圆形

import turtle
turtle.color('red')
turtle.circle(80)
turtle.done()

运行结果:

python怎么画圆

二、使用Numpy库

# -*- coding:utf-8 -*-
#! python3
import numpy as np
import matplotlib.pyplot as plt
# ==========================================
# 圆的基本信息
# 1.圆半径
r = 2.0
# 2.圆心坐标
a, b = (0., 0.)
# ==========================================
# 方法一:参数方程
theta = np.arange(0, 2*np.pi, 0.01)
x = a + r * np.cos(theta)
y = b + r * np.sin(theta)
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x, y)
axes.axis('equal')
plt.title('www.jb51.net')
# ==========================================
# 方法二:标准方程
x = np.arange(a-r, a+r, 0.01)
y = b + np.sqrt(r**2 - (x - a)**2)
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x, y) # 上半部
axes.plot(x, -y) # 下半部
plt.axis('equal')
plt.title('www.jb51.net')
# ==========================================
plt.show()

运行效果:

方法一:

python怎么画圆

方法二:

python怎么画圆

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-07-01发表,共计1153字。
新手QQ群:570568346,欢迎进群讨论 Python51学习