How to set local rcParams or rcParams for one shape in matplotlib

I am writing a build function in python using matplotlib . The user can specify some things, for example. tick lines. The easiest way is to change rcParams , but these are global properties, so all new charts will have tick lines after calling the build function.

Is there a way to set default values ​​for individual lines for only one shape?

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

+7
python matplotlib plot
source share
1 answer

You can use the rc_context function in the with statement, which sets the rc parameters with the dictionary that you provided for the block indented below, and then reset until they were 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) 
+13
source share

All Articles