Matplotlib 背景与 vscode 主题不匹配

Matplotlib background won't match vscode theme

当我在 vscode 中的 jupyter notebook 中创建 matplotlib 图表时,它为整个单元格提供了白色背景。我试过把

"import matplotlib.pyplot as plt",
"plt.style.use('dark_background')"

在我的设置文件中将 运行 作为启动命令,这使绘图具有黑色背景,但它仍然与我的 vscode 主题不匹配。有谁知道如何使它们无缝匹配?透明图表背景mabye?

您可以在 PyQt5 中绘制图表 window。

我不确定这是否能解决您的问题,但这是使图表透明的一种方法。希望对您有所帮助:

您需要导入PyQt5(pip3 install PyQt5)。

from PyQt5 import QtCore

创建透明window的代码在定义你的图形后插入:

window = plt.gcf().canvas.manager.window # used to get your figure
window.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
window.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
window.setStyleSheet("background:transparent")

最后用 plot.show() 显示 window:

plt.show()

白色背景实际上是故意的,以便与 Jupyter 显示的内容相匹配。它来自读取 matplotlib 输出的 'needs_background' 元素。

不幸的是,根据 'needs_background' 元素,背景将始终为白色或黑色。 (正如您发现的 'dark_background' 风格)

代码在这里: https://github.com/microsoft/vscode-notebook-renderers/blob/91210f63be0fcca92d7ea9125b7e9445a423b0a4/src/client/render.tsx#L70

您可以在此处输入对 jupyter 扩展程序更改颜色的请求: https://github.com/microsoft/vscode-jupyter/issues