【发布时间】:2023-04-02 06:35:01
【问题描述】:
我想使用一种可以显示所有需要的表情符号的字体。我有一个如下所示的数据框:
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。
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) 让表情符号向右旋转,但不知道如何正确设置字体。
【问题讨论】:
-
你能把它设为minimal reproducible example吗? (即包括如何制作数据框,以便有人可以复制您的代码并运行它)。此外,您还没有展示如何将 matplotlib 中的字体设置为您选择的字体。你能把你设置字体的代码行包括进去吗?
-
完全忘记了,现在添加了@tom
标签: python matplotlib fonts