-1

The following function plots stock price and volume when called. However, when it is called to create multiple plots iteratively only the last plot was shown up in output. How to flush the plot outputs to get the multiple plots. Trying the sleep function but it does not work.

import yfinance as yf
import matplotlib.pyplot as plt
import numpy as np


def skplot(ticker,dt1,dt2):
    tck = yf.Ticker(ticker)
    print(ticker+"\n"+"Market      Cap:"+'${:,.0f}'.format(tck.info["marketCap"]))
    df = yf.download(ticker, start=dt1, end=dt2)

    top = plt.subplot2grid((4,4), (0, 0), rowspan=3, colspan=4)
    top.plot(df.index, df["Close"])
    plt.title(ticker)

    bottom = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4)
    bottom.bar(df.index, df['Volume'])

    plt.gcf().set_size_inches(10,8)

    time.sleep(1)

#this one works
skplot("HLIT","2018-01-01","2019-07-11")

#called in a loop produce only the last chart


def stocklist():
    '''Returns a list of stocks that met the criteria for rsi_plot'''
    l=[   
"HLIT"  ,
"OHRP"  ,
"HELE"  ,
"CY"           ]
    for i in l:
#        print(i)
        skplot(i,"2018-01-01","2019-07-11")
    return

stocklist()
user4224870
  • 135
  • 1
  • 2
  • 9
  • This seems like normal behavior. What are you trying to do with the plots? – Akaisteph7 Jul 12 '19 at 14:22
  • Do you want one figure with multiple *sub*plots or multiple figures? – wwii Jul 12 '19 at 14:30
  • I am expecting the following result: price chart of stock1 and vol ; price chart of stock2 and vol2; price chart of stock3 and vol; etc. Currently the output of Spyder shows only the last price chart and vol . That is stock price of "CY" and vol. If this explains. – user4224870 Jul 12 '19 at 14:35
  • Possible duplicate of [Is it possible to have multiple PyPlot windows? Or am I limited to subplots?](https://stackoverflow.com/questions/5993206/is-it-possible-to-have-multiple-pyplot-windows-or-am-i-limited-to-subplots) – wwii Jul 12 '19 at 15:20

2 Answers2

1

Try adding plt.figure() right above top = plt.subplot2grid((4,4), (0, 0), rowspan=3, colspan=4).
If you don't do that, then you will be continuously plotting in the same figure and overwriting it, or so I assume. Can't really test it since my own matplotlib is glitching out rn.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
PoorProgrammer
  • 459
  • 1
  • 4
  • 14
0

If you want a single figure that contains all the plots, you need to change the location(row,col) for each plot and remove the rowspan and colspan arguments:

def f(i):
    r,c = divmod(i, 4)
    top = plt.subplot2grid((4,4), (r, c))
    top.plot(range(10*i), range(10*i))

for i,thing in enumerate(range(16)):
    f(i)

plt.show()
plt.close()

Unless you want your plots to be positioned on something other than a regular grid, similar to these examples, you can just use plt.subplot.

def g(i):
    plt.subplot(4,4,i)
    plt.plot(range(10*i), range(10*i))

for i,thing in enumerate(range(16),1):
    g(i)

plt.show()
plt.close()
wwii
  • 23,232
  • 7
  • 37
  • 77