python - matplotlibs xticks 中的表情符号

标签 python matplotlib fonts

我想使用一种可以显示所需所有表情符号的字体。我有一个如下所示的数据框:

   chars   num
0      😳   295
1      ☺   365
2      😉   401
3      ♥   426
4      😁   461
5      😄   488
6      🙈   499
7      😍   644
8      😊   691
9      ❤   950
10     😂  1328

我想绘制表情符号的数量,而表情符号位于 xticks 中。我想在这里使用这种字体:Twemoji Color我已经安装了。我设法让它与标题、x_label、y_label 一起工作,但不能与 this solution 的 x_ticks 一起工作。 .

以下代码会生成此图像: 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()

目前我正在执行以下操作:

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()

我现在知道我可以使用axis.set_xticklabels(df.chars.tolist(), rotation=0)让表情符号向右旋转,但不知道如何正确设置字体。

这是输出:

最佳答案

您链接的 Twemoji Color 页面上显示“目前只有 FireFox 支持 SVGinOT 颜色表情符号”。

您最好使用另一个渲染为 html 的绘图库并使用 FireFox 来查看它。

关于python - matplotlibs xticks 中的表情符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51989729/

相关文章:

fonts - 如何在 JavaFx 2.2 中嵌入 .ttf 字体?

javascript - fillText 返回 0 后获取 Canvas 像素

python - 如何调试 python - C++ 程序

python - Windrose 图例括号格式和位置

python - for循环调用类属性报错

python - 带有 alpha 的 matplotlib Circle patch 产生边缘和面色的重叠

python - 如何绘制具有可缩放半径的圆?

android - 为 ListView 设置字体

Python:chrome.exe 的通用 webbrowser.get().open() 不起作用

python - 为什么 pygame.midi 不能在 Macos 上运行,而 pygame.mixer 可以