由于某些不可抗力的原因,导致在线访问Seaborn的数据集报错。可以使用离线加载的方式解决。
报错信息:
urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
Traceback (most recent call last): File "D:\sushun\tools\python312\python39\lib\urllib\request.py", line 1346, in do_open h.request(req.get_method(), req.selector, req.data, headers, File "D:\sushun\tools\python312\python39\lib\http\client.py", line 1286, in request self._send_request(method, url, body, headers, encode_chunked) File "D:\sushun\tools\python312\python39\lib\http\client.py", line 1332, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "D:\sushun\tools\python312\python39\lib\http\client.py", line 1280, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "D:\sushun\tools\python312\python39\lib\http\client.py", line 1040, in _send_output self.send(msg) File "D:\sushun\tools\python312\python39\lib\http\client.py", line 980, in send self.connect() File "D:\sushun\tools\python312\python39\lib\http\client.py", line 1448, in connect super().connect() File "D:\sushun\tools\python312\python39\lib\http\client.py", line 946, in connect self.sock = self._create_connection( File "D:\sushun\tools\python312\python39\lib\socket.py", line 823, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "D:\sushun\tools\python312\python39\lib\socket.py", line 954, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11004] getaddrinfo failed During handling of the above exception, another exception occurred: File "D:\sushun\code\python\Chatbot\data_visual_2.py", line 8, in <module> print (sns.get_dataset_names() ) File "D:\sushun\code\python\Chatbot\venv39\lib\site-packages\seaborn\utils.py", line 499, in get_dataset_names with urlopen(DATASET_NAMES_URL) as resp: File "D:\sushun\tools\python312\python39\lib\urllib\request.py", line 214, in urlopen return opener.open(url, data, timeout) File "D:\sushun\tools\python312\python39\lib\urllib\request.py", line 517, in open response = self._open(req, data) File "D:\sushun\tools\python312\python39\lib\urllib\request.py", line 534, in _open result = self._call_chain(self.handle_open, protocol, protocol + File "D:\sushun\tools\python312\python39\lib\urllib\request.py", line 494, in _call_chain result = func(*args) File "D:\sushun\tools\python312\python39\lib\urllib\request.py", line 1389, in https_open return self.do_open(http.client.HTTPSConnection, req, File "D:\sushun\tools\python312\python39\lib\urllib\request.py", line 1349, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
解决方案:
1. 查看本地缓存路径
import seaborn as sns
print(sns.get_data_home())
2. 浏览器访问 mwaskom/seaborn-data: Data repository for seaborn examples (github.com)
3. 下载后,将文件解压到缓存路径中
4. 再次运行程序
import matplotlib.pyplot as plt
import seaborn as sns
# 加载内置示例数据集
tips = sns.load_dataset("tips")
# 绘制散点图矩阵
sns.pairplot(tips, hue="day", diag_kind="kde")
plt.show()