简体   繁体   English

vscode 设置问题 - python plot 背景色

[英]vscode setup question - python plot background color

At some point, python plot color (either using jupyter notebook or ipython #%% command) is synced with my vscode theme.在某些时候,python plot 颜色(使用 jupyter notebook 或 ipython #%% 命令)与我的 vscode 主题同步。 It wasn't before that the plot had white background as default. plot 之前不是默认的白色背景。

When I use darker theme (monokai or vscode dark theme), the plot background become black.当我使用较暗的主题(monokai 或 vscode 深色主题)时,plot 背景变为黑色。 However, when I change it to light theme, the default python color comes back (white background).但是,当我将其更改为浅色主题时,默认的 python 颜色又回来了(白色背景)。 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.我可以为每个脚本设置 plot 颜色,但是当我运行其他人的代码时,这非常麻烦,尤其是在协作工作期间。

Which setting in vscode should I deal with?我应该处理 vscode 中的哪个设置? I tried several options related to jupyter, but they didn't solve my problem.我尝试了几个与 jupyter 相关的选项,但它们并没有解决我的问题。

If the setting related to ipython, how can I check the setup?如果设置与 ipython 相关,我该如何检查设置?

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.您所描述的行为可能是由于 matplotlib 库造成的,该库用于在 Python 中创建绘图。Matplotlib 使用系统的默认样式,这可能会受到您在文本编辑器中使用的主题的影响。

To set the default style for matplotlib, you can use the following code at the beginning of your script:要为 matplotlib 设置默认样式,您可以在脚本开头使用以下代码:

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.如果 rcParams 设置在您设置后仍未保留,可能是因为这些设置被配置文件或 IPython 或 Jupyter 环境中的设置覆盖。

One way to ensure that your settings are retained is to create a custom matplotlib style file and set it as the default style.确保保留您的设置的一种方法是创建自定义 matplotlib 样式文件并将其设置为默认样式。 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:为此,您可以在您选择的目录中创建一个名为 my_custom_style.mplstyle 的文件,并在该文件中包含以下行:

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:如果你使用jupyter notebook,你也可以在notebook的第一个单元格中添加如下代码片段,在notebook中设置matplotlib样式:

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:如果您使用的是 IPython,则可以通过在 ipython_config.py 文件中添加以下行来设置默认的 matplotlib 样式:

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.您还可以检查 ipython 配置文件目录,如果有任何自定义配置文件,如 ipython_kernel_config.py、ipython_config.py 可能会覆盖您的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM