Matplotlib LaTeX Formatting for ML Annotations

Master Matplotlib's LaTeX formatting for clear ML visualizations. Style text, equations, and data labels for advanced scientific plotting.

LaTeX Text Formatting in Matplotlib Annotations

Matplotlib's support for LaTeX allows users to style annotations, labels, and mathematical expressions within plots. This enables a wide range of formatting options, including bold, italic, underlined text, subscripts, superscripts, fractions, matrices, and colored text, making it ideal for scientific and technical visualizations.

Key LaTeX Formatting Capabilities in Matplotlib

  • Mathematical Expressions: Supports fractions, Greek letters, superscripts, subscripts, and more.
  • Text Styling: Allows bold, italics, underline, and font size adjustments using LaTeX commands.
  • Special Characters: Provides mechanisms to escape characters like dollar signs, percentage signs, and underscores.
  • Alignment: Offers limited control over text alignment within annotations using environments like flushleft, center, and flushright.

Basic Text Formatting

Bold Text

Use the \textbf{} command for bold text.

\textbf{Bold Text}

Italic Text

Use the \textit{} command for italic text.

\textit{Italic Text}

Underlined Text

Use the \underline{} command for underlined text.

\underline{Underlined Text}

Font Size Adjustments

LaTeX provides several commands to adjust font sizes:

  • \tiny
  • \small
  • \large
  • \Large
  • \huge
  • \Huge

Example: Bold Text in Annotations

import matplotlib.pyplot as plt

# Create a simple plot
x = [1, 2, 3, 4]
y = [2, 5, 7, 10]
plt.plot(x, y, marker='o', linestyle='-')

# Add an annotation with bold text
plt.annotate(r'\textbf{Max Value}',
   xy=(x[y.index(max(y))], max(y)),
   xytext=(2.5, 8),
   arrowprops=dict(facecolor='black', shrink=0.05),
   fontsize=12,
   color='blue',
   bbox=dict(boxstyle='round,pad=0.3', edgecolor='blue', facecolor='lightblue'))

# Set axis labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Example Plot with LaTeX Annotation')

# Show the plot
plt.show()

Mathematical Notation

When using LaTeX for mathematical expressions within Matplotlib, it's crucial to enclose the LaTeX code within dollar signs ($).

Text Styling in Math Mode

  • Bold Text: Use \mathbf{}.
    \mathbf{Bold Text}
  • Italic Text: Use \textit{}.
    \textit{Italic Text}
  • Sans-serif Text: Use \textsf{}.
    \textsf{Sans-serif Text}
  • Typewriter Text: Use \texttt{}.
    \texttt{Typewriter Text}

Example: Mathematical Notation in Annotations

This example demonstrates how to use bold text and Greek letters within an annotation.

import matplotlib.pyplot as plt

# Create a simple plot
x = [1, 2, 3, 4]
y = [2, 5, 7, 10]
plt.plot(x, y, marker='o', linestyle='-')

# Annotate with mathematical notation
plt.annotate(r'$\mathbf{Max Value} = \alpha + \beta$',
   xy=(x[y.index(max(y))], max(y)),
   xytext=(2.5, 8),
   arrowprops=dict(facecolor='black', shrink=0.05),
   fontsize=12,
   color='blue')

# Show the plot
plt.show()

Subscripts and Superscripts

  • Subscripts: Use the underscore _. For multi-character subscripts, enclose them in curly braces {}.
    x_{1}
  • Superscripts: Use the caret ^. For multi-character superscripts, enclose them in curly braces {}.
    x^{2}

Example: Using Subscripts and Superscripts in Annotations

import matplotlib.pyplot as plt

# Create a simple plot
x = [1, 2, 3, 4]
y = [2, 5, 7, 10]
plt.plot(x, y, 'o-', label='Data')

# Annotate with subscript and superscript
plt.annotate(r'$\mathrm{Point}_{\mathrm{max}}^{(4, 10)}$',
   xy=(x[y.index(max(y))], max(y)),
   xytext=(3, 8),
   arrowprops=dict(facecolor='black', arrowstyle='->'),
   fontsize=12,
   color='red')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Example Plot with Annotation')
plt.legend()
plt.show()

Combining Text and Math

You can seamlessly combine plain text with mathematical expressions within a single annotation.

Example: Combining Text and Math in Annotations

This example shows an annotation with a differential equation.

import matplotlib.pyplot as plt

# Create a simple plot
x = [1, 2, 3, 4]
y = [2, 5, 7, 10]
plt.plot(x, y, 'o-', label='Data')

# Annotate with combined text and math
plt.annotate(r'$\frac{dx}{dt} = \alpha \cdot x(t) + \beta$ is the differential equation',
   xy=(x[2], y[2]),
   xytext=(2, 6),
   arrowprops=dict(facecolor='black', arrowstyle='->'),
   fontsize=12,
   color='blue')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Example Plot with Annotation using LaTeX')
plt.legend()
plt.show()

Text Color and Font Styles

Text Color

Use the \textcolor{color_name}{text} command to apply a specific color to text.

\textcolor{color_name}{text}

Font Styles within Math Mode

  • Bold: \textbf{Bold Text} (works for text within math mode, but \mathbf is preferred for mathematical variables)
  • Italics: \textit{Italic Text}
  • Underline: \underline{Underlined Text}

Example: Text Color and Font Styles in Annotations

import matplotlib.pyplot as plt

# Create a simple plot
x = [1, 2, 3, 4]
y = [2, 5, 7, 10]
plt.plot(x, y, 'o-', label='Data')

# Annotate with different text color and font style
plt.annotate(r'\mathbf{\textcolor{red}{Max value:}} \ \textit{\textcolor{blue}{y_{\text{max}} = 10}}',
   xy=(x[y.index(max(y))], max(y)),
   xytext=(3, 8),
   arrowprops=dict(facecolor='black', arrowstyle='->'),
   fontsize=12)

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Example Plot with Annotation of Color and Font Style')
plt.legend()
plt.show()

Fractions, Arrays, and Matrices

Fractions

Use the \frac{numerator}{denominator} command for fractions.

\frac{a}{b}

Example: Fractions in Matplotlib

This example displays a fraction equation using plt.text.

import matplotlib.pyplot as plt

# Define fraction equation
equation = r'$\frac{a}{b} = \frac{c}{d}$'

# Display equation in a plot
plt.text(0.5, 0.5, equation, fontsize=12, ha='center')
plt.axis('off')

# Show the plot
plt.show()

Matrices

Use the bmatrix environment for matrices.

\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}

Example: Matrices in Matplotlib

This example displays a matrix equation using plt.text.

import matplotlib.pyplot as plt

# Define matrix equation
equation = r'$\begin{bmatrix} a & b \\ c & d \end{bmatrix}$'

# Display equation in a plot
plt.text(0.5, 0.5, equation, fontsize=12, ha='center')
plt.axis('off')

# Show the plot
plt.show()

Conclusion

LaTeX support in Matplotlib significantly enhances the ability to create professional and informative visualizations. By leveraging LaTeX syntax, users can precisely control the appearance of text and mathematical content in annotations, labels, and titles, leading to clearer and more impactful scientific and technical plots.

Matplotlib LaTeX Formatting for ML Annotations