'How to obtain monthly plots from a dataframe containing information over a year

I have 12 datasets, one dataset for each month of the same year. The 12 datasets are formatted in the same way.
I have successfully concatenated the 12 datasets in a single dataset.
Then I have converted the Date column into date format to allow operations with the dates.

I've tried to run a FOR loop to iterate over the months to obtain 12 plots in a range from 1 to 13.

# Let's read and concatenate the dataframes
all_files = sorted(glob.glob('PV00004*.csv'))

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True) 

# Let's ensure the Date column is in the date format
frame['Date'] = pd.to_datetime(frame['Date'])


# Let's run a FOR loop to iterate across 12 months to obtain one lineplot for
# each month

for i in range(1, 13):
    plt.xticks(rotation= 90)
    sns.set(rc = {'figure.figsize':(20,6)})
    sns.lineplot(data= frame[frame['Date'].dt.month.isin([i])], 
    x= 'Time', y= 'kWh', ci=None)

What I get is one single chart with 12 line plots.

enter image description here

Instead, I would like to get 12 individual lineplots - possibly using Seaborn - one for each month.

Thanks



Solution 1:[1]

Solution: Instead of using a FOR Loop, I have added the month column and created the plots.

# Let's read and concatenate the dataframes
all_files = sorted(glob.glob('PV00004*.csv'))

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True) 

# Let's ensure the Date column is in the date format
frame['Date'] = pd.to_datetime(frame['Date'])

# Let's add the column Month that extracts the month value frome the Date column
frame.insert(2, 'Month', frame['Date'].dt.month, True)

# Visualisation
dfp = frame.pivot_table(index='Time', columns='Month', values='kWh')
ax = dfp.plot(kind='line', subplots=True, figsize=(20, 30), layout=(6, 2), rot=90, legend=True)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luke Hill