mplcairo: segmentation fault on macOS in drawing text using mplcairo

The script I try to reproduce is

import faulthandler; faulthandler.enable()
# Set the backend to use mplcairo
import platform
print(platform.mac_ver())
import matplotlib, mplcairo
print(mplcairo.get_versions())
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
# plt.set_loglevel("debug")
from matplotlib.font_manager import FontProperties

# Load Apple Color Emoji font 
prop = FontProperties(fname='./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()

plt.savefig("e.png")

The output I got is:

('10.15.1', ('', '', ''), 'x86_64')
{'python': '3.7.4 (default, Sep  7 2019, 18:27:02) \n[Clang 10.0.1 (clang-1001.0.46.4)]', 'mplcairo': '0.2', 'matplotlib': '3.1.1', 'cairo': '1.16.0', 'freetype': '2.6.1', 'pybind11': '2.4.2'}
Default backend: MacOSX
Backend is now module://mplcairo.macosx
Fatal Python error: Segmentation fault

Current thread 0x0000000108a99dc0 (most recent call first):
  File "/usr/local/lib/python3.7/site-packages/matplotlib/text.py", line 291 in _get_layout
  File "/usr/local/lib/python3.7/site-packages/matplotlib/text.py", line 890 in get_window_extent
  File "/usr/local/lib/python3.7/site-packages/matplotlib/axis.py", line 1150 in <listcomp>
  File "/usr/local/lib/python3.7/site-packages/matplotlib/axis.py", line 1150 in _get_tick_bboxes
  File "/usr/local/lib/python3.7/site-packages/matplotlib/axis.py", line 1205 in draw
  File "/usr/local/lib/python3.7/site-packages/matplotlib/artist.py", line 38 in draw_wrapper
  File "/usr/local/lib/python3.7/site-packages/matplotlib/image.py", line 135 in _draw_list_compositing_images
  File "/usr/local/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2647 in draw
  File "/usr/local/lib/python3.7/site-packages/matplotlib/artist.py", line 38 in draw_wrapper
  File "/usr/local/lib/python3.7/site-packages/matplotlib/image.py", line 135 in _draw_list_compositing_images
  File "/usr/local/lib/python3.7/site-packages/matplotlib/figure.py", line 1709 in draw
  File "/usr/local/lib/python3.7/site-packages/matplotlib/artist.py", line 38 in draw_wrapper
  File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 211 in _get_cached_or_new_renderer
  File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 219 in get_renderer
  File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 319 in _get_fresh_straight_rgba8888
  File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 337 in print_png
  File "/usr/local/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2082 in print_figure
  File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 236 in print_figure
  File "/usr/local/lib/python3.7/site-packages/matplotlib/figure.py", line 2180 in savefig
  File "/usr/local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 722 in savefig
  File "emojis_plotted_complete_code.py", line 40 in <module>
[1]    14366 segmentation fault  python3 emojis_plotted_complete_code.py

Some modification I made different from issue17 is:

  • I moved Apple Color Emoji.ttc to cwd but to avoid some permission restriction, but it does not help.
  • I tested on using plt.set_loglevel("debug") but the output is not helpful so I comment it.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 27 (2 by maintainers)

Most upvoted comments

Yes, it will be autodeleted, you should replace /tmp/testenv by wherever you want to put it in your filesystem.

Thanks for clarifying. I agree none of this is really related to mplcairo directly, but I’m happy to hear that everying worked out fine at the end.

What happens with e.g.

conda create -p /tmp/testenv -c conda-forge cairo=1.16

There is clearly a conda-forge cairo 1.16 available (including for macos) at https://anaconda.org/conda-forge/cairo/files?version=1.16.0 so figuring out how to get it installed for you may be the easiest solution.

  • Does this happen with just plain text with the default font? with mplcairo master?
  • Do things work (excluding color emojis) when everything is installed from conda? (with cairo 1.14? with cairo 1.16 from conda-forge?)
  • Would you be able to try building freetype and mplcairo with debug symbols and provide a detailed backtrace?