Python Forum
Trying to loop through code to plot seaborn line plots across multiple subplots
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to loop through code to plot seaborn line plots across multiple subplots
#1
Hey all,

I am trying to loop through a dataframe 'out' and create a separate subplot for each group and level.

There are 35 groups and 5 levels, producing 175 plots in total.

I thus want to create 5 figures each with 35 subplots (7 rows and 5 columns).

However, when I try to do this with the code below, all of the subplots produced are empty.

I would be so grateful for a helping hand!

I have attached some example data below.

h = 0
l = 0 

for j in range(0,len(individualoutliers)):
    fig = plt.figure(figsize=(70,70))
    fig,axes = plt.subplots(7,5)
    for i in range(0,len(individualoutliers[j])):
        individualoutliersnew = individualoutliers[j]
        out = individualoutliersnew.loc[:, ["newID", "x", "y","level"]].apply(lambda x: pd.Series(x).explode())
        while (int(h) < 6 & int(l) < 4): 
            for k,g in out.groupby("newID"):
                     globals()['interestingvariable'] = g
                     newframe = interestingvariable 
                     sns.lineplot(data=newframe,x='x',y='y',ax=axes[int(h),int(l)])
                     axes[int(h),int(l)].set_xlabel('x-coordinate',labelpad = 40,fontsize=70,weight='bold')
                     axes[int(h),int(l)].set_ylabel('y-coordinate',labelpad = 40,fontsize=70,weight='bold')
                     plt.xticks(weight='bold',fontsize=60,rotation = 30)
                     plt.yticks(weight='bold',fontsize=60)
                     title = (newframe.iloc[0,0]+' '+'level'+' '+str(newframe.iloc[0,3]))
                     axes[int(h),int(l)].set_title(title,fontsize=70,pad=40,weight='bold') 
                     h = int(h)+1
                     l = int(l)+1 
    dir_name = "/Users/macbook/Desktop/"
    plt.rcParams["savefig.directory"] = os.chdir(os.path.dirname(dir_name))
    plt.savefig('figure'+' '+str(j))
    plt.show()
out.head(10)

newID   x   y   level
24  610020  55  60  1
24  610020  55  60  1
24  610020  55  60  1
24  610020  60  60  1
24  610020  60  65  1
24  610020  60  65  1
24  610020  65  70  1
24  610020  70  70  1
24  610020  70  75  1
24  610020  75  75  1
newframe.head(10)

newID   x   y   level
3313    5d254d  55  60  1
3313    5d254d  55  60  1
3313    5d254d  55  60  1
3313    5d254d  60  60  1
3313    5d254d  60  65  1
3313    5d254d  60  65  1
3313    5d254d  65  65  1
3313    5d254d  65  70  1
3313    5d254d  70  75  1
3313    5d254d  75  75  1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 268 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Plot multiple 2D Curves in 3D stc 5 892 Jun-11-2023, 05:48 PM
Last Post: snippsat
  Multiple Plots in Spyder PythonAndMe2023 0 815 Feb-03-2023, 07:00 PM
Last Post: PythonAndMe2023
  For Loop: To see plot for different columns JaneTan 0 937 Dec-14-2022, 05:55 AM
Last Post: JaneTan
  python multiple try except block in my code -- can we shorten code mg24 10 5,888 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,638 Aug-19-2022, 11:07 AM
Last Post: dm222
  Graphic line plot with matplotlib, text file in pytho khadija 2 1,338 Aug-15-2022, 12:00 PM
Last Post: khadija
  How to plot seaborn heatmap on top of a background circle SriRajesh 0 1,367 Jul-09-2022, 04:00 AM
Last Post: SriRajesh
  Installing and Using Seaborn Led_Zeppelin 4 2,470 Jun-07-2022, 09:44 PM
Last Post: snippsat
  Multiple Loop Statements in a Variable Dexty 1 1,175 May-23-2022, 08:53 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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