1

Now that its working like this way so I need to save them automatically in .eps format as an increment like 1.eps, 2.eps. Another thing, is there a way I can put the output of y like [1 1 1 1 1 1] on top of the each plot? I am quite new to python and thats why I am still trying to learn the things. The 3phases.txt file consists of 3 lines

1   1   1, 
1   -1  1, 
-1  -1  -1

The code again:

import matplotlib.pyplot as plt
import numpy as np

D=13.0
n = range(1,7)
x = np.linspace(-0.3-D/2, 0.3+D/2, 3000)

q = np.array([0, 4.38,  12.61,  3.63,  0,  6.39])
f = open('3phases.txt','r')

for line in f.readlines():  
    line = line.split()
    line.insert(0, '1')
    line.insert(3, '1')
    line.insert(4, '1')

    t=map(float,line)

    y = np.array(t*q)
    d=sum(l*np.cos(2*np.pi*j*x/D) for j,l in zip(n,y)) 

    fig, ax = plt.subplots()
    ax.plot(x, d, 'ro')

plt.show()
Hooked
  • 84,485
  • 43
  • 192
  • 261
user2095624
  • 363
  • 6
  • 8
  • 17

1 Answers1

3

So, you want to have a separate plot for each line of the input file? Then you can do along these lines:

import matplotlib.pyplot as plt
import numpy as np

with open('3phases.txt', 'r') as f:
   for j, line in enumerate(f):
      print line
      x = np.array([int(l) for l in line.split()])
      fig, ax = plt.subplots()

      ax.plot(x, x**2, 'ro')
      plt.savefig(str(j)+'.eps')

Alternatively, you might want to avoid creating new figures in a loop (especially if you've lots of them), create it once and reuse: then you'll need to clear it after saving, see here.

As a rule of thumb, I'd recommend avoiding the blanket imports from pylab, from pylab import *, unless you're in an interactive mode.

Community
  • 1
  • 1
ev-br
  • 24,968
  • 9
  • 65
  • 78