Google Summer of Code 2023: Matplotlib

Cover Image for Google Summer of Code 2023: Final Report

Google Summer of Code 2023: Final Report

Ratnabali Dutta

This project focuses on improving the mathematical text support in the Matplotlib library, which is popularly used for data visualization in multiple academic and industrial projects. Matplotlib supports mathematical typesetting through the mathtext module. The mathtext module is derived from LaTeX and is one of the most popular typesetting tools today. We examined the known/reported issues within the existing framework and expanded support for new Math-LaTeX-based features. We also tested and triaged new issues in the mathtext module. The overarching goal of this project was to improve the functionality and usability of mathext for a wide spectrum of use cases.


Improvements to MathTeXt support:

Matplotlib’s mathtext is a powerful tool for including mathematical expressions in your plots. The module utilizing a subset of LaTeX functionalities that enables mathematical symbols, math fonts, and font encodings to fine-tune the usability of math texts inside a data plot. This project fixed existing bugs from the GitHub issue tracker and added some needed font features like bold italics into the mathtext module. Below we discuss some of the changes added to the matplotlib and their respective pull requests (PRs) to get an overview of the project outcomes.

Current state: The things that were added

Some of the New Features added:

  • Bold-Italic fonts

    Mathtext provides the \mathbf and \mathit commands to format texts or glyphs typed in math mode into bold and italic subsequently. Occasionally, we require changing typefaces to emphasize certain symbols to be consistent with the rest of the document where the plot is added. However, nesting the commands for bold and italics doesn’t work as intuitively as LaTeX prioritizes the innermost command and makes using nested commands illegal in math mode. The LaTeX extension package, amsmath, provides the command, \mathbfit, to achieve bold italic glyphs. This PR adds the support for the \mathbfit command needed to get bold italics in matplotlib.

  • Boldsymbol

    The \boldsymbol command allows the users to apply boldface to nearly any symbols. The command, however comes with some confusingly interrelated parts, like Latin alphabets and lowercase Greek alphabets use the bold and italicized versions, unlike digits, uppercase Greek alphabets, and other glyphs which derive the upright bold version from the selected font wherever applicable.

  • Substack

    The \substack command is also provided by the amsmath LaTeX extension package, to produce multiline subscripts or superscripts. Implementing this feature turned out to be challenging because of syntax discrepancies. In LaTeX, the \\ is utilized as the next-line command, which is essential to provide a multiline subformula. However, when parsing the \\ symbol, Python views it as an escape character. Hence, maintaining consistency with the LaTeX command syntax proved to be trickier. To address this incompatibility, we used Pyparsing’s nested_expr feature.

  • Middle | Delimiters

    Delimiters used in formulas should be big enough to fit the equation it wraps. Mathtext offers \left and \right delimiters to obtain such stretchable delimiters. To maintain the aesthetic feel of the equation, LaTeX uses middle delimiters resizes according to the equation. The \middle command is used with strictly with the \left and \right commands.

Future work: The things that are left to do

Integrating a subset of LaTeX functionalities into mathtext requires a detail-attentive eye, which sometimes becomes challenging due to a load of subtleties. Mathtext still requires a lot of improvements, however, a few among many long-term enhancements that would be beneficial are:

Link(s) to code(s):

  • The list of my contributions to Mathtext: pull requests that got merged.

  • A list of open or draft pull requests that are scheduled long-term or currently work in progress.

  • A list of reported issues for Mathtext in the GitHub repository that include stylistic modification, feature requests, and performance improvements.

  • Multiple new features were incorporated into the mathtext module in the new release of Matplotlib (v3.8.0). The list of features can be found in the devdocs link currently.

Note: Weekly Progress and meeting updates can be found in this doc: Hackmd

Miscellaneous links and references