34

I have the following dictionary:

a = {'❤': 10, '‍‍‍': 23, '': 13, '': 10, '': 13}

I want to plot the emojis as a bar, and draw them on the bar. At first I did like here (with annotate), but it looks bad, and it doesn't support some emojis.

import matplotlib.pyplot as plt
ax = plt.subplot(111)
ax.bar(range(1,6), a.values())
for label, x, y in zip(a.keys(), range(1,6), a.values()):
    plt.annotate(
        label, 
        xy=(x, y), xytext=(10,10),
        textcoords='offset points', ha='right', va='bottom',
        bbox=dict(boxstyle='round,pad=0.5', alpha=0),
        fontname='Segoe UI Emoji',
        fontsize=20)

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.set_xticks([])
plt.show()

As I said, looks bad:

enter image description here

How can I plot the emojis such that they will look good with matplotlib?

The best option would be using a different font in matplotlib that will support those emojis (I tried using some different values for plt.rcParams['font.family'] with no success), but if it doesn't exists images would work too (but how?)

I don't want to start web scraping the images and labeling them (because I'm pretty sure someone already did it).

I'm using python 3, Spyder IDE, matplotlib version 2.0.2 with anaconda.

Any suggestions?

sheldonzy
  • 5,505
  • 9
  • 48
  • 86
  • 6
    yeah, you need to use a font that supports them. – juanpa.arrivillaga Nov 03 '17 at 19:16
  • Thanks. I used the font and edited my question. – sheldonzy Nov 05 '17 at 13:35
  • 1
    http://catherineh.github.io/programming/2017/10/24/emoji-data-markers-in-matplotlib – ImportanceOfBeingErnest Jun 06 '18 at 21:39
  • 1
    This may be solved here: https://github.com/matplotlib/matplotlib/issues/4492/ also see [this question](https://stackoverflow.com/questions/30049760/emoji-in-matplotlib-figures-on-os-x) – ImportanceOfBeingErnest Jun 06 '18 at 21:42
  • Hi @sheldonzy.. I am also facing this issue! do you have any live example? that I can refer!. thanks! – Shivali Patel Jun 25 '18 at 15:44
  • No, sorry. Check what the guy above you posted. – sheldonzy Jun 25 '18 at 16:55
  • 3
    This question should not be closed, the links given from it are old and not straightforward to follow, it's not clear that it's a duplicate at all. – baxx May 01 '20 at 18:32
  • 3
    @baxx Yeah I agree. I keep getting upvotes on this question, so I'm guessing people are getting to this question enough. I voted to reopen this question. – sheldonzy May 02 '20 at 13:29
  • @ImportanceOfBeingErnest assuming that you originally voted to close this question - I think you were wrong to do so. – baxx May 02 '20 at 13:33
  • I don't think I voted to close this, but honestly I don't recall what I did 2,5 years ago. In any case you could at least edit the question to show in how far existing solutions don't work for you - always under the condition that you use a font that *should actually* contain the symbol you want to use. – ImportanceOfBeingErnest May 02 '20 at 15:26
  • 1
    @ImportanceOfBeingErnest The point is that they're not existing solutions? They're a mish mash of kind of related things - so it's not right for someone to flag as a duplicate. The only thing that I could think of that might improve this is if it was explicitly about emoji, rather than images... I might just post that myself thinking about it – baxx May 03 '20 at 12:56
  • 1
    @baxx So the reason there are rectangles instead of symbols in the question is that those symbols do not exist in the font chosen. So I think the duplicates answer that sufficiently? – ImportanceOfBeingErnest May 04 '20 at 09:12
  • @ImportanceOfBeingErnest I'll try going through it this weekend when I have more time, because I was previously interested in this, found this thread, and didn't find the links that clear to be honest. What you say about fonts makes sense, if I'm able to easily get this working then I'll suggest an edit to this OP I guess (not sure what the best thing to do is), as it comes up in google, and wasn't clear to solve for me at least when I previously looked. Thanks – baxx May 04 '20 at 11:11
  • I heard that you needed the mplcairo backend installed which updates support for colored emoji's, I have done that, but so far I haven't been able to figure this out, it's either giving me black and white emoji's or it's erroring. – Veggiet Sep 17 '20 at 16:46

0 Answers0