Skip to content

Avoid overwrite of matplotlib.rcParamsDefault #460

@DanShort12

Description

@DanShort12

Hi folks,

We've started using OpenMOC for some simulations, and it's broadly working very well - thanks!

I think I've hit on an issue when integrating your package with ours though, in that when I import openmoc a bunch of our plotting styles get overwritten. It looks like the issue relates to this section of code in plotter.py:

# Default matplotlib parameters to use in all plots
matplotlib_rcparams = matplotlib.rcParamsDefault
matplotlib_rcparams['font.family'] = 'sans-serif'
matplotlib_rcparams['font.weight'] = 'normal'
matplotlib_rcparams['font.size'] = 15
matplotlib_rcparams['savefig.dpi'] = 500
matplotlib_rcparams['figure.dpi'] = 500

In this you're overwriting matplotlib's default rcParams (rcParamsDefault). I suspect that isn't an intended side-effect of that code block - in particular it clashes with the functionality in e.g. matplotlib.rcdefault(), which resets the current rcParams to the matplotlib defaults. We generally reset the rcParams to defaults first when plotting in case a third party package that we've called has applied their own styling (like in the case with OpenMOC).

I think it's more likely that your intended usage is:

# Default matplotlib parameters to use in all plots
matplotlib_rcparams = matplotlib.rcParams
matplotlib_rcparams['font.family'] = 'sans-serif'
matplotlib_rcparams['font.weight'] = 'normal'
matplotlib_rcparams['font.size'] = 15
matplotlib_rcparams['savefig.dpi'] = 500
matplotlib_rcparams['figure.dpi'] = 500

This behaviour can fairly easily be reproduced by executing the following:

import matplotlib.pyplot as plt

# Make a plot without setting any rcParams (i.e. using the defaults)
plt.plot([1, 2, 3, 4])
plt.ylabel("some numbers")
plt.savefig("before.png")

# Import OpenMOC
import openmoc

# Reset rcParams to default and save (should do nothing as we didn't set any rcParams above)
plt.rcdefaults()
plt.savefig("after.png")

Note that after.png has been set to a higher resolution (thus has a larger file size) than before.png by the update to savefig.dpi in plotter.py. Of course our actual styling is more complex that that, but I think this highlights the issue.

Cheers,
Dan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions