2

Suppose I absolutely need to use tex to generate the text of my figures, and that I use the following code:

#!/usr/bin/env python3

import matplotlib as mpl
mpl.use("PDF")
from matplotlib.backends.backend_pgf import FigureCanvasPgf
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


# https://stackoverflow.com/a/42768093/1878788
mpl.backend_bases.register_backend('pdf', FigureCanvasPgf)
TEX_PARAMS = {
    "text.usetex": True,            # use LaTeX to write all text
    "pgf.rcfonts": False,           # Ignore Matplotlibrc
    "pgf.texsystem": "lualatex",  # hoping to avoid memory issues
}
mpl.rcParams.update(TEX_PARAMS)


def main():
    data = pd.DataFrame({
        "A1": np.random.randn(4),
        "B1": np.random.randn(4),
        "A2": np.random.randn(4),
        "B2": np.random.randn(4)})
    data.plot.box()
    plt.savefig("test_tex_boxplot.pdf")


if __name__ == "__main__":
    exit(main())

This works.

Now if the column names in my DataFrame contain underscores (A_1 instead of A1), tex fails:

RuntimeError: LaTeX was not able to process the following string:
b'A_1'

How can I fix this?

bli
  • 7,549
  • 7
  • 48
  • 94

1 Answers1

1

One solution is to change the column names just before calling the plot function, by escaping the underscores:

#!/usr/bin/env python3

import matplotlib as mpl
mpl.use("PDF")
from matplotlib.backends.backend_pgf import FigureCanvasPgf
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from re import sub


# https://stackoverflow.com/a/42768093/1878788
mpl.backend_bases.register_backend('pdf', FigureCanvasPgf)
TEX_PARAMS = {
    "text.usetex": True,            # use LaTeX to write all text
    "pgf.rcfonts": False,           # Ignore Matplotlibrc
    "pgf.texsystem": "lualatex",  # hoping to avoid memory issues
}
mpl.rcParams.update(TEX_PARAMS)


def main():
    data = pd.DataFrame({
        "A_1": np.random.randn(4),
        "B_1": np.random.randn(4),
        "A_2": np.random.randn(4),
        "B_2": np.random.randn(4)})
    usetex = mpl.rcParams.get("text.usetex", False)
    if usetex:
        data.columns = [sub("_", "\_", colname) for colname in data.columns]
    data.plot.box()
    plt.savefig("test_tex_boxplot.pdf")


if __name__ == "__main__":
    exit(main())
bli
  • 7,549
  • 7
  • 48
  • 94