Skip to content

Commit

Permalink
remove use of matplotlib.rc_context (skrub-data#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromedockes committed Dec 11, 2024
1 parent c4ca457 commit 993ed4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 <jeromedockes>` and the matplotlib issue can be tracked
[here](https://github.com/matplotlib/matplotlib/issues/25041).


Release 0.4.0
Expand Down
23 changes: 19 additions & 4 deletions skrub/_reporting/_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import re
import warnings

import matplotlib
import numpy as np
from matplotlib import pyplot as plt

Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down

0 comments on commit 993ed4a

Please sign in to comment.