Matplotlib-Akzentierte Textvisualisierung

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 Bibliothek in Python, die zur Datenvisualisierung verwendet wird. Es unterstützt akzentuierte Zeichen über TeX-Mathtext oder Unicode. In diesem Tutorial wird gezeigt, wie man akzentuierten Text in Matplotlib verwendet.

Tipps für die virtuelle Maschine

Nachdem der Start der virtuellen Maschine abgeschlossen ist, klicken Sie in der oberen linken Ecke, um zur Registerkarte Notebook zu wechseln und Jupyter Notebook für die Übung zu öffnen.

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/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) 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/comments("Comments") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/tuples("Tuples") python/DataStructuresGroup -.-> python/sets("Sets") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48530{{"Matplotlib-Akzentierte Textvisualisierung"}} matplotlib/figures_axes -.-> lab-48530{{"Matplotlib-Akzentierte Textvisualisierung"}} python/comments -.-> lab-48530{{"Matplotlib-Akzentierte Textvisualisierung"}} matplotlib/line_plots -.-> lab-48530{{"Matplotlib-Akzentierte Textvisualisierung"}} python/tuples -.-> lab-48530{{"Matplotlib-Akzentierte Textvisualisierung"}} python/sets -.-> lab-48530{{"Matplotlib-Akzentierte Textvisualisierung"}} python/build_in_functions -.-> lab-48530{{"Matplotlib-Akzentierte Textvisualisierung"}} python/importing_modules -.-> lab-48530{{"Matplotlib-Akzentierte Textvisualisierung"}} python/data_visualization -.-> lab-48530{{"Matplotlib-Akzentierte Textvisualisierung"}} end

Verwenden von Mathtext

Mathtext ist eine Funktion in Matplotlib, die es Ihnen ermöglicht, TeX-Befehle zum Rendern von mathematischen Symbolen und Gleichungen zu verwenden. Mathtext unterstützt auch akzentuierte Zeichen.

import matplotlib.pyplot as plt

## Mathtext-Demo
fig, ax = plt.subplots()
ax.plot(range(10))
ax.set_title(r'$\ddot{o}\acute{e}\grave{e}\hat{O}'
             r'\breve{i}\bar{A}\tilde{n}\vec{q}$', fontsize=20)

## Kurzschreibweisen werden ebenfalls unterstützt und geschweifte Klammern sind optional
ax.set_xlabel(r"""$\"o\ddot o \'e\`e\~n\.x\^y$""", fontsize=20)
ax.text(4, 0.5, r"$F=m\ddot{x}$")
fig.tight_layout()

Verwenden von Unicode-Zeichen

Matplotlib unterstützt auch die direkte Verwendung von Unicode-Zeichen in Zeichenketten.

import matplotlib.pyplot as plt

## Unicode-Demo
fig, ax = plt.subplots()
ax.set_title("GISCARD CHAHUTÉ À L'ASSEMBLÉE")
ax.set_xlabel("LE COUP DE DÉ DE DE GAULLE")
ax.set_ylabel('André war hier!')
ax.text(0.2, 0.8, 'Institut für Festkörperphysik', rotation=45)
ax.text(0.4, 0.2, 'AVA (prüfe die Kerning)')

plt.show()

Ausführen des Codes

Um den Code auszuführen, muss Matplotlib installiert sein. Sie können Matplotlib mit pip installieren. Öffnen Sie die Befehlszeile und geben Sie ein:

pip install matplotlib

Zusammenfassung

Matplotlib unterstützt akzentuierte Zeichen über TeX-Mathtext oder Unicode. Sie können TeX-Befehle verwenden, um mathematische Symbole und Gleichungen zu rendern. Matplotlib unterstützt auch die direkte Verwendung von Unicode-Zeichen in Zeichenketten.