Créer un graphique de spirale avec Python et Matplotlib

PythonPythonBeginner
Pratiquer maintenant

This tutorial is from open-source community. Access the source code

💡 Ce tutoriel est traduit par l'IA à partir de la version anglaise. Pour voir la version originale, vous pouvez cliquer ici

Introduction

Dans ce laboratoire, nous allons utiliser Python et Matplotlib pour créer un graphique en spirale remplie. Nous utiliserons les bibliothèques numpy et matplotlib.pyplot pour générer le graphique.

Conseils sur la machine virtuelle

Une fois le démarrage de la machine virtuelle terminé, cliquez dans le coin supérieur gauche pour basculer vers l'onglet Carnet de notes pour accéder au carnet Jupyter Notebook pour pratiquer.

Parfois, vous devrez peut-être attendre quelques secondes pour que le carnet Jupyter Notebook ait fini de charger. La validation des opérations ne peut pas être automatisée en raison des limitations du carnet Jupyter Notebook.

Si vous rencontrez des problèmes pendant l'apprentissage, n'hésitez pas à demander à Labby. Donnez votre feedback après la session, et nous résoudrons rapidement le problème pour vous.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") python/ControlFlowGroup -.-> python/for_loops("For Loops") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("Numerical Computing") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48736{{"Créer un graphique de spirale avec Python et Matplotlib"}} matplotlib/figures_axes -.-> lab-48736{{"Créer un graphique de spirale avec Python et Matplotlib"}} python/for_loops -.-> lab-48736{{"Créer un graphique de spirale avec Python et Matplotlib"}} python/lists -.-> lab-48736{{"Créer un graphique de spirale avec Python et Matplotlib"}} python/tuples -.-> lab-48736{{"Créer un graphique de spirale avec Python et Matplotlib"}} python/importing_modules -.-> lab-48736{{"Créer un graphique de spirale avec Python et Matplotlib"}} python/numerical_computing -.-> lab-48736{{"Créer un graphique de spirale avec Python et Matplotlib"}} python/data_visualization -.-> lab-48736{{"Créer un graphique de spirale avec Python et Matplotlib"}} end

Importation des bibliothèques

Nous allons importer les bibliothèques nécessaires pour générer le graphique. Nous utiliserons numpy pour les calculs numériques et matplotlib.pyplot pour créer le graphique.

import matplotlib.pyplot as plt
import numpy as np

Définition des variables

Nous allons définir les variables theta, a et b pour notre graphique.

theta = np.arange(0, 8*np.pi, 0.1)
a = 1
b =.2

Générer le graphique

Nous allons générer le graphique en parcourant quatre valeurs de dt et en concaténant les tableaux x et y résultants.

for dt in np.arange(0, 2*np.pi, np.pi/2.0):

    x = a*np.cos(theta + dt)*np.exp(b*theta)
    y = a*np.sin(theta + dt)*np.exp(b*theta)

    dt = dt + np.pi/4.0

    x2 = a*np.cos(theta + dt)*np.exp(b*theta)
    y2 = a*np.sin(theta + dt)*np.exp(b*theta)

    xf = np.concatenate((x, x2[::-1]))
    yf = np.concatenate((y, y2[::-1]))

    p1 = plt.fill(xf, yf)

plt.show()

Interprétation

Le graphique généré par le code montre une spirale remplie de couleur. Les variables a et b contrôlent la forme de la spirale, tandis que la variable theta contrôle le nombre de tours. La boucle sur dt nous permet de créer une spirale avec quatre "bras" en effectuant une rotation du graphique de 45 degrés à chaque fois.

Sommaire

Dans ce laboratoire, nous avons appris à utiliser Python et Matplotlib pour générer un graphique de spirale remplie. Nous avons défini des variables pour le graphique, généré le graphique à l'aide d'une boucle et interprété le graphique résultant. Grâce à ces connaissances, nous pouvons créer des graphiques similaires avec différentes formes et couleurs.