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

Visualizing Basic Shapes

Pyyhkäise näyttääksesi valikon

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?

Valitse oikea vastaus

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 6

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 6
some-alt