欢迎访问宙启技术站
智能推送

使用Python绘制20种不同的随机形状

发布时间:2023-12-12 03:14:31

Python是一门功能强大的编程语言,可以用来进行图形绘制。在Python中,我们可以使用matplotlib库来实现图形的绘制和可视化。下面将介绍如何使用Python绘制20种不同的随机形状,并提供相应的代码例子。

1. 点(Point)

绘制点的方法是通过plt.plot()函数,并设置标记样式为.

import matplotlib.pyplot as plt
import random

x = random.randint(0, 10)
y = random.randint(0, 10)
plt.plot(x, y, marker='.', markersize=10, color='blue')
plt.show()

2. 线(Line)

绘制线的方法是通过plt.plot()函数,并设置标记样式为-

import matplotlib.pyplot as plt
import random

x = [random.randint(0, 10), random.randint(0, 10)]
y = [random.randint(0, 10), random.randint(0, 10)]
plt.plot(x, y, marker='-', markersize=10, color='blue')
plt.show()

3. 折线(Polyline)

绘制折线的方法是通过plt.plot()函数,并设置标记样式为-

import matplotlib.pyplot as plt
import random

x = [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)]
y = [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)]
plt.plot(x, y, marker='-', markersize=10, color='blue')
plt.show()

4. 矩形(Rectangle)

绘制矩形的方法是通过plt.Rectangle()函数,并添加到图形中。

import matplotlib.pyplot as plt
import random

x = random.randint(0, 10)
y = random.randint(0, 10)
width = random.randint(1, 5)
height = random.randint(1, 5)
rect = plt.Rectangle((x, y), width, height, edgecolor='blue', facecolor='none')
plt.gca().add_patch(rect)
plt.show()

5. 正方形(Square)

绘制正方形的方法是通过plt.Rectangle()函数,并添加到图形中。

import matplotlib.pyplot as plt
import random

x = random.randint(0, 10)
y = random.randint(0, 10)
length = random.randint(1, 5)
square = plt.Rectangle((x, y), length, length, edgecolor='blue', facecolor='none')
plt.gca().add_patch(square)
plt.show()

6. 圆形(Circle)

绘制圆形的方法是通过plt.Circle()函数,并添加到图形中。

import matplotlib.pyplot as plt
import random

x = random.randint(0, 10)
y = random.randint(0, 10)
radius = random.randint(1, 5)
circle = plt.Circle((x, y), radius, edgecolor='blue', facecolor='none')
plt.gca().add_patch(circle)
plt.show()

7. 椭圆(Ellipse)

绘制椭圆的方法是通过plt.Ellipse()函数,并添加到图形中。

import matplotlib.pyplot as plt
import random

x = random.randint(0, 10)
y = random.randint(0, 10)
width = random.randint(1, 5)
height = random.randint(1, 5)
ellipse = plt.Ellipse((x, y), width, height, edgecolor='blue', facecolor='none')
plt.gca().add_patch(ellipse)
plt.show()

8. 多边形(Polygon)

绘制多边形的方法是通过plt.Polygon()函数,并添加到图形中。

import matplotlib.pyplot as plt
import random

x = [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)]
y = [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)]
polygon = plt.Polygon(list(zip(x, y)), edgecolor='blue', facecolor='none')
plt.gca().add_patch(polygon)
plt.show()

9. 扇形(Sector)

绘制扇形的方法是通过plt.wedge()函数,并添加到图形中。

import matplotlib.pyplot as plt
import random

x = random.randint(0, 10)
y = random.randint(0, 10)
radius = random.randint(1, 5)
start_angle = random.randint(0, 180)
end_angle = random.randint(181, 360)
sector = plt.wedge((x, y), radius, start_angle, end_angle, edgecolor='none', facecolor='blue')
plt.gca().add_patch(sector)
plt.show()

10. 梯形(Trapezoid)

绘制梯形的方法是通过plt.Polygon()函数,并添加到图形中。

import matplotlib.pyplot as plt
import random

x = [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)]
y = [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)]
trapezoid = plt.Polygon(list(zip(x, y)), edgecolor='blue', facecolor='none')
plt.gca().add_patch(trapezoid)
plt.show()

11. 梯形(Trapezium)

绘制梯形的方法是通过plt.Polygon()函数,并添加到图形中。

import matplotlib.pyplot as plt
import random

x = [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)]
y = [random.randint(0, 10), random.randint(0, 10), random.randint(0, 10), random.randint(0, 10)]
trapezium = plt.Polygon(list(zip(x, y)), edgecolor='blue', facecolor='none')
plt.gca().add_patch(trapezium)
plt.show()

12. 弧(Arc)

绘制弧的方法是通过plt.Arc()函数,并添加到图形中。

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import random

x = random.randint(0, 10)
y = random.randint(0, 10)
width = random.randint(1, 5)
height = random.randint(1, 5)
start_angle = random.randint(0, 180)
end_angle = random.randint(181, 360)
arc = patches.Arc((x, y), width, height, angle=0, theta1=start_angle, theta2=end_angle, edgecolor='blue')
plt.gca().add_patch(arc)
plt.show()

13. 箭头(Arrow)

绘制箭头的方法是通过plt.arrow()函数,并添加到图形中。

import matplotlib.pyplot as plt
import random

x = random.randint(0, 10)
y = random.randint(0, 10)
dx = random.randint(1, 5)
dy = random.randint(1, 5)
arrow = plt.arrow(x, y, dx, dy, edgecolor='blue')
plt.gca().add_patch(arrow)
plt.show()

14. 曲线(Curve)

绘制曲线的方法是通过plt.plot()函数,并设置曲线的控制点。

import matplotlib.pyplot as plt
import random
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x) + random.uniform(-0.2, 0.2)
plt.plot(x, y, marker='.', markersize=10, color='blue')
plt.show()

15. 网格(Grid)

绘制网格的方法是通过plt.grid()函数,并设置线型和颜色。

import matplotlib.pyplot as plt
import random

x = [random.randint(0, 10), random.randint(0, 10)]
y = [random.randint(0, 10), random.randint(0, 10)]
plt.plot(x, y, marker='.', markersize=10, color='blue')
plt.grid(True, linestyle='--', linewidth=0.5, color='gray')
plt.show()

16. 文本(Text)

绘制文本的方法是通过plt.text()函数,并设置文本内容和位置。

import matplotlib.pyplot as plt
import random

x = random.randint(0, 10)
y = random.randint(0, 10)
text = plt.text(x, y, 'Random', fontsize=12, color='blue')
plt.show()

17.