1
votes

I'm new to coding. I've been tasked to plot a graph using data from multiple data frame. The number of data frames to be used is indefinite (n). The same X & Y-axis will be plotted throughout the data frames. Can you please kindly guide me through this? My mentor has wrote the code for the first part(extracting the data from the track files, and then importing them to Python). I just need to plot the graph.

import subprocess
import pandas as pd
import os
import matplotlib.pyplot as plt
#folder where the .tracks files are
main_path = r"N:\Projects\Misc\Joint geometry analysis"
file_list = [each for each in os.listdir(main_path) if each.endswith('.tracks')]
print (file_list)
opera_app = r"readtrac.exe" #utility for converting track files to ascii fwf
os.chdir( main_path )
#put readtrac.exe and library files into the working directory

utility_path = r"C:\Program Files\Vector Fields\Opera 15R3 x64\bin"
utilities = [opera_app, "libifcoremd.dll", "libmmd.dll"]

for u in utilities:
    f = os.path.join(main_path,u)
    if not os.path.exists(f):
        s = os.path.join(utility_path,u)
        print ( "copy " + s + " " + f)
        status = subprocess.check_output(['copy', s, f], shell=True)
        print("status: ", status.decode('utf-8'))
header = 9 #line where the columns start
widths = [6,16,16,16,16,16,16] # format of the .fwf file - column widths

df = [] #list of data frames

for f in file_list:
    trac_name_ip = f
    trac_name_op = trac_name_ip[:-6] + "fwf"
    print ('converting a track file called '  + trac_name_ip + ' to an ascii fwf file called ' + trac_name_op)
    subprocess.run([opera_app, trac_name_ip, 'B', trac_name_op])  # NB this won't overwrite a .fwf file
    df.append(pd.read_fwf(trac_name_op, header = header, widths = widths)) #read the fwf file into a pandas dataframe
df[0].head()

I know how to plot a basic graph but I don't know what commands I should use to plot a graph with indefinite amount of data frames - lets say I want to plot df[['XP', 'ZP']] of each data frame onto one plot, what should I do? Ideally I want a XY scatter plot, with the name of the each individual dataframes as the name of the series in a legend.

Thanks

1

1 Answers

0
votes

You can use plt.scatter() multiple times to plot many DataFrames into the same figure. While you don't call plt.show() each DataFrame will be displayed using the same axis.

Here is an example of what you can do :

import pandas as pd
import matplotlib.pyplot as plt
import random

n = 4

# Generating DataFrames
df_names = [f'df{i}' for i in range(n)]
data = []

for i in range(n):
    data.append(pd.DataFrame(
        [[random.randint(1, 10), random.randint(1, 10)] for j in
         range(random.randint(1, 10))], columns=["A", "B"]))

# Plot DFDataFrames
for i, df in enumerate(data): # i is DataFrame number and df the DataFrame itself
    plt.scatter(df["A"], df["B"], label=df_names[i])

plt.title("Title")
plt.xlabel("X stuff")
plt.ylabel("Y stuff")
plt.legend()
plt.show()

It will creates a graph as bellow that is, I hope, what you want to do.

enter image description here

If you have a lot of data to display you may want to use subplots in order to have multiple plots on the same image. You can find the documentation (and some examples) about subplots here : https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html