Visualizing Basic Shapes
Glissez pour afficher le menu
To visualize geometric shapes in Python, you will use the matplotlib library. This library is widely used for creating static, animated, and interactive visualizations. For geometric modelling, matplotlib is especially useful for plotting points, lines, and polygons on a two-dimensional plane.
The most common way to start is by importing matplotlib.pyplot as plt. You can then use commands like plt.plot() to draw lines and points, and plt.fill() to color polygons. Each shape is defined by a set of coordinates, which you pass as lists or arrays to these functions.
123456789101112131415161718192021222324252627import matplotlib.pyplot as plt # Triangle vertices triangle_x = [1, 3, 2, 1] triangle_y = [1, 1, 3, 1] # Quadrilateral vertices quad_x = [4, 6, 6, 4, 4] quad_y = [1, 1, 3, 4, 1] plt.figure(figsize=(6, 6)) # Plot triangle plt.plot(triangle_x, triangle_y, marker='o', color='blue', label='Triangle') plt.fill(triangle_x, triangle_y, color='blue', alpha=0.2) # Plot quadrilateral plt.plot(quad_x, quad_y, marker='s', color='green', label='Quadrilateral') plt.fill(quad_x, quad_y, color='green', alpha=0.2) plt.title('Triangle and Quadrilateral Visualization') plt.xlabel('X axis') plt.ylabel('Y axis') plt.legend() plt.axis('equal') plt.grid(True) plt.show()
To plot a triangle, you provide the x and y coordinates of its vertices. To close the shape, you repeat the first vertex at the end of the coordinate list. The same approach works for quadrilaterals and other polygons.
You can customize your plots by adding titles, labels, changing colors, and adjusting line styles. These simple commands form the foundation for more advanced geometric modelling and visualization tasks.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion