I have 2 timeseries dataframes which were sourced from two 2-dimensional arrays. The structure of these dataframes is:
Generate example dataframes
import pandas as pd
import numpy as np
date_range = pd.period_range('1981-01-01','1981-01-04',freq='D')
x = np.arange(8).reshape((4,2))
y = np.arange(8).reshape((4,2))
x = pd.DataFrame(x, index = date_range, columns = ['station1','station2'])
y = pd.DataFrame(y, index = date_range, columns = ['station1','station2'])
print(x)
station1 station2
1981-01-01 0 1
1981-01-02 2 3
1981-01-03 4 5
1981-01-04 6 7
The Aim
I want to generate a multi-plot where the values of 'x' and 'y' are plotted as lines on the same graphs, with x and y split by colour, but have multiple 'rows' of graphs of each station. Using above example code, each separate graph would be plotting the different station columns.
What I've tried
I tried the seaborn route: first concatenating the two dataframes together - each df represents a variable, so I added them as keys to name these variables after concatenation. Then I used melt to be able to multi-plot them:
df = pd.concat([x , y], keys = ['Var1', 'Var2'])
meltdf = df.melt(var_name = 'Station', value_name = 'Value', ignore_index = False)
print(meltdf)
Station Value
Var1 1981-01-01 station1 0
1981-01-02 station1 2
1981-01-03 station1 4
1981-01-04 station1 6
Var2 1981-01-01 station1 0
1981-01-02 station1 2
1981-01-03 station1 4
1981-01-04 station1 6
Var1 1981-01-01 station2 1
1981-01-02 station2 3
1981-01-03 station2 5
1981-01-04 station2 7
Var2 1981-01-01 station2 1
1981-01-02 station2 3
1981-01-03 station2 5
1981-01-04 station2 7
I want to plot the values of Var1 & Var2 as lines on the same graph for station1, same for station2 and so on. I want to keep the date as index as these should be timeseries plots, with 'date' along the x axis. I tried this non-working code (for example):
import seaborn as sns
sns.relplot(data=df, x = 'Var1', y = 'Var2', kind = 'line', hue = 'keys', row = 'Station')
Should I 'double melt' the dfs instead to have variable type as its own col? The concat + keys step seems incorrect.