如何绘制一个 emoji 作为 Matplotlib 中条形图的标签?
我们可以使用 annotate() 将表情符号放置在条形图的顶部。
步骤
- 设置图形大小并调整子图之间的填充。
- 制作包含表情符号的频率和标签列表。
- 使用 figure() 方法创建新图形或激活现有图形。
- 使用 bar() 方法绘制条形图。
- 使用 annotate() 方法放置表情符号作为标签
- 使用 show() 方法显示图形。
示例
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True freqs = [7, 8, 5, 3, 6] labels = ['😊', '😲', '😂', '😃', '😛'] plt.figure() p1 = plt.bar(np.arange(len(labels)), freqs) for rect1, label in zip(p1, labels): height = rect1.get_height() plt.annotate( label, (rect1.get_x() + rect1.get_width()/2, height+.05), ha="center", va="bottom", fontsize=30 ) plt.show()
输出

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP