Plotting Several Graphs in python with multiple lines from .dat file

345 Views Asked by At

I am trying to plot a set of data from a .dat file in Python.

I want to create ten different plots, each with seven different lines displayed on them. X-values are a hundred points evenly spaced between 1 and 100. Y values are the 100 points from columns 2:101. Seven lines should be labeled from Column 1.

The format of the .dat file is as follows:

Column 1: The values that I want displayed in my legend, there are 7 different values Columns 2-101: The set of 100 data points that I want plotted for each of the values in column 1

Following this set of seven rows, there is a blank line and then the pattern repeats for a total of ten times (80 lines in the file).

Any help would be very welcome, this file format is much more complex than I am used to dealing with.

1

There are 1 best solutions below

0
On

For X you can use np.linspace(1,100,100)

Your dat.file you can directly load as an array by utilizing np.loadtxt("input.dat") the blank lines will be ignored by default and now that you have everything in an array you can do anything you want with it.

The loadtxt function comes with parameters such as usecols and skiprows, so if you have text labels in your file in the first column or row you can skip over these. (https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html)

for plotting you can use of course import matplotlib.pyplot as plt and then just start to plot the different columns and rows of the array you loaded with loadtxt to your liking :)