3

I'm using Python 3.4 in macOS. Matplotlib is supposed to support Unicode in labels, but I'm not seeing Emojis rendered properly.

import matplotlib.pyplot as plt
# some code to generate `data` and `labels`...
plt.clf()
plt.scatter(data[:, 0], data[:, 1], c=col)
# disclaimer: labeling taken from example http://stackoverflow.com/questions/5147112/matplotlib-how-to-put-individual-tags-for-a-scatter-plot
for label, x, y in zip(labels, data[:, 0], data[:, 1]):
    plt.annotate(
        label, # some of these contain Emojis
        xy=(x, y), xytext=(-20, 20),
        textcoords='offset points', ha='right', va='bottom',
        bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
        arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))
plt.show(False)

result

A few of the old pre-Unicode Emojis show up in their old style, but the rest (in this example, "fire," "music," and others) don't. Is there a trick to make these appear properly?

sudo
  • 5,604
  • 5
  • 40
  • 78

1 Answers1

5

You problem here is that the default font have no good support for emojis.

In plt.annotate function, you can add a parameter fontname to specify the typeface that has a good support for emojis.

Following code are what I got on my Windows machine with some edits to your code, it seems that "Segoe UI Emoji" has been installed on my computer already.

# this line is for jupyter notebook
%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np
# config the figure for bigger and higher resolution
plt.rcParams["figure.figsize"] = [12.0, 8.0]
plt.rcParams['figure.dpi'] = 300
data = np.random.randn(7, 2)
plt.scatter(data[:, 0], data[:, 1])
labels = '        ☺️  '.split()
print(labels)
for label, x, y in zip(labels, data[:, 0], data[:, 1]):
    plt.annotate(
        label, # some of these contain Emojis
        xy=(x, y), xytext=(-20, 20),
        textcoords='offset points', ha='right', va='bottom',
        bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
        arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'),
        fontname='Segoe UI Emoji', # this is the param added
        fontsize=20)
plt.show()

Here is what I got, the emojis may not show clearly, it depends on your typeface: enter image description here

Feishi
  • 659
  • 4
  • 12
  • Thanks, that answers the question. Sadly, it's not working out of the box in OS X, so for now I'm using Bokeh (which has its pros and cons) to plot this graph. I searched and found the OSX-specific caveats here: https://github.com/matplotlib/matplotlib/issues/4492 – sudo Apr 16 '17 at 05:02
  • Sorry that this bug still unsolved. Maybe sometime you can try another developing platform. When some thing didn't work well in my Windows, I'll try in on Linux. – Feishi Apr 17 '17 at 00:26
  • Yes, Linux tends to be the best supported platform when it comes to these things. I use a Linux VM but without a display, preferring to keep GUI things on the Mac side. Even with an Ubuntu Desktop installation, I'd have to go find a font that supports the very latest Emojis. – sudo Apr 17 '17 at 08:51