简体   繁体   English

如何绘制来自两个不同数据集的数据

[英]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.datdata_a_200.datdata_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.pngplot_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)并将xy作为新数据。 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.datb.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:一些注意事项:

  • For your case, consider using 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). Enumerate(请参阅文档)给出一个元组,其中包含通过迭代序列(在您的情况下,是一个列表)中获得的当前计数和值。
  • My current approach is not the fastest since it does 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.如果您知道确切的文件名或文件数量,则可以通过仅使用大小写匹配打开确切的文件名来加快速度。
  • I used 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.您可以查看此stackoverflow 响应以了解它的作用。
  • I removed plot.show() since the assumption is that you just want to save the .png files.我删除了plot.show()因为假设您只想保存.png文件。
  • If you don't clear the plot or use a function that in the back-end clears the plot, you will continue to append lines to the same figure, resulting in 400 lines (200 a and 200 b) by the end of your plot.如果您不清除绘图或使用后端清除绘图的函数,您将继续向同一个图形追加线条,从而在绘图结束时产生 400 行(200 a 和 200 b) .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM