This maybe a silly question, but I have multiple plots redundant looking code all based on one years of time series data from pandas dataframes. Is it possible to combine these into one plot but with each year of data (2018,2019,2020) be a different color? Any tips appreciated not a lot of wisdom here.
g = sns.lmplot(x="CDD",y="kWh",data=df_2018_avg, fit_reg=True)
g.fig.set_size_inches(15,8)
g.fig.suptitle(f'2018 kWh / Day Vs CDD model: R-squared {round(stats_model_2018.rsquared,2)}')
g = sns.lmplot(x="CDD",y="kWh",data=df_2019_avg, fit_reg=True)
g.fig.set_size_inches(15,8)
g.fig.suptitle(f'2019 kWh / Day Vs CDD model: R-squared {round(stats_model_2019.rsquared,2)}')
g = sns.lmplot(x="CDD",y="kWh",data=df_2020_avg, fit_reg=True)
g.fig.set_size_inches(15,8)
g.fig.suptitle(f'2020 kWh / Day Vs CDD model: R-squared {round(stats_model_2020.rsquared,2)}')
CodePudding user response:
You can add an extra column to contain the year and concatenate the dataframes to form one big dataframe. Then you can use hue='year':
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df_2018_avg = pd.DataFrame({'CDD': np.linspace(0, 21, 100),
'kWh': np.linspace(9000, 10500, 100) np.random.normal(0, 500, 100)})
df_2019_avg = pd.DataFrame({'CDD': np.linspace(0, 21, 100),
'kWh': np.linspace(9000, 11000, 100) np.random.normal(0, 500, 100)})
df_2020_avg = pd.DataFrame({'CDD': np.linspace(0, 21, 100),
'kWh': np.linspace(8000, 10000, 100) np.random.normal(0, 500, 100)})
df_2018_avg['year'] = 2018
df_2019_avg['year'] = 2019
df_2020_avg['year'] = 2020
g = sns.lmplot(x="CDD", y="kWh", hue="year",
data=pd.concat([df_2018_avg, df_2019_avg, df_2020_avg]), fit_reg=True)
g.fig.set_size_inches(15, 8)
plt.show()
CodePudding user response:
Append a 'year' column to each dataframe that matches the year of the data.
df_2018_avg['year'] = 2018
df_2019_avg['year'] = 2019
df_2020_avg['year'] = 2020
Then combine each table into one dataframe
df = pd.concat([df_2018_avg, df_2019_avg, df_2020_avg])
Then just add the 'hue' argument in your seaborn function.
g = sns.lmplot(x="CDD",y="kWh",data=df, fit_reg=True, hue='year')
CodePudding user response:
If you accept to use sns.regplot
(axes-level function) instead of sns.lmplot
(figure-level function), it's possible to combine all graphs in one:
Demo:
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
fig, ax = plt.subplots(figsize=(15, 8))
for day, df in tips.groupby('day'):
sns.regplot(x="total_bill", y="tip", data=df, ax=ax, label=day)
ax.legend()
plt.show()