Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set local rcParams or rcParams for one figure in matplotlib

I am writing a plotting function in python using matplotlib. The user can specify some things, e.g. "tick lines". The easiest way would be to change the rcParams, but those are global properties, so all new plots will have tick lines after that plotting function was called.

Is there a way to set the plotting defaults specifically for just one figure?

Or is there at least a good way to change the properties for one plotting-function and then change them back to the values that were used before (not necessarily the rcdefaults)?

like image 430
John Smith Avatar asked Apr 01 '14 17:04

John Smith


People also ask

How do I reset matplotlib rcParams?

Use matplotlib. style. use('default') or rcdefaults() to restore the default rcParams after changes.

Which parameter is used to set a main title for a figure with multiple plots?

Hence, to set a single main title for all subplots, suptitle() method is used. Parameters: This method accept the following parameters that are discussed below: t : This parameter is the title text.

What is matplotlib rcParams?

matplotlib.rc can be used to modify multiple settings in a single group at once, using keyword arguments: mpl. rc('lines', linewidth=4, linestyle='-.') plt.

What does RC stand for in rcParams?

It stands for “run commands”.


1 Answers

You can use the rc_context function in a with statement, which will set the rc parameters with a dictionary you provide for the block indented below and then reset them to whatever they were before after the block. For example:

with plt.rc_context({"axes.grid": True, "grid.linewidth": 0.75}):
    f, ax = plt.subplots()  # Will make a figure with a grid
    ax.plot(x, y)

f, ax = plt.subplots()  # Will make a figure without a grid
ax.plot(x, y)
like image 141
mwaskom Avatar answered Oct 18 '22 17:10

mwaskom