gpt4 book ai didi

python - 如何在 Matplotlib 中绘制带有图例的多个两列文本文件中的数据?

转载 作者:IT老高 更新时间:2023-10-28 22:17:19 26 4
gpt4 key购买 nike

如何打开来自不同目录的多个文本文件并将它们绘制在带有图例的单个图表上?

最佳答案

如果你直接使用 pylab(包含在 matplotlib 中)而不是 matplotlib,这相对简单。从文件名和图例名称列表开始,例如 [ ('name of file 1', 'label 1'), ('name of file 2', 'label 2'), ...]。然后你可以使用类似下面的东西:

import pylab

datalist = [ ( pylab.loadtxt(filename), label ) for filename, label in list_of_files ]

for data, label in datalist:
pylab.plot( data[:,0], data[:,1], label=label )

pylab.legend()
pylab.title("Title of Plot")
pylab.xlabel("X Axis Label")
pylab.ylabel("Y Axis Label")

您可能还想在绘图命令中添加类似 fmt='o' 的内容,以便从线更改为点。默认情况下,matplotlib 和 pylab 在同一个图上绘图而不清除它,所以你可以多次运行 plot 命令。

关于python - 如何在 Matplotlib 中绘制带有图例的多个两列文本文件中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11248812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com