1

I was wondering how you can change the matplotlib.rcParams permanently instead of indicating it every time when you open jupyter notebook. (like change in the profile as the default value).

Thanks

nonickname
  • 55
  • 2
  • 5

1 Answers1

1

A short answer, also for personal reference...

I would still prefer indicating it in the code though. However it doesn't have to be done for every single parameters as it can be done in one line calling for a style file for instance.

Anyway, here are several ways of doing so, from the most permanent change to the most soft and subtle way of changing the parameters:

1. Change default config file

Change the default parameters in matplotlibrc file, you can find its location with:

import matplotlib as mpl
mpl.get_configdir()

2. Modify iPython profile

Change how matplotlib default options are loaded in iPython by modifying options in ~/.ipython/profile_default/ipython_kernel_config.py, or wherever your configuraiton file is (ipython locate profile to find out) (see this answer for details)

3. Create your own styles

You can create your own custom rcParams files and store them in .config/matplotlib/stylelib/*.mplstyle or wherever os.path.join(matplotlib.get_configdir(), stylelib) is.

The file should be in the following format, similar to matplotlibrc, e.g. my_dark.mplstyle:

axes.labelcolor: white
axes.facecolor : 333333
axes.edgecolor : white
ytick.color : white
xtick.color : white
text.color : white
figure.facecolor : 0D0D0D

This is an example of a style for a dark background with white axes, and white tick labels...

To use it, before your plots, call:

import matplotlib.pyplot as plt
plt.style.use("my_dark")
# Note that matplotlib has already several default styles you can chose from

You can overlay styles by using several ones at once, the last one overwrites its predecessors' parameters. This can be useful to keep e.g. font variables from a certain style (paper-like, presentation) but simultaneously toggle a dark mode on top, by overwriting all color-related parameters. With something like plt.style.use(["paper_fonts", "my_dark"])

More info in their documentation.

4. Context manager for rc_params

You can also use a context manager, this option is great in my opinion, to do something like:

import matplotlib as mpl
import matplotlib.pyplot as plt

with mpl.rc_context(rc={'text.usetex': True}, fname='screen.rc'):
    plt.plot(x, a)

plt.plot(x, b)

Here we used a first set of parameters as defined in the file screen.rc then tweak the text.usetex value solely for the plt.plot(x,a) while plt.plot(x, b) will be using another (default here) set of rc parameters.

Again, see the doc for more information.

H. Rev.
  • 370
  • 1
  • 8