Matplotlib Fill Betweenx

MatplotlibMatplotlibBeginner
Jetzt üben

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

💡 Dieser Artikel wurde von AI-Assistenten übersetzt. Um die englische Version anzuzeigen, können Sie hier klicken

Einführung

Matplotlib ist eine Datenvisualisierungsbibliothek in Python. Es bietet eine Vielzahl von Tools, um komplexe und anpassbare Diagramme, Grafiken und Kurven zu erstellen. Eine der nützlichsten Tools, die von Matplotlib bereitgestellt wird, ist die fill_betweenx-Funktion. Diese Funktion wird verwendet, um den Bereich zwischen zwei horizontalen Kurven zu füllen. In diesem Tutorial werden wir die fill_betweenx-Funktion untersuchen und lernen, wie sie verwendet wird, um verschiedene Arten von Diagrammen zu erstellen.

VM-Tipps

Nachdem der VM-Start abgeschlossen ist, klicken Sie in der oberen linken Ecke, um zur Registerkarte Notebook zu wechseln und Jupyter Notebook für die Übung zu nutzen.

Manchmal müssen Sie einige Sekunden warten, bis Jupyter Notebook vollständig geladen ist. Die Validierung von Vorgängen kann aufgrund von Einschränkungen in Jupyter Notebook nicht automatisiert werden.

Wenn Sie bei der Lernphase Probleme haben, können Sie Labby gerne fragen. Geben Sie nach der Sitzung Feedback, und wir werden das Problem für Sie prompt beheben.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) 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

Einfaches Füllen zwischen horizontalen Linien plotten

In diesem Schritt werden wir lernen, wie die fill_betweenx-Funktion verwendet wird, um ein einfaches Diagramm zu erstellen. Wir werden den Bereich zwischen zwei Kurven füllen.

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()

Mehrere Teilplots erstellen

In diesem Schritt werden wir lernen, wie mehrere Teilplots erstellt werden und wie die fill_betweenx-Funktion verwendet wird, um den Bereich zwischen zwei horizontalen Kurven in jedem Teilplot zu füllen.

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()

Verwendung logischer Bedingungen

In diesem Schritt werden wir lernen, wie logische Bedingungen verwendet werden, um den Bereich zwischen zwei horizontalen Kurven zu füllen.

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()

Verwendung maskierter Arrays

In diesem Schritt werden wir lernen, wie maskierte Arrays verwendet werden, um den Bereich zwischen zwei horizontalen Kurven zu füllen.

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()

Zusammenfassung

In diesem Tutorial haben wir gelernt, wie die fill_betweenx-Funktion in Matplotlib verwendet wird, um den Bereich zwischen zwei horizontalen Kurven zu füllen. Wir haben auch gelernt, wie mehrere Teilplots erstellt werden und wie logische Bedingungen und maskierte Arrays verwendet werden, um verschiedene Arten von Plots zu erstellen. Mit der fill_betweenx-Funktion können wir in Python komplexe und anpassbare Plots erstellen.