python 画几何图
Python语言具有很强的绘图能力,不单单可以绘制常规的统计图表,还可以绘制各种复杂的几何图形。下面我们将介绍Python中如何绘制几何图形。
# 导入绘图库 import matplotlib.pyplot as plt # 绘制正方形 def square(length): fig, ax = plt.subplots() ax.set_xlim([-length, length]) ax.set_ylim([-length, length]) plt.plot([-length, length, length, -length, -length], [-length, -length, length, length, -length]) plt.show() # 绘制三角形 def triangle(length): fig, ax = plt.subplots() ax.set_xlim([-length, length]) ax.set_ylim([-length, length]) plt.plot([-length, 0, length, -length], [0, length, 0, 0]) plt.show() # 绘制圆形 def circle(radius): fig, ax = plt.subplots() ax.set_xlim([-radius, radius]) ax.set_ylim([-radius, radius]) circle = plt.Circle((0, 0), radius=radius, fill=False) ax.add_artist(circle) plt.show() # 绘制长方形 def rectangle(width, height): fig, ax = plt.subplots() ax.set_xlim([-width, width]) ax.set_ylim([-height, height]) plt.plot([-width, width, width, -width, -width], [-height, -height, height, height, -height]) plt.show() #测试 square(5) triangle(10) circle(6) rectangle(8, 5)
在这个例子中,我们使用了Python中的matplotlib库来绘制几何图形。我们定义了四个函数来分别绘制正方形、三角形、圆形和长方形。每一个函数都使用了matplotlib的绘图工具来定义图形的大小和形状,并将其绘制出来。最后,我们使用函数来测试这些几何图形的绘制结果。
总之,Python语言是非常强大的绘图工具,在绘制几何图形方面也不例外。使用matplotlib库可以轻松地绘制各种形状的几何图形,而且代码也很简单易懂。希望这篇文章能够帮助您了解Python语言绘图的基本方法。
文章来源:丸子建站
文章标题:python 画几何图
https://www.wanzijz.com/view/57355.html