Skip to content

Fix some scatter plot issues #7167

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

Merged
merged 6 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions xarray/plot/dataarray_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Iterable,
Literal,
MutableMapping,
cast,
overload,
)

Expand Down Expand Up @@ -925,7 +926,7 @@ def newplotfunc(

_is_facetgrid = kwargs.pop("_is_facetgrid", False)

if markersize is not None:
if plotfunc.__name__ == "scatter":
size_ = markersize
size_r = _MARKERSIZE_RANGE
else:
Expand Down Expand Up @@ -960,7 +961,7 @@ def newplotfunc(

cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs(
plotfunc,
hueplt_norm.values.data,
cast("DataArray", hueplt_norm.values).data,
**locals(),
)

Expand Down Expand Up @@ -1013,13 +1014,7 @@ def newplotfunc(
)

if add_legend_:
if plotfunc.__name__ == "hist":
ax.legend(
handles=primitive[-1],
labels=list(hueplt_norm.values.to_numpy()),
title=label_from_attrs(hueplt_norm.data),
)
elif plotfunc.__name__ in ["scatter", "line"]:
if plotfunc.__name__ in ["scatter", "line"]:
_add_legend(
hueplt_norm
if add_legend or not add_colorbar_
Expand All @@ -1030,11 +1025,26 @@ def newplotfunc(
plotfunc=plotfunc.__name__,
)
else:
ax.legend(
handles=primitive,
labels=list(hueplt_norm.values.to_numpy()),
title=label_from_attrs(hueplt_norm.data),
)
hueplt_norm_values: list[np.ndarray | None]
if hueplt_norm.data is not None:
hueplt_norm_values = list(
cast("DataArray", hueplt_norm.data).to_numpy()
)
else:
hueplt_norm_values = [hueplt_norm.data]

if plotfunc.__name__ == "hist":
ax.legend(
handles=primitive[-1],
labels=hueplt_norm_values,
title=label_from_attrs(hueplt_norm.data),
)
else:
ax.legend(
handles=primitive,
labels=hueplt_norm_values,
title=label_from_attrs(hueplt_norm.data),
)

_update_axes(
ax, xincrease, yincrease, xscale, yscale, xticks, yticks, xlim, ylim
Expand Down
37 changes: 25 additions & 12 deletions xarray/plot/facetgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Iterable,
Literal,
TypeVar,
cast,
)

import numpy as np
Expand Down Expand Up @@ -41,6 +42,9 @@
from matplotlib.quiver import QuiverKey
from matplotlib.text import Annotation

from ..core.dataarray import DataArray


# Overrides axes.labelsize, xtick.major.size, ytick.major.size
# from mpl.rcParams
_FONTSIZE = "small"
Expand Down Expand Up @@ -402,18 +406,24 @@ def map_plot1d(
hueplt_norm = _Normalize(hueplt)
self._hue_var = hueplt
cbar_kwargs = kwargs.pop("cbar_kwargs", {})
if not hueplt_norm.data_is_numeric:
# TODO: Ticks seems a little too hardcoded, since it will always
# show all the values. But maybe it's ok, since plotting hundreds
# of categorical data isn't that meaningful anyway.
cbar_kwargs.update(format=hueplt_norm.format, ticks=hueplt_norm.ticks)
kwargs.update(levels=hueplt_norm.levels)
if "label" not in cbar_kwargs:
cbar_kwargs["label"] = label_from_attrs(hueplt_norm.data)
cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs(
func, hueplt_norm.values.to_numpy(), cbar_kwargs=cbar_kwargs, **kwargs
)
self._cmap_extend = cmap_params.get("extend")

if hueplt_norm.data is not None:
if not hueplt_norm.data_is_numeric:
# TODO: Ticks seems a little too hardcoded, since it will always
# show all the values. But maybe it's ok, since plotting hundreds
# of categorical data isn't that meaningful anyway.
cbar_kwargs.update(format=hueplt_norm.format, ticks=hueplt_norm.ticks)
kwargs.update(levels=hueplt_norm.levels)

cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs(
func,
cast("DataArray", hueplt_norm.values).data,
cbar_kwargs=cbar_kwargs,
**kwargs,
)
self._cmap_extend = cmap_params.get("extend")
else:
cmap_params = {}

# Handle sizes:
_size_r = _MARKERSIZE_RANGE if func.__name__ == "scatter" else _LINEWIDTH_RANGE
Expand Down Expand Up @@ -513,6 +523,9 @@ def map_plot1d(

if add_colorbar:
# Colorbar is after legend so it correctly fits the plot:
if "label" not in cbar_kwargs:
cbar_kwargs["label"] = label_from_attrs(hueplt_norm.data)

self.add_colorbar(**cbar_kwargs)

return self
Expand Down
Loading