Why is matplotlib ignoring my rcParams settings?

Viewed 21

In jupyter notebooks recently, I have had matplotlib ignore the rcParams. For example, I will set pl.rcParams['figure.figsize'] = (10, 8), then find that all plots come out with size (6,4). Generally, if I make a plot with some features (not a blank canvas), then set rcParams again, the settings stick.

This example shows the settings not sticking until the second time I create a figure:

pl.rcParams['figure.figsize'] = (10,8)
print(pl.rcParams['figure.figsize'])
pl.rcParams['font.size'] = 16

pl.rcParams['figure.figsize'] = (10,8)
fig = pl.figure()
pl.draw()
ax = fig.gca()
print(fig.get_size_inches())
pl.close(fig.number)

pl.rcParams['figure.figsize'] = (10,8)
fig = pl.figure()
pl.draw()
ax = fig.gca()
print(fig.get_size_inches())
pl.close(fig.number)

result:

[10.0, 8.0]
[6. 4.]
[10.  8.]

This behavior has only begun recently, so I suspect it's a change in matplotlib, but I can't pin it down. I'm using matplotlib 3.5.2.

1 Answers

I have a partial answer to this question now, though I still don't understand the origin and will accept a better answer.

If I run

%matplotlib inline

before setting the rcparams, they "stick", while if I don't specify the backend first, the rcparams are re-set to the defaults upon the first instance of figure creation.

Related