skip to Main Content

At some point, python plot color (either using jupyter notebook or ipython #%% command) is synced with my vscode theme. It wasn’t before that the plot had white background as default.

When I use darker theme (monokai or vscode dark theme), the plot background become black. However, when I change it to light theme, the default python color comes back (white background). I need white background as default. I can set plot color for every script but it’s very nuisance when I run other person’s code, especially during collaborative works.

Which setting in vscode should I deal with? I tried several options related to jupyter, but they didn’t solve my problem.

If the setting related to ipython, how can I check the setup?

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out. There was an option in vscode setting whether to set the ipython images to match with the background color or not.

    It took me several months to find this option, and I cannot figure it out again right now. But if someone is suffering with this problem like me, scrutinize the vscode setting.


  2. The behavior you are describing is likely due to the matplotlib library, which is used for creating plots in Python. Matplotlib uses the default style for your system, which can be influenced by the theme you are using in your text editor.

    To set the default style for matplotlib, you can use the following code at the beginning of your script:

    import matplotlib as mpl
    mpl.rcParams['figure.facecolor'] = 'white'
    

    This will set the default background color of your plots to white, regardless of the theme you are using in your text editor.

    If the rcParams setting is not being retained even after you set it, it could be because the settings are being overridden by a configuration file or by the settings in your IPython or Jupyter environment.

    One way to ensure that your settings are retained is to create a custom matplotlib style file and set it as the default style. To do this, you can create a file called my_custom_style.mplstyle in a directory of your choice and include the following line in the file:

    figure.facecolor : white
    

    This sets the background color of the plots to white.

    Then you can use this style by calling

    mpl.style.use('my_custom_style')
    

    This will set the default background color of your plots to white, regardless of the theme you are using in your text editor.

    If you use jupyter notebook, you can also set the matplotlib style in the notebook by adding the following code snippet in the first cell of your notebook:

    from matplotlib import style
    style.use("<path-to-style-file>")
    

    If you are using IPython, you can set the default matplotlib style by adding the following lines in your ipython_config.py file:

    c.InteractiveShellApp.matplotlib = '<path-to-style-file>'
    

    You can also check in ipython profile directory, if there any custom configuration files like ipython_kernel_config.py, ipython_config.py that might be overwriting your settings.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search