如果我在 Visual Studio Code 和 Matplotlib 中使用深色主题,则可以将图形配置为深色背景,但小部件/单元格的背景仍然是白色。
%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('dark_background')
figure, axis = plt.subplots()
axis.plot(np.arange(100))
如果我使用 Jupyter 扩展的选项在 notebook 中使用 Visual Studio Code 主题,也会发生这种情况(实际上,情况会更糟,因为轴标签不可见)。
注意,我需要使用
%matplotlib widget
作为渲染器,并且不能使用%matplotlib inline
。对于内联,我找到了一个有效的配置。
在浏览器中,如果我选择 Jupyter 深色主题,它就可以工作,所以我想,如果可能的话,我会在 Jupyter 渲染器扩展中选择不同的“主题”(?)
有没有办法配置 Jupyter 扩展 / Visual Studio Code / ... 以获得真正的“Matplotlib 深色模式”?
我找到了解决方法!
(确保 Visual Studio Code 已关闭。)
打开文件资源管理器,转到:
C:\Users\<your_username_here>\.vscode\extensions\ms-toolsai.jupyter-<(THE VERSION NUMBER MAY VARY)2022.4.1021342353>\out\webviews\webview-side\ipywidgetsRenderer
在运行任何 Jupyter 内容之前,打开 ipywidgetsRenderer.js 文件(使用 Notepad++ 或 Notepad 甚至 Visual Studio Code)
无论您在哪里看到
cell-output-ipywidget-background
后跟 background: white
,请将 white
替换为 black
。
现在问题解决了。享受吧:)
一个简单的解决方案是在绘图单元之前添加一个 html 样式单元:
%%html
<style>
.cell-output-ipywidget-background {
background-color: transparent !important;
}
.jp-OutputArea-output {
background-color: transparent;
}
</style>