Matplotlib – 用 Python 中的元组元素列表创建一个频率直方图
在 Python 中用元组元素列表制作频率直方图,我们可以采取以下步骤:
- 设置图形大小,调整子图之间和周围的间距。
- 制作元组列表、数据。
- 迭代数据后,制作频率和索引列表。
- 使用 bar() 方法制作条形图。
- 要显示图形,可以使用 show() 方法。
示例
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = [("a", 1), ("c", 3), ("d", 4), ("b", 2), ("e", 7), ("f", 3), ('g', 2)] ind = [] fre = [] for item in data: ind.append(item[0]) fre.append(item[1]) plt.bar(ind, fre) plt.show()
输出
它将产生以下输出:
广告