Erstellen von Text und Mathetext mit Pyplot

PythonPythonBeginner
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 leistungsstarke Datenvisualisierungsbibliothek in Python. Es bietet eine Vielzahl von Tools, um Graphen und Diagramme in Python zu erstellen. In diesem Tutorial lernen wir, wie man Text und Mathtext mit pyplot erstellt.

VM-Tipps

Nachdem die VM gestartet 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 matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlotCustomizationGroup(["Plot Customization"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") matplotlib/PlotCustomizationGroup -.-> matplotlib/titles_labels("Adding Titles and Labels") 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-48888{{"Erstellen von Text und Mathetext mit Pyplot"}} matplotlib/figures_axes -.-> lab-48888{{"Erstellen von Text und Mathetext mit Pyplot"}} matplotlib/line_plots -.-> lab-48888{{"Erstellen von Text und Mathetext mit Pyplot"}} python/lists -.-> lab-48888{{"Erstellen von Text und Mathetext mit Pyplot"}} python/tuples -.-> lab-48888{{"Erstellen von Text und Mathetext mit Pyplot"}} matplotlib/titles_labels -.-> lab-48888{{"Erstellen von Text und Mathetext mit Pyplot"}} python/importing_modules -.-> lab-48888{{"Erstellen von Text und Mathetext mit Pyplot"}} python/numerical_computing -.-> lab-48888{{"Erstellen von Text und Mathetext mit Pyplot"}} python/data_visualization -.-> lab-48888{{"Erstellen von Text und Mathetext mit Pyplot"}} end

Importieren der erforderlichen Bibliotheken

Zunächst müssen wir die erforderlichen Bibliotheken importieren. Wir werden die Bibliotheken numpy und matplotlib.pyplot importieren.

import matplotlib.pyplot as plt
import numpy as np

Daten erstellen

Als nächstes werden wir die Daten für das Diagramm erstellen. Wir werden eine Sinuswelle mit der Bibliothek numpy erstellen.

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)

Das Diagramm zeichnen

Jetzt werden wir das Diagramm mit der plot()-Methode der pyplot-Bibliothek zeichnen.

plt.plot(t, s)

Text zum Diagramm hinzufügen

Wir können Text zum Diagramm hinzufügen, indem wir die text()-Methode der pyplot-Bibliothek verwenden. Wir werden den Text "Hello, world!" an den Koordinaten (0, -1) hinzufügen.

plt.text(0, -1, r'Hello, world!', fontsize=15)

Titel, X-Beschriftung und Y-Beschriftung hinzufügen

Wir können Titel, X-Beschriftung und Y-Beschriftung zum Diagramm hinzufügen, indem wir die title(), xlabel() und ylabel()-Methoden der pyplot-Bibliothek verwenden. Wir werden "Spannung gegen Zeit" als Titel, "Zeit [s]" als X-Beschriftung und "Spannung [mV]" als Y-Beschriftung hinzufügen.

plt.title(r'Voltage vs Time', fontsize=20)
plt.xlabel('Time [s]')
plt.ylabel('Voltage [mV]')

Zeige das Diagramm an

Schließlich werden wir das Diagramm mit der show()-Methode der pyplot-Bibliothek anzeigen.

plt.show()

Zusammenfassung

In diesem Tutorial haben wir gelernt, wie man Text und Mathetext mit pyplot erstellt. Wir haben gelernt, wie man Text zum Diagramm hinzufügt, wie man Titel, X-Beschriftung und Y-Beschriftung zum Diagramm hinzufügt und wie man das Diagramm mit pyplot anzeigt.