编写一个简单的 Python 股票服务。我怎样才能将其编程为只显示几秒钟的图形?

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

这是目前没有任何错误的程序...

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()

我希望它在几秒钟后隐藏图表。这可能吗?

您可以通过调整此处的最后一行并再添加两行来自动关闭 PyPlot 图。默认情况下设置为 True 的块会阻止 python 脚本继续执行 运行,直到手动关闭图窗。通过将 block 设置为 false,您的代码将继续 运行,您可以完成其他任务,例如关闭图形或用不同的图替换它。

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

这将在 4 秒后关闭图形。