Search code examples
pythonmatplotliblogginglatex

Conflict between logging module, Latex and matplotlib


The following example:

import logging
import matplotlib.pyplot as plt
import matplotlib.pyplot as mpl

mpl.rc('font', family='serif', serif="DejaVu Serif")
mpl.rc('text', usetex=True)

# Set up logging module
level = logging.INFO
frmt = '%(message)s'
handlers = [
    logging.FileHandler("test.log", mode='a'),
    logging.StreamHandler()]
logging.basicConfig(level=level, format=frmt, handlers=handlers)

fig = plt.figure()
plt.scatter(.5, .5)
plt.xlabel("test label")
plt.savefig("del.png")

generates dozens of warnings:

No LaTeX-compatible font found for the serif fontfamily in rcParams. Using default.

All these go away and the output stays apparently exactly the same if I comment out the serif="DejaVu Serif" argument. I can also leave that argument in and comment out the logging block, and the warnings also go away.

I'm running elementary OS (based on Ubuntu 22.04.3) with apparently all the required fonts and LaTeX packages installed.

Is this a conflict between the logging module and my system's fonts? Is this an issue with logging and LaTeX? What is going on here?


Solution

  • The handlers you've configured don't use fonts per se - they just output text messages. My guess is that matplotlib or some other library is logging messages (called from your code after the basicConfig() call), but they don't show up unless you configure logging. This could explain why the messages either go away if you remove logging (messages are generated, but don't appear because there's no logging handlers to output them), or if you remove the serif= argument (events aren't logged in that case).

    I advise you to leave both in and change the format string frmt to include %(name)s and %(levelname)s in order to see where the messages are being logged, and at what severity. That should give a better idea of what's going on.