I have 2 dataframes which I combine and then melt with pandas. I need to multi-plot them (as below) and the code needs to be scalable. They consist of 2 variables which form the 'key' column below ('x' and 'y' here), across multiple 'stations' (just 2 here, but needs to be scalable). I've used relplot()
to be able to multi-plot the two variables on each graph, and different stations on separate graphs.
Is there any way to maintain this format but introduce a 2nd y axis to each plot? 'x' and 'y' need to be on different scales in my actual data. I've seen examples where the relplot call is stored with y = 1st variable
, and a 2nd lineplot call is added for the 2nd variable with ax.twinx()
included in it. So in example below, 'x' and 'y' would each have a y axis on the same graph.
How would I make that work with a melted dataframe (e.g. below) where 'key' = 2 variables and 'station' can be length n? Or is the answer to scrap that df format and start again?
The multi-plot as it stands:
import numpy as np
np.random.seed(123)
date_range = pd.period_range('1981-01-01','1981-01-04',freq='D')
x = np.random.randint(1, 10, (4,2))
y = np.random.randint(1, 10, (4,2))
x = pd.DataFrame(x, index = date_range, columns = ['station1','station2'])
y = pd.DataFrame(y, index = date_range + pd.to_timedelta(1, unit="D"), columns = ['station1','station2'])
#keep information where each data point comes from
x["key"], y["key"] = "x", "y"
#moving index into a column
x = x.reset_index()
y = y.reset_index()
#and changing it to datetime values that seaborn can understand
#necessary because pd.Period data is used
x["index"] = pd.to_datetime(x["index"].astype(str))
y["index"] = pd.to_datetime(y["index"].astype(str))
#combining dataframes and reshaping
df = pd.concat([x, y]).melt(["index", "key"], var_name="station", value_name="station_value")
#plotting
fg = sns.relplot(data=df, x = "index", y = "station_value", kind = "line", hue = "key", row = "station")
#shouldn't be necessary but this example had too many ticks for the interval
from matplotlib.dates import DateFormatter, DayLocator
fg.axes[0,0].xaxis.set_major_locator(DayLocator(interval=1))
fg.axes[0,0].xaxis.set_major_formatter(DateFormatter("%y-%m-%d"))
plt.show()
You could relplot
for only one key
(without hue
), then similar to the linked thread, loop the subplots, create a twinx
, and lineplot
the second key
/station
combo:
#plotting
fg = sns.relplot(data=df[df['key']=='x'], x="index", y="station_value", kind="line", row="station")
for station, ax in fg.axes_dict.items():
ax1 = ax.twinx()
sns.lineplot(data=df[(df['key'] == 'y') & (df['station'] == station)], x='index', y='station_value', color='orange', ci=None, ax=ax1)
ax1.set_ylabel('')
Output: