Reputation: 491
I have a pandas dataframe with different columns, one being year
and another being doy
. I would like to loop over the years (2001 to 2019) and plot the doy
in different seaborn countplots for each year.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv("file.csv")
print(df.head())
ID doy Year ... NA_L1NAME B_year number
1772 1774 199 2001 ... 1.1 1934.0 7.942371e+06
1786 1788 160 2002 ... 1.1 1953.0 2.146587e+05
1792 1794 199 2003 ... 1.2 1960.0 2.017792e+07
1801 1803 199 2004 ... 1.3 1968.0 4.293173e+05
1802 1804 195 2005 ... 1.4 1969.0 1.824599e+07
f, axes = plt.subplots(4,5)
for f,ax in zip(AKB.Year, axes.ravel()):
sns.countplot(AKB.doy, ax = ax)
ax.set_title(f)
However, this does not work.
How can I do it?
Upvotes: 0
Views: 520
Reputation: 80329
You can use sns.catplot
with kind='count'
to create a FacetGrid of countplots:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
np.random.seed(2021)
df = pd.DataFrame({'year': np.repeat(np.arange(2001, 2020), 100)})
df['doi'] = np.random.randint(180, 200, len(df))
sns.catplot(data=df, kind='count', x='doi', col='year', col_wrap=5,
height=3, aspect=1.8)
plt.tight_layout()
plt.show()
Upvotes: 1