Visualizing Basic Shapes
Swipe to show 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.
1234567891011121314151617181920import matplotlib.pyplot as plt # Triangle vertices triangle_x = [1, 3, 2, 1] triangle_y = [1, 1, 3, 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) plt.title('Triangle 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.
Plotting quadrilateral
12345678910111213141516171819import matplotlib.pyplot as plt # Quadrilateral vertices quad_x = [4, 6, 6, 4, 4] quad_y = [1, 1, 3, 4, 1] # 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('Quadrilateral Visualization') plt.xlabel('X axis') plt.ylabel('Y axis') plt.legend() plt.axis('equal') plt.grid(True) plt.show()
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat