Matplotlib Fill Betweenx

MatplotlibMatplotlibBeginner
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

Matplotlib est une bibliothèque de visualisation de données en Python. Elle fournit une large variété d'outils pour créer des graphiques, des diagrammes et des courbes complexes et personnalisables. L'un des outils les plus utiles fournis par Matplotlib est la fonction fill_betweenx. Cette fonction est utilisée pour remplir la zone entre deux courbes horizontales. Dans ce tutoriel, nous explorerons la fonction fill_betweenx et apprendrons à l'utiliser pour créer différents types de graphiques.

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 Notebook pour accéder à Jupyter Notebook pour pratiquer.

Parfois, vous devrez peut-être attendre quelques secondes pour que Jupyter Notebook ait fini de charger. La validation des opérations ne peut pas être automatisée en raison des limitations de 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églerons rapidement le problème pour vous.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") python/BasicConceptsGroup -.-> python/booleans("Booleans") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") 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-48735{{"Matplotlib Fill Betweenx"}} matplotlib/figures_axes -.-> lab-48735{{"Matplotlib Fill Betweenx"}} python/booleans -.-> lab-48735{{"Matplotlib Fill Betweenx"}} matplotlib/line_plots -.-> lab-48735{{"Matplotlib Fill Betweenx"}} python/lists -.-> lab-48735{{"Matplotlib Fill Betweenx"}} python/tuples -.-> lab-48735{{"Matplotlib Fill Betweenx"}} python/importing_modules -.-> lab-48735{{"Matplotlib Fill Betweenx"}} python/numerical_computing -.-> lab-48735{{"Matplotlib Fill Betweenx"}} python/data_visualization -.-> lab-48735{{"Matplotlib Fill Betweenx"}} end

Tracer un simple remplissage entrex

Dans cette étape, nous allons apprendre à utiliser la fonction fill_betweenx pour créer un tracé simple. Nous allons remplir la zone entre deux courbes.

import matplotlib.pyplot as plt
import numpy as np

y = np.arange(0.0, 2, 0.01)
x1 = np.sin(2 * np.pi * y)
x2 = 1.2 * np.sin(4 * np.pi * y)

fig, ax = plt.subplots(figsize=(6, 6))

ax.fill_betweenx(y, x1, x2, color='green', alpha=0.5)
ax.plot(x1, y, color='blue')
ax.plot(x2, y, color='red')

plt.show()

Création de multiples sous-graphiques

Dans cette étape, nous allons apprendre à créer de multiples sous-graphiques et à utiliser la fonction fill_betweenx pour remplir la zone entre deux courbes horizontales dans chaque sous-graphique.

import matplotlib.pyplot as plt
import numpy as np

y = np.arange(0.0, 2, 0.01)
x1 = np.sin(2 * np.pi * y)
x2 = 1.2 * np.sin(4 * np.pi * y)

fig, [ax1, ax2, ax3] = plt.subplots(1, 3, sharey=True, figsize=(12, 4))

ax1.fill_betweenx(y, 0, x1, color='green', alpha=0.5)
ax1.plot(x1, y, color='blue')
ax1.set_title('Fill between (x1, 0)')

ax2.fill_betweenx(y, x1, 1, color='red', alpha=0.5)
ax2.plot(x1, y, color='blue')
ax2.set_title('Fill between (x1, 1)')

ax3.fill_betweenx(y, x1, x2, color='orange', alpha=0.5)
ax3.plot(x1, y, color='blue')
ax3.plot(x2, y, color='red')
ax3.set_title('Fill between (x1, x2)')

plt.show()

Utilisation de conditions logiques

Dans cette étape, nous allons apprendre à utiliser des conditions logiques pour remplir la zone entre deux courbes horizontales.

import matplotlib.pyplot as plt
import numpy as np

y = np.arange(0.0, 2, 0.01)
x1 = np.sin(2 * np.pi * y)
x2 = 1.2 * np.sin(4 * np.pi * y)

fig, ax = plt.subplots(figsize=(6, 6))

ax.plot(x1, y, color='black')
ax.plot(x2, y, color='black')

ax.fill_betweenx(y, x1, x2, where=x2 >= x1, facecolor='green', alpha=0.5)
ax.fill_betweenx(y, x1, x2, where=x2 <= x1, facecolor='red', alpha=0.5)

plt.show()

Utilisation d'arrays masqués

Dans cette étape, nous allons apprendre à utiliser des arrays masqués pour remplir la zone entre deux courbes horizontales.

import matplotlib.pyplot as plt
import numpy as np

y = np.arange(0.0, 2, 0.01)
x1 = np.sin(2 * np.pi * y)
x2 = 1.2 * np.sin(4 * np.pi * y)
x2 = np.ma.masked_greater(x2, 1.0)

fig, ax = plt.subplots(figsize=(6, 6))

ax.plot(x1, y, color='black')
ax.plot(x2, y, color='black')

ax.fill_betweenx(y, x1, x2, where=x2 >= x1, facecolor='green', alpha=0.5)
ax.fill_betweenx(y, x1, x2, where=x2 <= x1, facecolor='red', alpha=0.5)

plt.show()

Sommaire

Dans ce tutoriel, nous avons appris à utiliser la fonction fill_betweenx dans Matplotlib pour remplir la zone entre deux courbes horizontales. Nous avons également appris à créer de multiples sous-graphiques et à utiliser des conditions logiques et des tableaux masqués pour créer différents types de tracés. En utilisant la fonction fill_betweenx, nous pouvons créer des tracés complexes et personnalisables en Python.