Python Interactive Window 中未显示 Matplotlib 图

Matplotlib graph does not show in Python Interactive Window

以下是我的代码,但我无法在我的 Visual Studio 代码上显示情节,即使我在 Python Interactive [=20] 上 运行 =],通常应该在 运行 之后显示图表。表格显示得很好。我也没有像往常一样弹出默认图表。我做错了什么?

import yfinance as yf
import pandas as pd
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import talib

df = pd.read_csv('filename.csv')
df = df.sort_index()
macd, macdsignal, macdhist = talib.MACD(df['Close'], fastperiod=12, slowperiod=26, signalperiod=9)
macd = macd.to_list()
macdsignal = macdsignal.to_list()
macdhist = macdhist.to_list()

df['macd'], df['macdsignal'], df['macdhist'] = macd,macdsignal,macdhist  

ax = plt.gca()
print(df.columns)
df.plot(kind='line',x='Date',y='macd', color='blue',ax=ax)
df.plot(kind='line',x='Date',y='macdsignal', color='red', ax=ax)

plt.show()

csv 文件中的数据如下所示

问题出在 matplotlib.use('agg'),它不支持 show() 函数。这阻止了图表在 Visual Studio 的交互式 Window 上显示。但是,matplotlib.use('agg') 方法可用于将图形保存为 .png 格式。

根据Matplotlib.org,聚合为"the canonical renderer for user interfaces, which uses the Anti-Grain Geometry C++ library to make a raster (pixel) image of the figure"。更多信息可以在这里找到 link here