Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change matplotlib settings temporarily?

I usually set rcParams before plotting anything setting fontsize, figuresize and other settings to better suit my needs. However for a certain axes or figure I would like to change some part of the settings. For example say I set fontsize = 20 in defaults, and for an inset I add to an axes I would like to change all fontsize to 12. What's the easiest way to achieve it? Currently I am tweaking fontsize of different text i.e label font size, tick label font size etc. by hand! Is it possible to do something like

Some plotting here with fontsize=20

with fontsize=12 :
    inset.plot(x,y)
    Set various labels and stuff

Resume plotting with fontsize=20
like image 777
Navdeep Rana Avatar asked Sep 17 '25 07:09

Navdeep Rana


1 Answers

Matplotlib provides a context manager for rc Parameters rc_context(). E.g.

from matplotlib import pyplot as plt

rc1 = {"font.size" : 16}
rc2 = {"font.size" : 8}

plt.rcParams.update(rc1)

fig, ax = plt.subplots()

with plt.rc_context(rc2):
    axins = ax.inset_axes([0.6, 0.6, 0.37, 0.37])

plt.show()

enter image description here

like image 158
ImportanceOfBeingErnest Avatar answered Sep 19 '25 09:09

ImportanceOfBeingErnest