'python plotting from two different files

I have two files, named "data1.dat" and "data2.dat". I want to take first column of "data1.dat" as xlabel and third column of "data2.dat" as ylabel and make a plot. How can I do that? Help please.



Solution 1:[1]

You can read both files and store the required column data in numpy arrays as follows :

import numpy as np
import matplotlib.pyplot as plt

with open('data1.dat','r') as f1:
    x=np.genfromtxt(f1) . # I suppose your data1 file has 1 column
with open('data2.dat','r') as f2:
    y=np.genfromtxt(f2)
    y=y[:,2]  # I only the third column

# plot
plt.figure()
plt.plot(x,y)
plt.show()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lefloxy