Search code examples
python-3.xtimealpha-vantage

Programming a simple Python Stock Service. How can I program this to only show the graph for only a few seconds?


This is the current programs with no errors at all...

from alpha_vantage.timeseries import TimeSeries
from alpha_vantage.techindicators import TechIndicators
from matplotlib.pyplot import figure
import matplotlib.pyplot as plt

# Your key here
key = 'W01B6S3ALTS82VRF'
# Chose your output format, or default to JSON (python dict)
ts = TimeSeries(key, output_format='pandas')
ti = TechIndicators(key)

# Get the data, returns a tuple
# aapl_data is a pandas dataframe, aapl_meta_data is a dict
aapl_data, aapl_meta_data = ts.get_daily(symbol='AAPL')
# aapl_sma is a dict, aapl_meta_sma also a dict
aapl_sma, aapl_meta_sma = ti.get_sma(symbol='AAPL')


# Visualization
figure(num=None, figsize=(15, 6), dpi=80, facecolor='w', edgecolor='k')
aapl_data['4. close'].plot()
plt.tight_layout()
plt.grid()
plt.show()

I want it to hide the graph after a few seconds. Is this possible?


Solution

  • You can auto-close the PyPlot figure by adjusting the last line here and adding two more lines. Block, which is set to True by default, stops the python script from continuing to run until the figure is manually closed. By setting block to false, your code will continue to run and you can complete other tasks such as closing the figure or replacing it with a different plot.

    plt.show(block=False)
    plt.pause(4)
    plt.close()
    

    This will close the figure after 4 seconds.