1

I'm attempting to plot the square roots in a single figure. However, this is not getting plotted. Can somebody help me?

import numpy as np
import matplotlib.pyplot as plt

plt.figure()
for i in np.arange(1,5):
    zm=i**2        
    plt.plot(i,zm,'r')    
    print(i,zm)
plt.show()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
d_k
  • 21
  • 1

1 Answers1

0

A few issues with your code:

  • zm should be an array, but instead it is an integer that gets overwritten every cycle with the return of i**2,
  • The plot() instruction should be outside the loop,
  • You don't really need the for loop, you can do the square of the array with the ** operator.

I guess this is what you are looking for:

import numpy as np
import matplotlib.pyplot as plt

xx = np.arange(1, 5)
zm = xx**2
plt.figure()
plt.plot(xx,zm,'r')
plt.show()

enter image description here

BTW, I believe you meant square and not square root.

I hope it helps.

gmagno
  • 1,770
  • 1
  • 19
  • 40