如何在Matplotlib中使用图例从多个两列文本文件中绘制数据? [英] How to plot data from multiple two column text files with legends in Matplotlib?

查看:421
本文介绍了如何在Matplotlib中使用图例从多个两列文本文件中绘制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I open multiple text files from different directories and plot them on a single graph with legends?

推荐答案

如果您直接使用pylab(包含在matplotlib中)而不是matplotlib,则这相对简单.首先从文件名和图例名称列表开始,例如[('文件名1','标签1'),('文件名2','标签2',...].然后,您可以使用类似以下的内容:

This is relatively simple if you use pylab (included with matplotlib) instead of matplotlib directly. Start off with a list of filenames and legend names, like [ ('name of file 1', 'label 1'), ('name of file 2', 'label 2'), ...]. Then you can use something like the following:

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'之类的内容添加到plot命令中,以便从一条线变为点.默认情况下,带有pylab的matplotlib会在不清除该图的情况下将其绘制到同一图形上,因此您可以多次运行plot命令.

You also might want to add something like fmt='o' to the plot command, in order to change from a line to points. By default, matplotlib with pylab plots onto the same figure without clearing it, so you can just run the plot command multiple times.

这篇关于如何在Matplotlib中使用图例从多个两列文本文件中绘制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆