Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

How to Iterate or Loop the Python Script for the range

I have built a python script to plot an image file, it works fine. When I want to plot multiple image files within a range, I am able to create multiple image files. The issue I am facing - the first image files comes out fine. The second, third and there after all image files are overlapped with the previous image files. What is that I am doing wrong. Appreciate some help, I am new to Python.

df4 = ((df3 + 1).cumprod() - 1)

for row_no in range(2, 10):
  df5 = df4.iloc[:row_no,:]

  rcParams['figure.figsize']=25,10
  plt.scatter(df5.mean(), df5.std())
  plt.vlines(df5['SPY'].mean(), ymin = -0.01, ymax = 0.10, colors = 'blue') 
  plt.hlines(df5['SPY'].std(), xmin = -0.10, xmax = 0.10, colors = 'blue') 
  plt.tight_layout()
  plt.xlabel('Expected returns')
  plt.ylabel('Risk')
  plt.title("Watchlist Today Performance")
  plt.xlim([-0.1, 0.1])
  plt.ylim([-0.01, 0.1])

  for label, x, y in zip(df5.columns, df5.mean(), df5.std()):
    #plt.annotate(label, xy = (x, y), xytext = (20, -20), textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'red', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
    plt.annotate(label, xy = (x, y), xytext = (20, -20), textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'red', alpha = 0.5))

  plt.savefig('/content/drive/MyDrive/Images/jan26/image.png')

  i = 0
  while os.path.exists("/content/drive/MyDrive/Images/jan26/image-%s.png" % i):
    i += 1

  with open('/content/drive/MyDrive/Images/jan26/image.png', 'rb') as f:
    data = f.read()

  with open('/content/drive/MyDrive/Images/jan26/image-%s.png' % i, 'wb') as f:
    f.write(data)

First Image file enter image description here Fifth Image File enter image description here Final Image File enter image description here

question from:https://stackoverflow.com/questions/65926196/how-to-iterate-or-loop-the-python-script-for-the-range

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...