Skip to content

Prettyplot doesn't reinstate its environment when run a second time #181

Description

@austinstover

Bug Description / Observed Behavior

Prettyplot doesn't reset the matplotlib environment the second time it is imported and run.

Let's say you want to use latex in your prettyplots. You'll run:
mpl.rcParams.update({'text.usetex': True,}),
Make your prettyplot, and then, to be a good samaritan, you'll clean the matplotlib environment to revert it for future standard plots without latex (or prettyplot):
mpl.rcParams.update(mpl.rcParamsDefault).

Now, if you try to reimport and rerun prettyplot, the plot is no longer pretty. I believe this is due to import caching--it seems that prettyplot modifies the matplotlib rcParams upon import, instead of when run. The second time you import prettyplot, python doesn't modify the rcParams, since the import is already cached.

Steps to Reproduce

  1. Make a plot with prettyplot
  2. Reset mpl.rcParams
  3. Rerun the script (without restarting the python kernel)

This script suffices; try running it twice. It only plots properly the first time.

import matplotlib as mpl
import matplotlib.pyplot as plt
import aerosandbox.tools.pretty_plots as p

print("axes.facecolor:", mpl.rcParams["axes.facecolor"])
print("axes.grid:", mpl.rcParams["axes.grid"])
print("figure.dpi:", mpl.rcParams["figure.dpi"])

x = [0, 1, 2, 3]
y = [0, 1, 4, 9]

plt.figure(figsize=(4, 3))
plt.plot(x, y, marker="o", label="Data")
p.show_plot(
    title="Run this cell twice",
    xlabel="x",
    ylabel="y",
)

mpl.rcParams.update(mpl.rcParamsDefault)

Expected Behavior

I expect prettyplot to plot prettily each time it is run, rather than depend on backend import caching mechanics to plot prettily.

System Information

  • Operating System:
  • Python Version: 3.11.10
  • AeroSandbox Version: 4.2.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions