0

I am trying to plot in a nested loop, I expect three different plots with a different colors.

import matplotlib.pyplot as plt
import numpy as np
    
Temp = np.array([6, 7, 8])
Freq = np.arange(1, 10, 0.1)
 
# nested for loop for 3 plots
for T in Temp:
        for f in Freq:
            def quanta(f,T):
                return(f*T)
            final = quanta(f,T)  
            plt.plot(f, final)
             
plt.show()
1
  • 1
    move the plt.show()inside the for loop just after the plt.plot Commented Dec 24, 2022 at 22:03

1 Answer 1

1

one possibility is to use the subplot() function to provide three subplots. I made an adaptation in the code and from what I understand of the question, I believe it can answer you:

import matplotlib.pyplot as plt
import numpy as np

Temp = np.array([6, 7, 8])
Freq = np.arange(1, 10, 0.1)
colors = ['red', 'blue', 'green']

for i, T in enumerate(Temp):
    final = []
    for f in Freq:
        def quanta(f,T):
            return(f*T)
        final.append(quanta(f,T))
    plt.subplot(3, 1, i+1)
    plt.plot(Freq, final, color=colors[i])

plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but I did not want subplots, so I tried the same code but using for i, T in enumerate(Temp):, I get the plot in different colour but legends now are in the same colour

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.