'Is there a way I can iterate through a function that will give me multiple graphs?

I have a Data frame that includes all the columns listed in newcol_var. I made a function that when I manually put these columns in, it will give me a graph. Is there a way I can make a loop of this? The code I have so fr does this, however, only gives me one graph with all the lines on it.

newcol_var = ["PTS_dif","FG_PCT_dif", 'FT_PCT_dif',
                'FG3_PCT_dif', 'AST_dif', 'REB_dif', "WIN_PCT_dif"]
title = ["Average Points Difference [Home-Away]" , "Average 2-Pointer DIfference [Home-Away]", 
          "Average Free-Throw Difference [Home-Away]",
         "Average 3-Pointer Difference [Home-Away]", "Average Assists Difference [Home-Away]",
         "Average Rebound Difference [Home-Away]", "Average Win Percentage Difference [Home-Away]"]

for col in newcol_var:    
    ylabel = title[newcol_var.index(i)]    
    for j in ylabel:
        def difgraphs(col, j):    
            plt.plot(stats_by_yr["SEASON"], stats_by_yr[col], label = "Difference", color = "hotpink")
            plt.xticks(range(2003,2022,1))
            plt.legend()
            plt.xlabel("Season")
            plt.ylabel(col)
            plt.title(j)
    
            return
    
    difgraphs(col, ylabel)

What am I doing wrong? For reference, here is the Dataframe I am using http://localhost:8889/edit/CMSE%20project/NBAstats_cleaned.csv



Solution 1:[1]

Pyplot by default will only show the last plot. To display multiple plot you need to

create a figure first

plt.figure(1)

Then in the loop create a subplot before calling plot

pl.subplot(3,1,1) # Modify the params to change the size ...
plt.plot(...)
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mokemon