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

Update plot_modifications.py #5116

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Update plot_modifications.py #5116

wants to merge 3 commits into from

Conversation

tiapac
Copy link

@tiapac tiapac commented Feb 11, 2025

I noticed that making a contour plot does not allow me to use the kwarg "cmap" in the 'plot_args' dictionary.
If I add the "cmap" key work to 'plot_args' as in

s = yt.ProjectionPlot(ds, "x", ("gas","density"))	
s.annotate_contour(("gas","density"), plot_args = { "cmap":"jet"})```

returns

Traceback (most recent call last):
  File ".conda/envs/ytenv/lib/python3.12/site-packages/yt/visualization/plot_window.py", line 1285, in run_callbacks
    callback(cbw)
  File ".conda/envs/ytenv/lib/python3.12/site-packages/yt/visualization/plot_modifications.py", line 1032, in __call__
    cset = plot._axes.contour(xi, yi, zi, levels, **self.plot_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".conda/envs/ytenv/lib/python3.12/site-packages/matplotlib/__init__.py", line 1473, in inner
    return func(
           ^^^^^
  File ".conda/envs/ytenv/lib/python3.12/site-packages/matplotlib/axes/_axes.py", line 6659, in contour
    contours = mcontour.QuadContourSet(self, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".conda/envs/ytenv/lib/python3.12/site-packages/matplotlib/contour.py", line 801, in __init__
    raise ValueError('Either colors or cmap must be None')

ValueError: Either colors or cmap must be None

The cause is that by default yt partially generate said dictionary ( in yt/visualization/plot_modifications.py, line 931 ) and then add the user supplied options:

class ContourCallback(PlotCallback):
...
...
...
    def __init__(
        self,
        field: AnyFieldKey,
        levels: int = 5,
        *,
        factor: tuple[int, int] | int = 4,
        clim: tuple[float, float] | None = None,
        label: bool = False,
        take_log: bool | None = None,
        data_source: YTDataContainer | None = None,
        plot_args: dict[str, Any] | None = None,
        text_args: dict[str, Any] | None = None,
        ncont: int | None = None,  # deprecated
    ) -> None:
...
...
...
self.plot_args = {
    "colors": "black",
    "linestyles": "solid",
    **(plot_args or {}),
}

This generate a conflict in matplotlib because when "colors" is supplied, "cmap" is not accepted.

A solution might be to only add the default values if they are not supplied by the user in plot_args , as also suggested by @chrishavlin .

This is the content of the pull request.

Copy link

welcome bot commented Feb 11, 2025

Hi! Welcome, and thanks for opening this pull request. We have some guidelines for new pull requests, and soon you'll hear back about the results of our tests and continuous integration checks. Thank you for your contribution!

@tiapac tiapac closed this Feb 11, 2025
@chrishavlin
Copy link
Contributor

@tiapac did you mean to close this one?

@tiapac
Copy link
Author

tiapac commented Feb 11, 2025

Yes, I modified through the online editor, so I was worried that the indentation was not correct. I also wanted to add a bit of context to the changes for future references. I tested it locally and it's ok, so I'll reopen it.
Sorry, I am still a bit clumsy with git outside my private repositories.

@tiapac tiapac reopened this Feb 11, 2025
@chrishavlin
Copy link
Contributor

pre-commit.ci autofix

@chrishavlin
Copy link
Contributor

@tiapac I just triggered the pre-commit bot to fix the failing style check: locally you'll want to run git pull to make sure you pull those changes locally.

@tiapac
Copy link
Author

tiapac commented Feb 11, 2025

The type check test gives an error here:

Run mypy yt
  mypy yt
  shell: /usr/bin/bash -e {0}
  env:
    pythonLocation: /opt/hostedtoolcache/Python/3.10.16/x64
    PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.10.16/x64/lib/pkgconfig
    Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.16/x64
    Python[2](https://github.com/yt-project/yt/actions/runs/13268926486/job/37043480434?pr=5116#step:6:2)_ROOT_DIR: /opt/hostedtoolcache/Python/[3](https://github.com/yt-project/yt/actions/runs/13268926486/job/37043480434?pr=5116#step:6:3).10.16/x64
    Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.16/x6[4](https://github.com/yt-project/yt/actions/runs/13268926486/job/37043480434?pr=5116#step:6:4)
    LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.10.16/x64/lib
yt/visualization/plot_modifications.py: note: In member "__init__" of class "ContourCallback":
yt/visualization/plot_modifications.py:966: error: Unsupported right operand type for in ("dict[str, Any] | None")  [operator]
yt/visualization/plot_modifications.py:968: error: Unsupported right operand type for in ("dict[str, Any] | None")  [operator]
yt/utilities/performance_counters.py: note: In member "print_stats" of class "PerformanceCounters":
yt/utilities/performance_counters.py:73: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
Found 2 errors in 1 file (checked [5](https://github.com/yt-project/yt/actions/runs/13268926486/job/37043480434?pr=5116#step:6:5)16 source files)
Error: Process completed with exit code 1.

I think it is because if plot_args defaults to None, thus the in operation fails in such case.
A solution might be to switch to

if "colors" not in self.plot_args and "cmap" not in self.plot_args: ...
instead of
if "colors" not in plot_args and "cmap" not in plot_args: ... ?

@chrishavlin
Copy link
Contributor

A solution might be to switch to

if "colors" not in self.plot_args and "cmap" not in self.plot_args: ...

yup! that should fix it.

Changed ``plot_args`` to ``self.plot_args``  in the if/in check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants