Python Forum
Nested for loops: Iterating over columns of a DataFrame to plot on subplots
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested for loops: Iterating over columns of a DataFrame to plot on subplots
#1
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:
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:
   
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:
   
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:
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,906 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Need help with creating dynamic columns with for loops for stock prices PaDat 2 902 Feb-22-2023, 04:34 AM
Last Post: PaDat
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,487 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  For Loop: To see plot for different columns JaneTan 0 970 Dec-14-2022, 05:55 AM
Last Post: JaneTan
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,680 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,593 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,225 Jul-18-2022, 12:59 AM
Last Post: Skaperen
Question Error with MatPlotLib subplots Danno 8 3,432 Apr-01-2022, 12:23 AM
Last Post: Danno
  Convert python dataframe to nested json kat417 1 6,348 Mar-18-2022, 09:14 PM
Last Post: kat417
  Break out of nested loops muzikman 11 3,353 Sep-18-2021, 12:59 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020