Skip to content

Commit 7683442

Browse files
Illviljanpre-commit-ci[bot]jhammanmathause
authored
Use plt.rc_context for default styles (#7318)
* Use plt.rc_context for default styles * Add tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Explain tests * Check that args are prioritized * Use rc_context as decorator * Using as decorator is unfortunately not lazy, reverting * Add test for facetgrid as well * Update whats-new.rst * Update whats-new.rst * Update xarray/tests/test_plot.py Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com> * import assert_no_warnings --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Joe Hamman <joe@earthmover.io> Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>
1 parent 637095f commit 7683442

File tree

3 files changed

+66
-21
lines changed

3 files changed

+66
-21
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ Bug fixes
194194
By `Michael Niklas <https://github.com/headtr1ck>`_.
195195
- Fix static typing of :py:meth:`xr.polyval` (:issue:`7312`, :pull:`7315`).
196196
By `Michael Niklas <https://github.com/headtr1ck>`_.
197+
- Fix matplotlib raising a UserWarning when plotting a scatter plot
198+
with an unfilled marker (:issue:`7313`, :pull:`7318`).
199+
By `Jimmy Westling <https://github.com/illviljan>`_.
197200
- Fix multiple reads on fsspec S3 files by resetting file pointer to 0 when reading file streams (:issue:`6813`, :pull:`7304`).
198201
By `David Hoese <https://github.com/djhoese>`_ and `Wei Ji Leong <https://github.com/weiji14>`_.
199202
- Fix :py:meth:`Dataset.assign_coords` resetting all dimension coordinates to default (pandas) index (:issue:`7346`, :pull:`7347`).

xarray/plot/dataarray_plot.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
)
5353
from xarray.plot.facetgrid import FacetGrid
5454

55+
_styles: MutableMapping[str, Any] = {
56+
# Add a white border to make it easier seeing overlapping markers:
57+
"scatter.edgecolors": "w",
58+
}
59+
5560

5661
def _infer_line_data(
5762
darray: DataArray, x: Hashable | None, y: Hashable | None, hue: Hashable | None
@@ -883,6 +888,8 @@ def newplotfunc(
883888
# All 1d plots in xarray share this function signature.
884889
# Method signature below should be consistent.
885890

891+
plt = import_matplotlib_pyplot()
892+
886893
if subplot_kws is None:
887894
subplot_kws = dict()
888895

@@ -894,6 +901,7 @@ def newplotfunc(
894901
allargs = locals().copy()
895902
allargs.update(allargs.pop("kwargs"))
896903
allargs.pop("darray")
904+
allargs.pop("plt")
897905
allargs["plotfunc"] = globals()[plotfunc.__name__]
898906

899907
return _easy_facetgrid(darray, kind="plot1d", **allargs)
@@ -973,24 +981,25 @@ def newplotfunc(
973981
ckw = {vv: cmap_params[vv] for vv in ("vmin", "vmax", "norm", "cmap")}
974982
cmap_params_subset.update(**ckw)
975983

976-
if z is not None:
977-
if ax is None:
978-
subplot_kws.update(projection="3d")
979-
ax = get_axis(figsize, size, aspect, ax, **subplot_kws)
980-
# Using 30, 30 minimizes rotation of the plot. Making it easier to
981-
# build on your intuition from 2D plots:
982-
ax.view_init(azim=30, elev=30, vertical_axis="y")
983-
else:
984-
ax = get_axis(figsize, size, aspect, ax, **subplot_kws)
985-
986-
primitive = plotfunc(
987-
xplt,
988-
yplt,
989-
ax=ax,
990-
add_labels=add_labels,
991-
**cmap_params_subset,
992-
**kwargs,
993-
)
984+
with plt.rc_context(_styles):
985+
if z is not None:
986+
if ax is None:
987+
subplot_kws.update(projection="3d")
988+
ax = get_axis(figsize, size, aspect, ax, **subplot_kws)
989+
# Using 30, 30 minimizes rotation of the plot. Making it easier to
990+
# build on your intuition from 2D plots:
991+
ax.view_init(azim=30, elev=30, vertical_axis="y")
992+
else:
993+
ax = get_axis(figsize, size, aspect, ax, **subplot_kws)
994+
995+
primitive = plotfunc(
996+
xplt,
997+
yplt,
998+
ax=ax,
999+
add_labels=add_labels,
1000+
**cmap_params_subset,
1001+
**kwargs,
1002+
)
9941003

9951004
if np.any(np.asarray(add_labels)) and add_title:
9961005
ax.set_title(darray._title_for_slice())
@@ -1233,9 +1242,6 @@ def scatter(
12331242
hueplt: DataArray | None = kwargs.pop("hueplt", None)
12341243
sizeplt: DataArray | None = kwargs.pop("sizeplt", None)
12351244

1236-
# Add a white border to make it easier seeing overlapping markers:
1237-
kwargs.update(edgecolors=kwargs.pop("edgecolors", "w"))
1238-
12391245
if hueplt is not None:
12401246
kwargs.update(c=hueplt.to_numpy().ravel())
12411247

xarray/tests/test_plot.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from xarray.tests import (
3131
assert_array_equal,
3232
assert_equal,
33+
assert_no_warnings,
3334
requires_cartopy,
3435
requires_cftime,
3536
requires_matplotlib,
@@ -3204,3 +3205,38 @@ def test_facetgrid_axes_raises_deprecation_warning() -> None:
32043205
ds = xr.tutorial.scatter_example_dataset()
32053206
g = ds.plot.scatter(x="A", y="B", col="x")
32063207
g.axes
3208+
3209+
3210+
@requires_matplotlib
3211+
def test_plot1d_default_rcparams() -> None:
3212+
import matplotlib as mpl
3213+
3214+
ds = xr.tutorial.scatter_example_dataset(seed=42)
3215+
3216+
with figure_context():
3217+
# scatter markers should by default have white edgecolor to better
3218+
# see overlapping markers:
3219+
fig, ax = plt.subplots(1, 1)
3220+
ds.plot.scatter(x="A", y="B", marker="o", ax=ax)
3221+
np.testing.assert_allclose(
3222+
ax.collections[0].get_edgecolor(), mpl.colors.to_rgba_array("w")
3223+
)
3224+
3225+
# Facetgrids should have the default value as well:
3226+
fg = ds.plot.scatter(x="A", y="B", col="x", marker="o")
3227+
ax = fg.axs.ravel()[0]
3228+
np.testing.assert_allclose(
3229+
ax.collections[0].get_edgecolor(), mpl.colors.to_rgba_array("w")
3230+
)
3231+
3232+
# scatter should not emit any warnings when using unfilled markers:
3233+
with assert_no_warnings():
3234+
fig, ax = plt.subplots(1, 1)
3235+
ds.plot.scatter(x="A", y="B", ax=ax, marker="x")
3236+
3237+
# Prioritize edgecolor argument over default plot1d values:
3238+
fig, ax = plt.subplots(1, 1)
3239+
ds.plot.scatter(x="A", y="B", marker="o", ax=ax, edgecolor="k")
3240+
np.testing.assert_allclose(
3241+
ax.collections[0].get_edgecolor(), mpl.colors.to_rgba_array("k")
3242+
)

0 commit comments

Comments
 (0)