4

I've created a bar plot of emoji frequencies using matplotlib in Jupyter Notebook. I've also tested this in the Terminal.

enter image description here

Code:

freqs = [90, 29, 5, 29, 15]
labels = [, , , , ]
# Plot the figure
plt.figure(figsize=(12, 8))
ax = pd.Series(freqs).plot(kind='bar', color='pink', width=0.8)
ax.set_title('Most Frequently Used Emojis')
ax.set_ylabel('Frequency', fontsize=15)
ax.set_xlabel('Emojis', fontsize=15)
plt.tick_params(
    axis='x',
    which='both',
    bottom=False,
    top=False,
    labelbottom=False
)

new_ylim = ax.get_ylim()[1]+30
ax.set_ylim((0, new_ylim))

rects = ax.patches

# Make labels
for rect, label in zip(rects, labels):
    height = rect.get_height()
    plt.annotate(
        label,
        (rect.get_x() + rect.get_width()/2, height+5),
        ha="center",
        va="bottom",
        fontsize=30
    )

plt.show()

As you can see, the emojis don't show up.

After adding

import matplotlib.font_manager as mfm
emoji_font = mfm.FontProperties(fname="/System/Library/Fonts/Apple Color Emoji.ttc")

and fontproperties = emoji_font as an option in plt.annotate, it throws RuntimeError: In FT2Font: Could not set the fontsize. The figure will not display.

How can I get emojis to show in a bar plot in Jupyter Notebook?

If I use a font such as Symbola, the emojis seem like they will look like this:

enter image description here

I don't want these emojis. Nor do I want to load images. They should be the real Apple emojis.

Other questions that are still unanswered or are answered using the incorrect emoji depiction from above:

None of them get the Apple emojis onto the graph. Why can't this happen? Am I missing something? Is it possible with another plotting library?

brienna
  • 1,415
  • 1
  • 18
  • 45
  • Currently your code does not include any emojis. It also does not use a font that would be capable of showing them. If you think none of the other cases work for you, make sure to have a [mcve] and the full error it produces inside the question. – ImportanceOfBeingErnest Nov 18 '18 at 11:29
  • However, judging from [this thread](https://github.com/matplotlib/matplotlib/issues/4492/), it's not possible to save an image with the apple color emojis with matplotlib. The codes from that thread might work in jupyter using the `%matplotlib notebook` backend though. – ImportanceOfBeingErnest Nov 18 '18 at 11:43
  • In conclusion, you may decide to use png images of the emojis and place them at the positions you want them to appear. Similar to [this question](https://stackoverflow.com/questions/9285159/pyplot-annotate-with-image-png-or-numpy-array-instead-of-text). – ImportanceOfBeingErnest Nov 18 '18 at 11:49
  • Or [this blogpost](http://catherineh.github.io/programming/2017/10/24/emoji-data-markers-in-matplotlib). – ImportanceOfBeingErnest Nov 18 '18 at 12:15
  • @ImportanceOfBeingErnest updated my question to include bar plot data and an attempt to set the font. I'm really hoping I don't need to load pngs, because I want my code to be as standalone as possible. I just don't understand why it doesn't work if the emojis are right there in the markdown cell if I print `labels` :( – brienna Nov 18 '18 at 16:49
  • The crucial sentence in [this thread](https://github.com/matplotlib/matplotlib/issues/4492/) is *"I suspect the OS is supplying the emoji images through the GUI framework"*. This somehow works by accident; the font rendering for saving files would not be able to use those Glyphs. This in turn would also mean that you have little chance of getting this to work in a notebook, if `%matplotlib notebook` doesn't work for you. – ImportanceOfBeingErnest Nov 18 '18 at 17:01
  • Is this GUI framework you're referring to in the notebook itself or in matplotlib? I've tried my code in the Terminal with the same results – brienna Nov 18 '18 at 17:58
  • That would be the GUI window you get via the console. But it may only work when using the "macosx" backend. – ImportanceOfBeingErnest Nov 18 '18 at 18:20
  • In Terminal, it's macosx backend... yet it doesn't work. :/ I'm using Anaconda. I'm still looking through this. – brienna Nov 18 '18 at 18:24
  • Also mind that something that works by coincidence in a particular version of matplotlib is not guaranteed to still work in newer versions. – ImportanceOfBeingErnest Nov 18 '18 at 18:40
  • I've summarized my solution in this post: https://towardsdatascience.com/how-i-got-matplotlib-to-plot-apple-color-emojis-c983767b39e0 – brienna Jun 16 '21 at 17:29

1 Answers1

1

i have created a small library (imojify) to deal with that issue

from imojify import imojify
from matplotlib import pyplot as plt 
from matplotlib.offsetbox import OffsetImage,AnnotationBbox
def offset_image(cords, emoji, ax):
    img = plt.imread(imojify.get_img_path(emoji))
    im = OffsetImage(img, zoom=0.08)
    im.image.axes = ax
    ab = AnnotationBbox(im, (cords[0], cords[1]),  frameon=False, pad=0)
    ax.add_artist(ab)



emjis = ['', '', '', '','', '', '']
values =[30, 50, 15, 29, 15, 50, 12]

fig, ax = plt.subplots(figsize=(12,8))
ax.bar(range(len(emjis)), values, width=0.5,align="center")
ax.set_xticks(range(len(emjis)))
ax.set_xticklabels([])
ax.tick_params(axis='x', which='major', pad=26)
ax.set_ylim((0, ax.get_ylim()[1]+10))

for i, e in enumerate(emjis):
    offset_image([i,values[i]+5], e, ax)

the library contains images for all emojis, imojify.get_img_path(emoji) simply returns the path of the emoji image then you can use OffsetImage to add these images as labels