mplcairo
mplcairo copied to clipboard
Jupyter Assertion Error / AttributeError: can't set attribute 'renderer'
I am following Brienna's Gist to plot emojis in matplotlib, which has the following code:
# Set the backend to use mplcairo
import matplotlib, mplcairo
print('Default backend: ' + matplotlib.get_backend())
matplotlib.use("module://mplcairo.macosx")
print('Backend is now ' + matplotlib.get_backend())
# IMPORTANT: Import these libraries only AFTER setting the backend
import matplotlib.pyplot as plt, numpy as np
from matplotlib.font_manager import FontProperties
# Load Apple Color Emoji font
prop = FontProperties(fname='/System/Library/Fonts/Apple Color Emoji.ttc')
# Set up plot
freqs = [301, 96, 53, 81, 42]
labels = ['😊', '😱', '😂', '😄', '😛']
plt.figure(figsize=(12,8))
p1 = plt.bar(np.arange(len(labels)), freqs, 0.8, color="lightblue")
plt.ylim(0, plt.ylim()[1]+30)
# Make labels
for rect1, label in zip(p1, labels):
height = rect1.get_height()
plt.annotate(
label,
(rect1.get_x() + rect1.get_width()/2, height+5),
ha="center",
va="bottom",
fontsize=30,
fontproperties=prop
)
plt.show()
When running this in jupyter notebook, i get the following error:
python[13573:53170] *** Assertion failure in +[NSEvent otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:], NSEvent.m:647
When running this in a python script I get:
Default backend: MacOSX
Backend is now module://mplcairo.macosx
Traceback (most recent call last):
File "/opt/homebrew/Caskroom/miniconda/base/envs/proj/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 1226, in _on_timer
ret = func(*args, **kwargs)
File "/opt/homebrew/Caskroom/miniconda/base/envs/proj/lib/python3.10/site-packages/matplotlib/backends/backend_macosx.py", line 68, in callback_func
callback()
File "/opt/homebrew/Caskroom/miniconda/base/envs/proj/lib/python3.10/site-packages/matplotlib/backends/backend_macosx.py", line 88, in _draw_idle
self.draw()
File "/opt/homebrew/Caskroom/miniconda/base/envs/proj/lib/python3.10/site-packages/mplcairo/base.py", line 233, in draw
super().draw()
File "/opt/homebrew/Caskroom/miniconda/base/envs/proj/lib/python3.10/site-packages/matplotlib/backends/backend_macosx.py", line 50, in draw
super().draw()
File "/opt/homebrew/Caskroom/miniconda/base/envs/proj/lib/python3.10/site-packages/matplotlib/backends/backend_agg.py", line 394, in draw
self.renderer = self.get_renderer()
AttributeError: can't set attribute 'renderer'
I am on a Mac (M2) and mplcairo.get_versions() gives:
{
'python': '3.10.8 (main, Nov 24 2022, 08:08:27) [Clang 14.0.6 ]',
'mplcairo': '0.5',
'matplotlib': '3.7.2',
'cairo': '1.16.0',
'freetype': '2.12.1',
'pybind11': '2.11.1',
'raqm': None,
'hb': None
}
Can you try at least the python script with matplotlib 3.8 (which has just been released)? I do see that there's an issue with 3.7.2, though I haven't investigated the cause yet.
For the notebook integration, I think this needs to be reported to matplotlib itself, as even a simple
import matplotlib as mpl; mpl.use("macosx")
from matplotlib import pyplot as plt
plt.plot()
(i.e. using matplotlib's own macosx backend, which mplcairo still relies on) in a notebook seems to crash with the same error you report. On the other hand you don't really need to use module://mplcairo.macosx; module://mplcairo.tk or module://mplcairo.qt (if you have pyqt6 installed) should work just fine.
Thanks for your swift reply!
Just tried with matplotlib 3.8 and it seems to be the same issue, both as script and in the notebook.
Ah yes, I remember the problem now. For the macosx backend you'll need to install mplcairo from HEAD (pip install git+https://github.com/matplotlib/mplcairo); alternatively, as noted above, you can use one of the other backends such as module://mplcairo.qt or module://mplcairo.tk.