Demo de fuentes Stix

PythonPythonBeginner
Practicar Ahora

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

💡 Este tutorial está traducido por IA desde la versión en inglés. Para ver la versión original, puedes hacer clic aquí

Introducción

Este es un tutorial paso a paso sobre cómo usar Matplotlib para graficar ecuaciones matemáticas y texto con diferentes fuentes.

Consejos sobre la VM

Una vez que se haya iniciado la VM, haga clic en la esquina superior izquierda para cambiar a la pestaña Cuaderno y acceder a Jupyter Notebook para practicar.

A veces, es posible que tenga que esperar unos segundos a que Jupyter Notebook termine de cargarse. La validación de las operaciones no se puede automatizar debido a las limitaciones de Jupyter Notebook.

Si tiene problemas durante el aprendizaje, no dude en preguntar a Labby. Deje comentarios después de la sesión y resolveremos el problema rápidamente para usted.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) 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/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-48961{{"Demo de fuentes Stix"}} matplotlib/figures_axes -.-> lab-48961{{"Demo de fuentes Stix"}} python/for_loops -.-> lab-48961{{"Demo de fuentes Stix"}} python/lists -.-> lab-48961{{"Demo de fuentes Stix"}} python/tuples -.-> lab-48961{{"Demo de fuentes Stix"}} python/sets -.-> lab-48961{{"Demo de fuentes Stix"}} python/build_in_functions -.-> lab-48961{{"Demo de fuentes Stix"}} python/importing_modules -.-> lab-48961{{"Demo de fuentes Stix"}} python/data_visualization -.-> lab-48961{{"Demo de fuentes Stix"}} end

Instalar Matplotlib

Para comenzar, es necesario tener Matplotlib instalado en su entorno. Puede hacer esto ejecutando el siguiente comando en su terminal o línea de comandos:

pip install matplotlib

Importar Matplotlib y Definir Texto

En este paso, importamos Matplotlib y definimos el texto que graficaremos usando diferentes fuentes.

import matplotlib.pyplot as plt

circle123 = "\N{CIRCLED DIGIT ONE}\N{CIRCLED DIGIT TWO}\N{CIRCLED DIGIT THREE}"

tests = [
    r'$%s\;\mathrm{%s}\;\mathbf{%s}$' % ((circle123,) * 3),
    r'$\mathsf{Sans \Omega}\;\mathrm{\mathsf{Sans \Omega}}\;'
    r'\mathbf{\mathsf{Sans \Omega}}$',
    r'$\mathtt{Monospace}$',
    r'$\mathcal{CALLIGRAPHIC}$',
    r'$\mathbb{Blackboard\;\pi}$',
    r'$\mathrm{\mathbb{Blackboard\;\pi}}$',
    r'$\mathbf{\mathbb{Blackboard\;\pi}}$',
    r'$\mathfrak{Fraktur}\;\mathbf{\mathfrak{Fraktur}}$',
    r'$\mathscr{Script}$',
]

Graficar el Texto

Ahora que hemos definido el texto, podemos graficarlo usando Matplotlib. En este paso, creamos una figura y agregamos el texto a ella usando el método fig.text().

fig = plt.figure(figsize=(8, len(tests) + 2))
for i, s in enumerate(tests[::-1]):
    fig.text(0, (i +.5) / len(tests), s, fontsize=32)

plt.show()

Analizar la Salida

Después de ejecutar el código, deberíamos ver el texto graficado con diferentes fuentes. La salida debería verse así:

plotted text with fonts

Resumen

En este tutorial, aprendimos cómo graficar ecuaciones matemáticas y texto usando diferentes fuentes en Matplotlib. Cubrimos los pasos para instalar Matplotlib, importarlo en nuestro código, definir el texto y graficarlo usando fig.text().