如何在 JupyterLab 深色主题中显示图例和刻度?

How to make figure legends and ticks visible in JupyterLab dark theme?

当使用 JupyterLab(版本 0.32.0)深色主题时,由于前景和背景颜色非常接近,因此无法看到图形刻度和图例。如何让它们可见?

以下是一些截图以供说明:

绘图代码:

import matplotlib.pyplot as plt
%matplotlib inline

plt.subplot()
plt.plot([1,2,3], label="test1")
plt.plot([3,2,1], label="test2")
plt.legend(bbox_to_anchor=(1.05, 1), loc=2,
           frameon=False)

浅色主题输出图:

深色主题下同样的输出图:

备注:

这个问题已在 Github 两个 JupyterLab and Matplotlib, and it has been answered by telamonian and blink1073 的问题中被问过多次。我在此处重新发布并重新回答问题,以便更轻松地查找答案。

根据 JupyterLab 的 issue 3855,即将发布的版本可能会解决此问题。

只是在这里引用 telamonian and blink1073 的手动解决方法:

plt.style.use('dark_background')

以下是一个简短的演示:

代码:

import matplotlib.pyplot as plt

%matplotlib inline

plt.style.use('dark_background')
plt.plot(list(range(10)), list(range(10)))