How to suppress matplotlib warnings (and findfont messages), tested
To suppress matplotlib warnings, filter by message= inside warnings.catch_warnings(). A module="matplotlib" filter does not work, because matplotlib attributes warnings to your line. And findfont: Font family not found is a logging message, silenced only via logging.getLogger("matplotlib.font_manager"). Tested on matplotlib 3.11.2.
1. Three common warnings, two different mechanisms
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.legend() # no labels
ax.text(0, 0, "x", fontfamily="NoSuchFont")
fig.savefig("/tmp/a.png")
ax.set_xlim(1, 1) # identical limits
Output:
/w/b1.py:7: UserWarning: No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
ax.legend() # no labels
findfont: Font family 'NoSuchFont' not found.
findfont: Font family 'NoSuchFont' not found.
findfont: Font family 'NoSuchFont' not found.
findfont: Font family 'NoSuchFont' not found.
findfont: Font family 'NoSuchFont' not found.
/w/b1.py:10: UserWarning: Attempting to set identical low and high xlims makes transformation singular; automatically expanding.
ax.set_xlim(1, 1) # identical limits
The legend and xlim messages are Python UserWarnings. The findfont lines are not warnings at all: they are logging messages from the matplotlib.font_manager logger, printed once per font lookup (five times here for one text call and one save).
2. Why filterwarnings(module="matplotlib") does nothing
import logging
import warnings
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
warnings.filterwarnings("ignore", module="matplotlib") # Python warnings only
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.legend()
ax.text(0, 0, "x", fontfamily="NoSuchFont")
fig.savefig("/tmp/a.png")
ax.set_xlim(1, 1)
print("--- now also silence the font logger ---")
logging.getLogger("matplotlib.font_manager").setLevel(logging.ERROR)
ax.text(0, 0, "y", fontfamily="OtherMissingFont")
fig.savefig("/tmp/b.png")
print("done")
Output:
/w/b2.py:10: UserWarning: No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
ax.legend()
findfont: Font family 'NoSuchFont' not found.
findfont: Font family 'NoSuchFont' not found.
findfont: Font family 'NoSuchFont' not found.
findfont: Font family 'NoSuchFont' not found.
findfont: Font family 'NoSuchFont' not found.
/w/b2.py:13: UserWarning: Attempting to set identical low and high xlims makes transformation singular; automatically expanding.
ax.set_xlim(1, 1)
--- now also silence the font logger ---
done
matplotlib issues its warnings with a stacklevel that points at your line (/w/b2.py:10), so the warning's module is your script, not matplotlib, and a module="matplotlib" filter never matches. The findfont messages ignore warning filters entirely; only raising the logger level to ERROR stopped them (the second missing font printed nothing).
3. What works: filter by message, scoped
import warnings
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="No artists with labels")
ax.legend()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
ax.set_xlim(1, 1)
print([type(x.message).__name__ + ": " + str(x.message) for x in w])
Output:
['UserWarning: Attempting to set identical low and high xlims makes transformation singular; automatically expanding.']
Filter on the start of the message (message= is a regex matched from the beginning), inside warnings.catch_warnings() so other warnings stay visible. catch_warnings(record=True) captures warnings into a list instead of printing them, which is handy in tests.
4. Better: fix the cause
- No artists with labels found to put in legend: pass
label="..."toplot/scatter, or drop thelegend()call. - findfont: Font family not found: install the font, or use one matplotlib ships, such as
"DejaVu Sans". - identical low and high xlims: your data has a single x value; set a range yourself, e.g.
set_xlim(x - 1, x + 1).
Related: matplotlib not showing plot · suppress pandas warnings · NumPy RuntimeWarning.
Frequently asked questions
Why doesn't warnings.filterwarnings('ignore', module='matplotlib') work?
matplotlib emits warnings with a stacklevel pointing at your code, so the warning's module is your script. Filter by message (or category) instead.
How do I stop 'findfont: Font family not found' messages?
They come from the logging module, not warnings: logging.getLogger('matplotlib.font_manager').setLevel(logging.ERROR). Or install the font / use 'DejaVu Sans'.
How do I hide 'No artists with labels found to put in legend'?
Give your plots a label= argument, or remove legend(). To only hide it: warnings.filterwarnings('ignore', message='No artists with labels').
Written with AI assistance. Measured on 2026-10-23 in Docker: python:3-alpine (Python 3.14.7) with matplotlib 3.11.2. Each block was run verbatim and the output pasted unedited. PlainTables is our extension; Python and matplotlib are free.