Aug-19-2022, 11:07 AM
Hi all,
I am trying to write some code which iterates over columns in a DataFrame and plots each column in its own subplot.
Take the DataFrame:
But the below plots the the same column on every subplot.
Does anyone have a solution to this problem:
I am trying to write some code which iterates over columns in a DataFrame and plots each column in its own subplot.
Take the DataFrame:
df = pd.DataFrame( { "year":["2000", "2001", "2003", "2004", "2005"], "UK": [2.0, 2.4, 1.3, 5.4, 4.4], "USA": [3.1, 2.3, 2.8, 2.6, 5.3], "RUS": [4.3, 2.6, 3.5, 5.7, 7.6], "FRA": [5.6, 4.5, 4.5, 6.7, 3.9] })
Output: year UK USA RUS FRA
0 2000 2.0 3.1 4.3 5.6
1 2001 2.4 2.3 2.6 4.5
2 2003 1.3 2.8 3.5 4.5
3 2004 5.4 2.6 5.7 6.7
4 2005 4.4 5.3 7.6 3.9
I have tried adapting the below code:rows, cols = 2, 2 fig, ax = plt.subplots(rows, cols) for row in range(rows): for col in range(cols): ax[row, col].text(0.5, 0.5, str((row, col)), color="green", fontsize=18, ha='center')
Output:[attachment=1916]
So, instead of having the relevant subplot axes coordinates on subplots, the columns from the DataFrame are plotted against the year column. Each subplot containing one of the country (UK, USA, RUS, FRA) columns against the year column. But the below plots the the same column on every subplot.
Does anyone have a solution to this problem:
rows, cols = 2, 2 x = df['year'] country = df.loc['United Kingdom':'France'] fig, ax = plt.subplots(rows, cols) for row in range(rows): for col in range(cols): for c in country: ax[row, col].plot(x, y)
Output:[attachment=1917]
The below output is what I am trying to achieve with the above code, in which each subplot contains a different column plotted against years:Output:[attachment=1918]