Python Forum
Nested for loops - help with iterating a variable outside of the main loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested for loops - help with iterating a variable outside of the main loop
#3
(Aug-17-2022, 09:06 PM)deanhystad Wrote: Try posting your questions one at a time. I have no idea how to answer everything you've asked so far. I don't really understand what you are asking half the time.

Starting with you last edit. Python thinks ax[index] is indexing a value in a list or array and ax(index) is a function call.

This is where Python sees the error:
Error:
---> 10 m = p(ax(p), ax(q))
And this is what Python thinks the error is:
Error:
TypeError: 'numpy.ndarray' object is not callable
For indexing use [], not ().


In your first attempt at plotting you got an error because you called the plot function with an invalid argument.
This is where Python sees the error:
Error:
---> 11 ax[row, col].plot(x, y, color="green", fontsize=18, ha='center')
And this is what Python thinks the error is:
Error:
AttributeError: 'Line2D' object has no property 'fontsize'
If you look at the documentation you would see that it does't have a fontsize argument.

https://matplotlib.org/stable/api/_as_ge....plot.html

if you have additional questions please ask them as new posts to this thread. Do not edit your original post.

Thank you, firstly, I've made the question far more concise. I appreciate your advice on that. Hopefully my questions are clearer now.

Regarding your first point, I've change from () to []:
def plot_global_inf_sub():
        n = len(df_transposed.columns)
        x = years
        p = 1
        i = 0
        for c in df_transposed.loc['Aruba':'Zimbabwe']:
            for p in range(n):
                for q in range(n): 
                    if i <= n:
                        m = p(ax[p], ax[q])
                        fig, m = plt.subplots(int(n/2), int(n/2))
                        a = 1
                        y = df_transposed[c]
                        ax[i,(i-i)].plot(x, y)
                        ax[i, a(i=+1)].plot(x, y)
                        plt.show()
        
plot_global_inf_sub()
And now get the following error:
Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/7n/v1x237vx47z_7y3g2_63b64h0000gs/T/ipykernel_6003/19824294.py in <module> 16 plt.show() 17 ---> 18 plot_global_inf_sub() /var/folders/7n/v1x237vx47z_7y3g2_63b64h0000gs/T/ipykernel_6003/19824294.py in plot_global_inf_sub() 8 for q in range(n): 9 if i <= n: ---> 10 m = p(ax[p], ax[q]) 11 fig, m = plt.subplots(int(n/2), int(n/2)) 12 a = 1 TypeError: 'int' object is not callable
It's good for me to read and understand your error/explanation reply, thank you.
Reply


Messages In This Thread
RE: Nested for loops - by deanhystad - Aug-17-2022, 09:06 PM
RE: Nested for loops - by dm222 - Aug-17-2022, 09:55 PM
RE: Nested for loops - by dm222 - Aug-17-2022, 10:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 505 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,960 Nov-07-2023, 09:49 AM
Last Post: buran
  reduce nested for-loops Phaze90 11 2,015 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Big O runtime nested for loop and append yarinsh 4 1,445 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,762 Aug-19-2022, 11:07 AM
Last Post: dm222
  loop (create variable where name is dependent on another variable) brianhclo 1 1,183 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  breaking out of nested loops Skaperen 3 1,285 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Multiple Loop Statements in a Variable Dexty 1 1,242 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,218 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to save specific variable in for loop in to the database? ilknurg 1 1,190 Mar-09-2022, 10:32 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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