You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Matplotlib 3.6中FigureCanvas无required_interactive_framework属性的警告

解决Matplotlib 3.6.2中的MatplotlibDeprecationWarning警告

这个警告是Matplotlib 3.6版本开始的兼容性问题:从3.6起,不再支持未指定required_interactive_framework属性的FigureCanvas,后续版本会彻底移除相关支持。结合你的代码(用到了Yellowbrick库),可以用以下几种方法解决:

方法1:显式指定Matplotlib后端

在导入matplotlib.pyplot之前,手动设置一个支持的交互后端,比如TkAgg、Qt5Agg等,根据你的操作系统和已安装的库选择:

import os
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# 先指定后端
import matplotlib
matplotlib.use('TkAgg') # 可替换为Qt5Agg、Agg(非交互模式)等
import matplotlib.pyplot as plt
import seaborn as sns
from yellowbrick.regressor import ResidualsPlot

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

directory = 'Data'
file = 'Titanic.csv'

titanic = pd.read_csv(os.path.join(directory, file))
titanicRelevent = titanic[['Class', 'Sex', 'Age', 'Survived']]
titanicNoNA = titanicRelevent.dropna()

sns.countplot(data=titanicNoNA, x='Class')
plt.show()
sns.countplot(data=titanicNoNA, x='Sex')
plt.show()
sns.countplot(data=titanicNoNA, x='Age')
plt.show()

方法2:更新Yellowbrick库

这个警告大概率是旧版本Yellowbrick与Matplotlib 3.6+不兼容导致的,更新到最新版Yellowbrick即可解决:

pip install --upgrade yellowbrick

方法3:临时抑制警告(仅临时场景使用)

如果只是想暂时屏蔽警告,不想修改后端或更新库,可以在代码开头添加警告过滤:

import warnings
from matplotlib.cbook import MatplotlibDeprecationWarning

warnings.filterwarnings("ignore", category=MatplotlibDeprecationWarning)

# 后续导入和代码保持不变

内容的提问来源于stack exchange,提问作者achi00

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 23:05:26