Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: yt may leak matplotlib configuration globally at runtime #4459

Closed
neutrinoceros opened this issue Jun 2, 2023 · 0 comments · Fixed by #4475 · May be fixed by #4460
Closed

BUG: yt may leak matplotlib configuration globally at runtime #4459

neutrinoceros opened this issue Jun 2, 2023 · 0 comments · Fixed by #4475 · May be fixed by #4460

Comments

@neutrinoceros
Copy link
Member

Bug report

Bug summary

Matplotlib contour styles for monochrome contour sets can be changed globally by yt at runtime (this is triggered by importing from yt.visualization._mpl_imports, which isn't implied by import yt).
The problematic line is very obviously:

matplotlib.rc("contour", negative_linestyle="solid")

(in fact I spotted the problem in the code first, and then worked on a script to show its undesired effect)

Code for reproduction

# adapted from https://matplotlib.org/stable/gallery/images_contours_and_fields/contour_label_demo.html#sphx-glr-gallery-images-contours-and-fields-contour-label-demo-py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import yt

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
zm = Z.mean()
Zp = Z - zm

def make_figure(filename):
    # Basic contour plot
    fig, ax = plt.subplots()
    CS = ax.contour(X, Y, Zp, linestyles=None, colors="black")
    #assert CS.negative_linestyles == "dashed"

    print(f"saving to {filename}")
    fig.savefig(filename)

# draw the figure
make_figure("/tmp/pre.png")

# do stuff with yt
ds = yt.load_sample("IsolatedGalaxy")
yt.SlicePlot(ds, "z", "density").save("/tmp/")

# try the pure-mpl figure again
make_figure("/tmp/post.png")

Actual outcome
pre
post

Expected outcome

pure matplotlib graphs should not be affected by yt at runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant