Answer a question


Summary

When figure is drawn with matplotlib using VSCode Jupyter Notebook, the figure is rendered in light theme even when the VSCode theme is dark. By my understanding, this issue was resolved and the VSCode is supposed to default the matplotlib figures to dark_background when the VSCode theme is default dark.

What am I doing wrong?


I am fairly new to this whole data science and machine learning scene, and up till now I have been using Jupyter Notebook (and sometimes Jupyter Lab) for all my coding needs. However, the code completion in Jupyter Notebook leaves something to be desired, and so began my search for alternatives to Jupyter Notebook.

I recently discovered that VSCode supports the use of Jupyter Notebook natively and after installing python extension, I was able to create new and open previously saved .ipynb files and every thing works great (especially the IntelliSense).

There is, however, one issue which is somewhat a nuisance. My theme for VSCode is default dark, and the figures produced by matplotlib are in light theme.

using matplotlib in VSCode .ipynb

I know I can use following code to render the figures in dark mode:

from matplotlib import style
style.use('dark_background')

But on GitHub, this issue has already been addressed so that when the VSCode is in dark mode, then the VSCode will automatically default the matplotlib style to dark_background. However I am not experiencing this automation.

Is there some setting that I have failed to configure?

Answers

After some searching I found this solution: In settings (via ctrl+,) search for jupyter run startup commands

edit the json to:

    "jupyter.runStartupCommands": [
        "import matplotlib.pyplot as plt",
        "plt.style.use('dark_background')"
    ]

Then, restart your kernel.

Note, that somehow only using matplotlib.style.use('dark_background') does not yet fully work out, since some part gets overwritten on loading pyplot in the notebook it seems.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐