From 399fd93c277c2e69b1e1a4f7009a573759491001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Dock=C3=A8s?= Date: Mon, 9 Dec 2024 13:40:28 +0100 Subject: [PATCH] remove use of matplotlib.rc_context (#1172) --- CHANGES.rst | 5 +++++ skrub/_reporting/_plotting.py | 23 +++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 1de6d3e93..03f16dd7e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -32,6 +32,11 @@ Changes Bug fixes --------- +* Generating a ``TableReport`` could have an effect on the matplotib + configuration which could cause plots not to display inline in jupyter + notebooks any more. This has been fixed in skrub in :pr:`1172` by + :user:`Jérôme Dockès ` and the matplotlib issue can be tracked + [here](https://github.com/matplotlib/matplotlib/issues/25041). Release 0.4.0 diff --git a/skrub/_reporting/_plotting.py b/skrub/_reporting/_plotting.py index e1e4b58f0..6a4daadbb 100644 --- a/skrub/_reporting/_plotting.py +++ b/skrub/_reporting/_plotting.py @@ -8,7 +8,6 @@ import re import warnings -import matplotlib import numpy as np from matplotlib import pyplot as plt @@ -51,9 +50,23 @@ def _plot(plotting_fun): @functools.wraps(plotting_fun) def plot_with_config(*args, **kwargs): - # This causes matplotlib to insert labels etc as text in the svg rather - # than drawing the glyphs. - with matplotlib.rc_context({"svg.fonttype": "none"}): + # + # Note: we do not use `matplotlib.rc_context` because it can prevent the + # inline display of plots in jupyter notebooks: + # + # https://github.com/matplotlib/matplotlib/issues/25041 + # https://github.com/matplotlib/matplotlib/issues/26716 + # + # otherwise we could write + # with matplotlib.rc_context({"svg.fonttype": "none"}): + # + # See https://github.com/skrub-data/skrub/pull/1172 + # + original_font_type = plt.rcParams["svg.fonttype"] + try: + # This causes matplotlib to insert labels etc as text in the svg rather + # than drawing the glyphs. + plt.rcParams["svg.fonttype"] = "none" with warnings.catch_warnings(): # We do not care about missing glyphs because the text is # rendered & the viewbox is recomputed in the browser. @@ -62,6 +75,8 @@ def plot_with_config(*args, **kwargs): "ignore", "Matplotlib currently does not support Arabic natively" ) return plotting_fun(*args, **kwargs) + finally: + plt.rcParams["svg.fonttype"] = original_font_type return plot_with_config