-1

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. There are 200 files ranging from data_a_001.dat to data_a_200.dat and data_b_001.dat to 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.dat as well as v from data_b_001.dat and so on till 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.

Simo
  • 955
  • 8
  • 18
  • Welcome to StackOverflow! It will be very helpful if you provide some sample of your data, your resulting plot and what plots you're trying to get. – Teoretic Sep 13 '18 at 19:39
  • I'm not sure I understand the question properly, but generally if you just do `plt.plot(x1,y1); plt.plot(x2,y2); plt.show()`, then both data sets appear in the resulting plot. – aquirdturtle Sep 13 '18 at 19:41

1 Answers1

0

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. Matplotlib will append it to the figure and handle the rest.

Below is an example on how to create N images with each containing a plot of your data from a.dat and 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 (see documentation) gives a tuple containing the current count and value obtained from iterating over a sequence (in your case, a list).
  • My current approach is not the fastest since it does glob.glob twice (for both files A and 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. You can take a look at this stackoverflow response for what it does.
  • I removed plot.show() since the assumption is that you just want to save the .png files.
  • 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.
Kai
  • 234
  • 3
  • 11