How I got Matplotlib to plot Apple Color Emojis

Your plotting options may be limited.This post investigates why the popular Python library Matplotlib currently lacks support for plotting emojis from the Apple Color Emoji font, and how we can overcome this to get the results we want.The problemAn attempt to plot emojis looks like this:This graph has no meaning other than to demonstrate the appearance of plotted emojis.Although we specified it as the font, these emojis are not Apple Color Emoji..They are from the font DejaVu Sans, the default font that took over after Matplotlib failed to find our specified font in the system.However, if we try to explicitly load Apple Color Emoji using font_manager.FontProperties, it throws the following error:RuntimeError: In FT2Font: Could not set the fontsize.A few of many related questions on Stack Overflow about plotting emojis in Matplotlib:How to plot (high quality) emoji in matplotlib?matplotlib: annotate plot with Emoji labelsEmoji in Matplotlib figures on OS XEmojis in matplotlibs xticksCould not load the emoji font in MatplotlibBafflingly, all remain unanswered..It simply cannot be done with recent versions of Matplotlib..Indeed, a look at Matplotlib’s GitHub repo reveals Issues #3135, #4492, and #12830, the first of which has been open for almost four years.Why Matplotlib cannot plot emojisApple Color Emoji is a TrueType Collection (TTC) file..This format combines multiple fonts into a single file, to save space..FT2Font, the dependency which threw that runtime error we saw earlier, only supports getting the first font from a TTC file..While this might work, limitations of the conversion dependency, ttconv, prevent the backends from actually using this font.There’s a solution coming in the form of pull request #9787..It currently awaits further review.While we wait for the pull request to be merged, there exists an immediate solution in which we can successfully plot emojis using Matplotlib.Immediate solution: Use a third-party backendEnter Mplcairo..A “new, essentially complete implementation of a cairo backend for Matplotlib,” Mplcairo improves on several points in the original backend..Most notably, Mplcairo introduces support for using the first font in a TTC file.After I git cloned the repo onto my machine, it was pretty easy to follow the detailed build and installation instructions..I summarize my process below.BuildingSince wheels are not available for OS X, we need to build the wheels ourselves..To do this, we need the following dependencies:C++ compiler with C++17 support, e.g.. More details

Leave a Reply