使用 matplotlib 从 .txt 文件绘制数据
matplotlibpythondata visualization更新于 2025/11/10 4:28:17
要使用 matplotlib 从 .txt 文件绘制数据,我们可以采取以下步骤 −
- 设置图形大小并调整子图之间和周围的填充。
- 初始化 bar_names 和 bar_heights 的空列表。
- 在 read "r" 中打开示例 .txt 文件模式并附加到条形的名称和高度列表中。
- 制作条形图。
- 要显示图形,请使用 show() 方法。
示例
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
bar_names = []
bar_heights = []
for line in open("test_data.txt", "r"):
bar_name, bar_height = line.split()
bar_names.append(bar_name)
bar_heights.append(bar_height)
plt.bar(bar_names, bar_heights)
plt.show()
"test_data.txt" 包含以下数据 −
Javed 12 Raju 14 Rishi 15 Kiran 10 Satish 17 Arun 23
输出
它将产生以下输出


相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

