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

Jupyter Lab中使用matplotlib触发内核崩溃问题求助

Jupyter Lab中matplotlib绘图触发内核崩溃问题

问题详情

  • 本地Python项目中使用matplotlib.pyplot绘图可正常运行,但相同代码在Jupyter Lab环境执行时会直接导致内核崩溃
  • 已尝试以下无效解决方案:
    • 将freetype从2.11版本降级至2.10:本地PyCharm和Jupyter环境均使用freetype 2.12.1版本,仅Jupyter出现问题,该方案无效
    • 设置环境变量:
      import os
      os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
      
      该方案同样无法解决问题

复现代码

业务场景代码

import tensorflow as tf
import pathlib
import matplotlib.pyplot as plt
import numpy as np

batch_size = 32
img_height = 180
img_width = 180

data_dir = pathlib.Path("./data_dir").with_suffix('')
output_dir = pathlib.Path("./ds_out").with_suffix('')

ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  labels='inferred',
  image_size=(img_height, img_width),
  batch_size=batch_size)

class_names = ds.class_names
print(class_names)

ds.save(str(output_dir))

loaded_ds = tf.data.Dataset.load(str(output_dir))

plt.figure(figsize=(10, 10))
for images, labels in loaded_ds.take(1):
  for i in range(9):
    ax = plt.subplot(3, 3, i + 1)
    plt.imshow(images[i].numpy().astype("uint8"))
    plt.title(class_names[labels[i]])
    plt.axis("off")

plt.show()

极简复现代码

# importing matplotlib module 
from matplotlib import pyplot as plt 
 
# x-axis values 
x = [5, 2, 9, 4, 7] 
 
# Y-axis values 
y = [10, 5, 8, 4, 2] 
 
# Function to plot 
plt.plot(x, y) 
 
# function to show the plot 
plt.show()

注:移除plt.plot(x, y)和plt.show()两行代码后,内核不会崩溃

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 11:52:28