Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Visualizing Basic Shapes | Introduction to Geometric Modelling
Geometric Modelling with Python

Visualizing Basic Shapes

Sveip for å vise menyen

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.

123456789101112131415161718192021222324252627
import 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.

question mark

Which matplotlib function is commonly used to fill the interior of a polygon with color?

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 6

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 1. Kapittel 6
some-alt