I managed to plot all the CSV files in a folder and plot the graphs in one window. But there is just one small thing missing so far. The plots have no title. And as I use the 'Beacon' column to decide which data should be plotted I would also like to have the Beacon name as title of the plot.
my data frame looks like this:
Beacon Average Gradient
Date
2017-06-10 A18 4.5566 1.11280
2017-06-10 B18 5.6694 -0.41125
2017-06-15 A18 4.5528 1.11350
2017-06-15 B18 5.6663 -0.41105
2017-06-15 C17 3.7307 -0.72965
... ... ...
2017-08-16 SB01 3.8360 -0.10900
2017-08-16 SB02 3.7130 -0.13750
2017-08-16 SB03 3.5610 0.13300
2017-08-16 SB04 3.9790 0.08100
2017-08-16 SB05 3.7230 -0.25600
My script looks like this:
import matplotlib.pyplot as plt
import pandas as pd
import glob
import os
import numpy as np
## chose directory
path =r'C:\soc3\data\sb_measurements'
globbed = glob.glob(path "/*.csv")
data = []
## Select and modify all CSV files in directory
for csv in globbed:
df = pd.read_csv(csv, header=0, skiprows=(0), delimiter = ',', names = ['Beacon', 'Go', 'Return', 'Average'])
df['Filename'] = os.path.basename(csv)
data.append(df)
for index in data:
index['Gradient'] = (np.gradient(index['Average']))
df = pd.concat(data, axis=0, ignore_index=True)
df = df.loc[:, df.columns.intersection(['Beacon', 'Average','Filename','Gradient'])]
df = df.dropna()
df = df.replace('.csv','', regex=True)
df['Filename'] = df['Filename'].replace('SB','20', regex=True)
df['Filename'] = df['Filename'].astype('|S')
df['Filename'] = pd.to_datetime(df['Filename'], format='%Y%m%d')
df.rename(columns = {'Filename':'Date'}, inplace = True)
df.set_index('Date', inplace=True)
counted_sets = df['Beacon'].nunique()
df = df.dropna()
grouped = df.groupby('Beacon')
ncols= 7
nrows = 4
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize = (90,30) , constrained_layout = True)
for (key, ax) in zip(grouped.groups.keys(), axes.flatten()):
grouped.get_group(key).plot(ax=ax)
# =============================================================================
# plt.savefig("output.jpg")
# =============================================================================
ax.legend()
plt.show()
print(df)
And these plots look like this:
I hope someone can help me out.
CodePudding user response:
I would say it is enough to provide the key to the title parameter of panda's plot function. Like this: grouped.get_group(key).plot(ax=ax, title=key)