1

I would like to use a font which can display all emojis needed. I have got a dataframe which looks like this:

   chars   num
0         295
1      ☺   365
2         401
3      ♥   426
4         461
5         488
6         499
7         644
8         691
9      ❤   950
10       1328

I want to plot the number of emojis, while the emojis are in the xticks. I wanted to use this font here: Twemoji Color which I have installed already. I managed to get it working with the title, x_label, y_label but not the x_ticks with this solution.

Tge following code results in this image: enter image description here

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from matplotlib import ft2font
from matplotlib.font_manager import ttfFontProperty
import pandas as pd

fpath = '/usr/share/fonts/Twitter Color Emoji/TwitterColorEmoji-SVGinOT.ttf'
fprop = fm.FontProperties(fname=fpath)

font = ft2font.FT2Font(fpath)
fprop = fm.FontProperties(fname=fpath)

ttfFontProp = ttfFontProperty(font)


fontprop = fm.FontProperties(family='sans-serif',
                            fname=ttfFontProp.fname,
                            size=fontsize,
                            stretch=ttfFontProp.stretch,
                            style=ttfFontProp.style,
                            variant=ttfFontProp.variant,
                            weight=ttfFontProp.weight)

emojis = ['', '☺', '', '♥', '', '', '', '', '', '❤', '']
nums = [295, 365, 401, 426, 461, 488, 499, 644, 691, 950, 1328]
df = pd.DataFrame({'chars': emojis, 'num': nums})

axis = df.plot.bar()
axis.set_title(' '.join(emojis), fontproperties=prop)
axis.set_xticklabels(df.chars.tolist(), rotation=0, fontsize=5)
plot.show()

Currently I am doing the following:

import matplotlib.pyplot as plt
import pandas as pd

emojis = ['', '☺', '', '♥', '', '', '', '', '', '❤', '']
nums = [295, 365, 401, 426, 461, 488, 499, 644, 691, 950, 1328]
df = pd.DataFrame({'chars': emojis, 'num': nums})
axis = x.plot.bar()
plt.show()

I know now, that I can use axis.set_xticklabels(df.chars.tolist(), rotation=0) to get the emojis rotated right, but not how to set the font properly.

And this is the output:

Spark Monkay
  • 422
  • 4
  • 18
  • Can you make this a [MCVE]? (i.e. include how to make the dataframe so someone could copy your code and run it). Also, you haven't shown how you set the font in matplotlib to your chosen font. Could you include the line of code where you set the font? – tmdavison Aug 23 '18 at 16:14
  • Totally forgot about that, have added it now @tom – Spark Monkay Aug 23 '18 at 17:44

2 Answers2

2

It says on the Twemoji Color page that you linked that "Only FireFox supports the SVGinOT color emoji for now".

You might be better off using another plotting library that renders to html and use FireFox to view it.

Felipe Lema
  • 2,700
  • 12
  • 19
2

When you set the title, you are setting the font properties to the font you want to use using fontproperties=prop.

But, when you set the xticklabels, you are not specifying the font.

Try doing the following:

axis.set_xticklabels(df.chars.tolist(), rotation=0, fontsize=5, fontproperties=prop)
tmdavison
  • 64,360
  • 12
  • 187
  • 165