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: fix orientation of colorbar in multiplanel plot #4981

Merged
merged 12 commits into from
Sep 13, 2024
15 changes: 14 additions & 1 deletion yt/visualization/base_plot_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,20 @@ def _set_axes(self) -> None:
self.image.axes.set_facecolor(self.colorbar_handler.background_color)

self.cax.tick_params(which="both", direction="in")
self.cb = self.figure.colorbar(self.image, self.cax)

# For creating a multipanel plot by ImageGrid
# we may need the location keyword, which was introduced since Matplotlib 3.7.0
xshaokun marked this conversation as resolved.
Show resolved Hide resolved
cb_location = getattr(self.cax, "orientation", None)
xshaokun marked this conversation as resolved.
Show resolved Hide resolved
if matplotlib.__version_info__ >= (3, 7):
cb_kwargs = {"location": cb_location}
else:
cb_kwargs = {}
if cb_location in ["top", "bottom"]:
warnings.warn(
"Colorbar orientation would be wrong in the current Matplotlib version (< 3.7.0)",
xshaokun marked this conversation as resolved.
Show resolved Hide resolved
stacklevel=2,
xshaokun marked this conversation as resolved.
Show resolved Hide resolved
)
self.cb = self.figure.colorbar(self.image, self.cax, **cb_kwargs)

cb_axis: Axis
if self.cb.orientation == "vertical":
Expand Down
18 changes: 18 additions & 0 deletions yt/visualization/tests/test_image_comp_2D_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ def test_particleprojectionplot_set_colorbar_properties():
return p.plots[field].figure


@pytest.mark.skipif(
mpl.__version_info__ < (3, 7),
reason="colorbar cannot currently be set horizontal in multi-panel plot with matplotlib older than 3.7.0",
)
@pytest.mark.parametrize("cbar_location", ["top", "bottom", "left", "right"])
@pytest.mark.mpl_image_compare
def test_multipanelplot_colorbar_orientation(cbar_location):
ds = fake_random_ds(16)
fields = [
("gas", "density"),
("gas", "velocity_x"),
("gas", "velocity_y"),
("gas", "velocity_magnitude"),
]
p = SlicePlot(ds, "z", fields)
return p.export_to_mpl_figure((2, 2), cbar_location=cbar_location)


class TestProfilePlot:
@classmethod
def setup_class(cls):
Expand Down
Loading