[英]How to plot data from two different data sets
I have trawled through some forums trying to solve this but I am new to programming and I just can't seem to figure it out.我浏览了一些试图解决这个问题的论坛,但我是编程新手,我似乎无法弄清楚。
I have two data sets with just 2 columns (x,u)
in data_a*.dat
and (x,v)
in data_b*.dat
.我有两个数据集,在
data_a*.dat
和(x,v)
在data_b*.dat
只有 2 列(x,u)
。 There are 200 files ranging from data_a_001.dat
to data_a_200.dat
and data_b_001.da
t to data_b_200.dat
有 200 个文件,从
data_a_001.dat
到data_a_200.dat
和data_b_001.da
t 到data_b_200.dat
I am trying to create a set of plots plot_001.png
to plot_200.png
such that plot_001
has x,u
from data_a_001.da
t as well as v
from data_b_001.dat
and so on till plot_200.png
我正在尝试创建一组
plot_001.png
到plot_200.png
这样plot_001
有来自data_a_001.da
t 的x,u
以及来自data_b_001.dat
v
等等直到plot_200.png
Thus far I've been using the following code to plot data from single files, but don't know how to get both data files on the same plot.到目前为止,我一直在使用以下代码从单个文件中绘制数据,但不知道如何在同一个图上获取两个数据文件。
import numpy as np
import matplotlib
import math
from matplotlib import pyplot as plt
import glob
data = sorted(glob.glob('data_*'))
i=0
for d in data:
if(i<201):
data = np.genfromtxt(fname=d)
x = data[:,0]
v = data[:,1]
plt.plot(x,v,color='blue')
plt.ylim(-1.5,1.5)
k = str(i)
plt.savefig('plot'+k.zfill(4)+'.png')
plt.close()
i = i + 1
matplotlib.pyplot.show()
I don't mind modifying the code or just trying something new to solve the problem.我不介意修改代码或者只是尝试一些新的东西来解决问题。
To create a second line that overlays the first line on the same plot, use plot.plot(x,y)
again with x
and y
as your new data.要创建覆盖同一图上第一条线的第二条线,请再次使用
plot.plot(x,y)
并将x
和y
作为新数据。 Matplotlib will append it to the figure and handle the rest. Matplotlib 会将其附加到图形并处理其余部分。
Below is an example on how to create N images with each containing a plot of your data from a.dat
and b.dat
.下面是一个关于如何创建 N 个图像的示例,每个图像都包含来自
a.dat
和b.dat
数据b.dat
。
import numpy as np
import matplotlib
import math
from matplotlib import pyplot as plt
import glob
files_a = sorted(glob.glob('data_a*'))
files_b = sorted(glob.glob('data_b*'))
for i, d in enumerate(files_a):
# get file and plot from data_a_i.png
data_a = np.genfromtxt(fname=d)
x = data_a[:,0]
u = data_a[:,1]
plt.plot(x,u,color='blue')
# get file and plot from data_b_i.png
data_b = np.genfromtxt(fname=files_b[i])
x = data_b[:,0]
v = data_b[:,1]
plt.plot(x,v,color='red')
# format plot and save
plt.ylim(-1.5,1.5)
k = str(i)
plt.savefig('plot'+k.zfill(4)+'.png')
# clear figure, can be re-used for other plots
plt.clf()
Some things to note:一些注意事项:
enumerate
rather than just an item in for
loop.enumerate
而不仅仅是item in for
循环中的item in for
。 Enumerate (see documentation ) gives a tuple containing the current count and value obtained from iterating over a sequence (in your case, a list). glob.glob
twice (for both files A and B).glob.glob
两次(对于文件 A 和 B)。 If you know the exact file names or number of files, you can definitely make it faster by opening the exact file names with just case-matching.plt.clf()
to clear the figure created so it can be re-used again.plt.clf()
清除创建的图形,以便可以再次使用。 You can take a look at this stackoverflow response for what it does.plot.show()
since the assumption is that you just want to save the .png
files.plot.show()
因为假设您只想保存.png
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.