0

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

6

0

Your Answer

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

Browse other questions tagged or ask your own question.