Loading and Plotting Mesa Model Data with Pandas and Matplotlib

Here is the code that solves the problem:

import matplotlib.pyplot as plt
import mesa_reader as mr
import pandas as pd

# load and plot data
h = pd.read_fwf('history.data', skiprows=5, header=None)

# get column names
col_names = list(h.columns.values)
print("The column headers:")
print(col_names)

# print model number value
model_number_val = h.iloc[0]['model_number']
print(model_number_val)

This code uses read_fwf to read the fixed-width file, and sets skiprows=5 to skip the first 5 rows of the file. The header=None argument tells pandas not to assume a header row.

The rest of the code is similar to the original code: it prints the column headers and model number value. Note that I changed the line print(h['model_number']) to print(model_number_val) to access the first row of the data, which should contain the model number value.


Last modified on 2025-01-17