2

I want to loop over two files and plot the first column versus the second column of each file next to each other. I write the script below to do that, but after running, the two diagrams are plotted in one figure, while I want to plot them in two separate diagrams next to each other.

Can someone help me out with how to fix my code regarding my purpose?

def plot_en(filename):

with open("%s.en" %filename,"r") as g_in:
    t=[]
    for line in g_in:
        t.append([ float(x) for x in line.split()])
        column1 = [ x[0] for x in t]
        column2 = [ x[1] for x in t]
        plt.plot(column1,column2)        
plot_en("tempor1")
plot_en("tempor2")

for example, consider files tempor1.en and tempor2.en are:

tempor1.en:

500  1.1
550  2.1
600  2.2
650  3.1
700  3.9

tempor2.en:

500  3.1
550  3.5
600  3.8
650  4.0
700  4.9

using the above python command I got two diagrams plotted on each other, like this: enter image description here

but I want to have separate diagrams for each of the tempor1.en and tempor2.en files right next to each other.

amirre
  • 45
  • 6

1 Answers1

1

What about the following, i.e. calling the function just once and provide a list having the name of the two files of interest:

import matplotlib.pyplot as plt
import numpy as np


def plot_en(filename):

    data_1 = np.loadtxt(f"{filename[0]}.en")
    data_2 = np.loadtxt(f"{filename[1]}.en")

    fig, (ax1, ax2) = plt.subplots(1, 2)
    ax1.plot(data_1[:, 0], data_1[:, 1])
    ax1.title.set_text(f"{filename[0]} Data")

    ax2.plot(data_2[:, 0], data_2[:, 1])
    ax2.title.set_text(f"{filename[1]} Data")


plot_en(["tempor1", "tempor2"])

enter image description here

blunova
  • 2,122
  • 3
  • 9
  • 21
  • Thanks. I have a large number of data, to avoid a dense diagram how can I decrease line weight in your code? or is there any other way, except changing lineweight, to increase the accuracy of diagrams for a huge input data? – amirre Jun 29 '22 at 23:30
  • For decreasing the linewidth just type: `ax1.plot(data_1[:, 0], data_1[:, 1], linewidth=0.3)` – blunova Jun 30 '22 at 06:40