mplcairo icon indicating copy to clipboard operation
mplcairo copied to clipboard

matplotlib inline support for JupyterLab

Open WinDerek opened this issue 2 years ago • 1 comments

Versions Information

>>> import mplcairo
>>> mplcairo.get_versions()
{'python': '3.9.5 (default, Jun  4 2021, 12:28:51) \n[GCC 7.5.0]', 'mplcairo': '0.4', 'matplotlib': '3.5.1', 'cairo': '1.16.0', 'freetype': '2.10.1', 'pybind11': '2.6.2', 'raqm': None, 'hb': None}

Problem Specification

I was trying to use mplcairo as my backend for Matplotlib plotting with custom .otf font (Source Han Serif SC, a popular CJK font). Here is my code:

import matplotlib
print("matplotlib.__version__:", matplotlib.__version__)
print('Default backend:', matplotlib.get_backend())
matplotlib.use("module://mplcairo.base")
# matplotlib.use("Cairo")
print('Backend is now:', matplotlib.get_backend())
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42


FIG_SIZE = (8,4.5)
DPI = 120
CHINESE_FP = fm.FontProperties(fname='./font/source_han_serif_sc_regular.otf', math_fontfamily="stix")


fig, ax = plt.subplots(figsize=FIG_SIZE, dpi=DPI)
ax.set_xlabel(r'测试中文字体 Normal', fontsize=28.0, fontweight="normal", fontproperties=CHINESE_FP)
ax.set_ylabel(r'测试中文字体 Bold', fontsize=28.0, fontweight="bold", fontproperties=CHINESE_FP)
# Save the figure
fig.savefig('./cjk_font_test.pdf', format='pdf', bbox_inches='tight')
fig.savefig('./cjk_font_test.eps', format='eps', bbox_inches='tight')
fig.savefig('./cjk_font_test.png', format='png', bbox_inches='tight')

print("Figures saved!")

In order to enable Matplotlib inline plotting with the mplcairo backend in JupyterLab, I tried adding the following code snippet (copied from the README of mplcairo) in my JupyterLab config file (jupyterlab_config.py):

import mplcairo.base
import ipykernel.pylab.backend_inline
ipykernel.pylab.backend_inline.new_figure_manager = mplcairo.base.new_figure_manager

I started my JupyterLab using the following command:

$ jupyter lab --config jupyterlab_config.py

This config did not work. I can save figures successfully, but there is no inline figure in the notebook. After searching for a while, I found that ipykernel.pylab.backend_inline is deprecated, matplotlib_inline.backend_inline should be used instead (see this commit). Thus, I tried the following config as well:

import mplcairo.base
import matplotlib_inline.backend_inline
matplotlib_inline.backend_inline.new_figure_manager = mplcairo.base.new_figure_manager

However, this did not work neither. I've read through the documentation and the webpages I can found. Hope mplcairo can be used in JupyterLab with inline support. Thanks for your awesome library!

WinDerek avatar Feb 26 '22 12:02 WinDerek

Sorry, I don't use jupyter at all these days and can't really help there (I'll probably remove the mention of ipykernel.pylab.backend_inline from the docs if that's getting deprecated...). The only thing I can suggest is to use the MPLCAIRO_PATCH_AGG environment variable (also documented in the README; it should be set before matplotlib is imported; also pay attention to the usual problems with envvars and jupyter, e.g. https://stackoverflow.com/questions/37890898/how-to-set-env-variable-in-jupyter-notebook)

anntzer avatar Mar 05 '22 19:03 anntzer