在上述代码基础上 输出股票实时图
时间: 2024-09-30 10:14:57 浏览: 24
要在上述代码基础上输出股票实时图,你可以使用`matplotlib`库,它非常适合绘制金融图表。以下是修改后的部分代码,将添加一个简单的时间序列折线图:
首先,确保已经安装了`matplotlib`库,如果没有,可以通过pip安装:
```bash
pip install matplotlib
```
然后,在`job`函数中添加绘制图表的部分:
```python
import matplotlib.pyplot as plt
plt.ion() # 开启交互模式
def plot_stock_chart(price_data):
fig, ax = plt.subplots()
ax.clear() # 清除上一帧的图表
ax.plot(price_data.index, price_data['Close'], label=stock_code)
ax.axhline(y=current_price, color='black', linestyle='--')
ax.set_ylabel('Price')
ax.set_xlabel('Time')
ax.grid(True)
ax.legend()
plt.pause(0.1) # 阻塞一段时间以便于查看图表
# ... (其他代码保持不变)
def job():
price = get_stock_price()
print_trend(price)
if current_price is not None:
plot_stock_chart(data['Close'])
current_price = price
```
这将在每次获取到新的股票价格时,更新图表并在屏幕上展示。`plt.pause(0.1)`会让程序暂停一小段时间,让图表有时间显示。
阅读全文
相关推荐


















