garrettj403 / SciencePlots

Matplotlib styles for scientific plotting
MIT License
7.12k stars 709 forks source link

[Bug]: weird incompatibility between logging module, matplotlib, and Latex #103

Closed Gabriel-p closed 1 year ago

Gabriel-p commented 1 year ago

Environment

System: Linux-6.2.0-34-generic-x86_64-with-glibc2.35
Python: 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0]
SciencePlots: 2.1.0
Matplotlib: 3.7.2
Latex distro: This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2022/dev/Debian) (preloaded format=latex)
 restricted \write18 enabled.

(I obtained the `Latex distro` info just typing `latex` on the terminal, not sure if that's what is being required here)

Describe the issue here

I'm getting dozens of No LaTeX-compatible font found for the serif fontfamily in rcParams. Using default. warnings when I try to use the logging module along with scienceplots.

How can we reproduce it? What have you tried?

Consider the following minimal example:

import logging
import matplotlib.pyplot as plt
import scienceplots
plt.style.use('science')

# 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.savefig("del.png")

In my system this results in:

$ python test.py

Test log line

QApplication: invalid style override 'adwaita' passed, ignoring it.
    Available styles: Windows, Fusion
No LaTeX-compatible font found for the serif fontfamily in rcParams. Using default.
No LaTeX-compatible font found for the serif fontfamily in rcParams. Using default.
(many more of these lines...)

If I comment the logging block out, the "No-LaTeX..." message goes away.

Extra info

No response

echedey-ls commented 1 year ago

Those warnings are being emitted from matplotlib, and because you are using LaTeX. The underlying cause is what the message says. From SciencePlots, this can't be solved, since this is just a styles package: we do not provide fonts.

You may change your used style to plt.style.use(['science', 'no-latex']) so that LaTeX errors don't show, or find a way to exclude matplotlib output. I'm not an expert in matplotlib internals nor the logging module, but maybe there are rcParams that deactivate that or some filters that can be applied to the logger, idk.

Hope to have helped you. I'll keep this open although this has nothing to do with SciencePlots. Once you find a solution, please post it to help others.

Gabriel-p commented 1 year ago

The issue appears to be triggered by the font.serif : cmr10, Computer Modern Serif, DejaVu Serif line in science.mplstyle. This code:

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")

produces apparently the same exact output whether I use or not the serif="DejaVu Serif" argument...

echedey-ls commented 1 year ago

Specified fonts in the following links shouldn't have that problem: