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

Add dataarray scatter #6778

Merged
merged 261 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 188 commits
Commits
Show all changes
261 commits
Select commit Hold shift + click to select a range
e10d5ca
allow adding any number of extra coords
Illviljan Feb 14, 2021
474928f
Explain how ds will becom darray
Illviljan Feb 14, 2021
54bd639
Update dataset_plot.py
Illviljan Feb 14, 2021
f58c52a
Merge branch 'master' into Illviljan-dataset_line_plot
Illviljan Feb 19, 2021
a8edc08
Update dataset_plot.py
Illviljan Feb 19, 2021
9c416f2
use coords for coords
Illviljan Feb 20, 2021
3ade6c4
Merge branch 'master' into Illviljan-dataset_line_plot
Illviljan Apr 2, 2021
c14ae47
Merge branch 'master' into Illviljan-dataset_line_plot
Illviljan Apr 14, 2021
93b50e2
Merge branch 'master' into Illviljan-dataset_line_plot
Illviljan May 9, 2021
7110220
Explain goal of moving ds plots to da
Illviljan May 13, 2021
76d42e5
Merge branch 'master' into Illviljan-dataset_line_plot
Illviljan May 18, 2021
b8c749d
Merge branch 'main' into Illviljan-dataset_line_plot
Illviljan Jun 24, 2021
3e90c1f
Merge branch 'main' into Illviljan-dataset_line_plot
Illviljan Jul 18, 2021
72c8e81
Update dataset_plot.py
Illviljan Jul 19, 2021
79dd87a
Update dataset_plot.py
Illviljan Jul 19, 2021
7b8fe1d
Update dataset_plot.py
Illviljan Jul 19, 2021
bf8b0ea
handle non-existant coords
Illviljan Jul 20, 2021
424a006
Update dataset_plot.py
Illviljan Jul 20, 2021
fe5bece
Look through the kwargs to find extra coords
Illviljan Jul 20, 2021
8d5f5c6
output of legend labels has changed
Illviljan Jul 20, 2021
a26992c
pop plt, comment out error test
Illviljan Jul 20, 2021
61ba8db
Merge branch 'Illviljan-dataset_line_plot' into remove_dataset_scatter
Illviljan Jul 21, 2021
f130b85
Update dataset_plot.py
Illviljan Jul 21, 2021
89515ef
Update facetgrid.py
Illviljan Jul 21, 2021
a7ee9f6
move some funcs to utils
Illviljan Jul 21, 2021
450c075
add the funcs to the moved place
Illviljan Jul 21, 2021
d70184a
Merge branch 'main' into remove_dataset_scatter
Illviljan Jul 22, 2021
5efcf12
various bugfixes
Illviljan Jul 24, 2021
ac60266
improve ds to da wrapper
Illviljan Jul 24, 2021
d33a8da
Filter kwargs
Illviljan Aug 16, 2021
c17a9bd
normalize args to be able to filter the correct args
Illviljan Aug 17, 2021
d6f2a10
Update plot.py
Illviljan Aug 17, 2021
a1ecc96
Update plot.py
Illviljan Aug 17, 2021
c2a7bae
Update dataset_plot.py
Illviljan Aug 17, 2021
2d06afa
Some fixes to string colorbar
Illviljan Aug 19, 2021
5c1fec0
Update plot.py
Illviljan Aug 19, 2021
0acb212
Check if hue is str
Illviljan Aug 21, 2021
087d789
Fix some failing tests
Illviljan Aug 22, 2021
cc572d2
Update dataset_plot.py
Illviljan Aug 22, 2021
d197c1f
Merge branch 'main' into remove_dataset_scatter
Illviljan Aug 28, 2021
6568963
Add more relevant params higher up
Illviljan Aug 28, 2021
579d2d9
Merge branch 'main' into remove_dataset_scatter
Illviljan Sep 4, 2021
05c30ae
use hue in facetgrid, normalize data
Illviljan Sep 5, 2021
4a8d8fe
Update plot.py
Illviljan Sep 8, 2021
bdff764
Move parts of scatter to a decorator
Illviljan Sep 14, 2021
e28ab8f
Merge branch 'main' into remove_dataset_scatter
Illviljan Oct 8, 2021
bf43580
Update plot.py
Illviljan Oct 8, 2021
9cb9fcd
Update plot.py
Illviljan Oct 10, 2021
e075a1f
Merge branch 'main' into remove_dataset_scatter
Illviljan Oct 12, 2021
e879a44
get scatter to work with decorator
Illviljan Oct 16, 2021
e970ce9
use correct name
Illviljan Oct 18, 2021
8cd9438
Add a Normalize class
Illviljan Oct 24, 2021
861b677
Merge branch 'main' into remove_dataset_scatter
Illviljan Oct 24, 2021
fb8a19e
skip use of Literal
Illviljan Oct 24, 2021
172b05d
remove test code
Illviljan Oct 24, 2021
b95bda6
fix lint errors
Illviljan Oct 24, 2021
bc4dc89
more linting fixes
Illviljan Oct 24, 2021
2d8f2d8
doctests fixing
Illviljan Oct 24, 2021
31230b5
Merge branch 'main' into remove_dataset_scatter
Illviljan Oct 24, 2021
5f1aeb7
Update utils.py
Illviljan Oct 24, 2021
2876920
Update plot.py
Illviljan Oct 24, 2021
e902bca
Update utils.py
Illviljan Oct 24, 2021
f118fca
Update plot.py
Illviljan Oct 24, 2021
0c3c49b
Update facetgrid.py
Illviljan Oct 24, 2021
3d77461
revert some old ideas
Illviljan Oct 24, 2021
358d788
Update utils.py
Illviljan Oct 24, 2021
f6eec55
Update plot.py
Illviljan Oct 24, 2021
cb7d7df
trim unused code
Illviljan Oct 24, 2021
a0694a3
use to_numpy instead
Illviljan Oct 24, 2021
96c9d55
more pint compats
Illviljan Oct 25, 2021
3e042c1
Merge branch 'main' into remove_dataset_scatter
Illviljan Oct 25, 2021
f610676
Merge branch 'main' into remove_dataset_scatter
Illviljan Oct 31, 2021
e9de8d3
work on facetgrid legends
Illviljan Oct 31, 2021
7c3d29e
Merge branch 'main' into remove_dataset_scatter
Illviljan Nov 2, 2021
a697d10
Merge branch 'main' into remove_dataset_scatter
Illviljan Nov 2, 2021
53a1715
facetgrid colorbar tweaks
Illviljan Nov 5, 2021
881365d
Merge branch 'main' into remove_dataset_scatter
Illviljan Nov 5, 2021
48c0cde
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 5, 2021
a10d4ce
Merge branch 'main' into remove_dataset_scatter
Illviljan Nov 6, 2021
154c4b9
Merge branch 'main' into remove_dataset_scatter
Illviljan Nov 19, 2021
a907832
Merge branch 'main' into remove_dataset_scatter
Illviljan Nov 20, 2021
8a7953e
Categoricals starts on 1 and is bounded 0,2
Illviljan Dec 1, 2021
8eee758
Handle None in Normalize
Illviljan Dec 4, 2021
b341236
Fix labels
Illviljan Dec 4, 2021
ac06a5d
Merge branch 'main' into remove_dataset_scatter
Illviljan Dec 4, 2021
9c7f787
Update plot.py
Illviljan Dec 4, 2021
d3afaf0
determine guide
Illviljan Dec 31, 2021
9789340
Merge branch 'main' into remove_dataset_scatter
Illviljan Dec 31, 2021
9828274
fix plt
Illviljan Jan 1, 2022
4791adf
Update facetgrid.py
Illviljan Jan 2, 2022
6ba3fbc
Don't be able to plot empty legends
Illviljan Jan 6, 2022
31a6d4f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 6, 2022
69766be
try out linecollection so lines behaves similar to scatter
Illviljan Jan 6, 2022
e35d0d9
linecollections half working
Illviljan Jan 6, 2022
c7c7483
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 6, 2022
5464544
Update utils.py
Illviljan Jan 7, 2022
817a4bb
Update plot.py
Illviljan Jan 7, 2022
1fa025e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 7, 2022
1961bb7
A few variations of linecollection
Illviljan Jan 9, 2022
4ff6d9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 9, 2022
d4fd066
Update plot.py
Illviljan Jan 10, 2022
12924fc
Merge branch 'main' into remove_dataset_scatter
Illviljan Jan 13, 2022
15b970d
line to utils
Illviljan Jan 15, 2022
41cd57a
Merge branch 'main' into remove_dataset_scatter
Illviljan Jan 15, 2022
a58e595
line plot changes
Illviljan Jan 19, 2022
6501393
Merge branch 'main' into remove_dataset_scatter
Illviljan Jan 19, 2022
ecc7d7c
reshape to get hues working
Illviljan Jan 20, 2022
ce4af29
Merge branch 'main' into remove_dataset_scatter
Illviljan Jan 20, 2022
d227bb3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 20, 2022
5fb6cf6
line edits legend not nice on line plots yet
Illviljan Jan 21, 2022
8f74c18
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 21, 2022
b4bdb66
Update tutorial.py
Illviljan Jan 21, 2022
76ea202
Merge branch 'remove_dataset_scatter' of https://github.com/Illviljan…
Illviljan Jan 21, 2022
dab55fc
doc changes, tuple to dict
Illviljan Jan 21, 2022
783f3cc
nice line plots and working legend
Illviljan Jan 22, 2022
101a03a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 22, 2022
8a1b310
comment out some variants
Illviljan Jan 22, 2022
475473b
Merge branch 'remove_dataset_scatter' of https://github.com/Illviljan…
Illviljan Jan 22, 2022
cd8d722
some cleanup
Illviljan Jan 22, 2022
a70ae67
Guess some dims if they weren't defined
Illviljan Jan 22, 2022
8978648
None is supposed to pass as well
Illviljan Jan 22, 2022
e6b3c2c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 22, 2022
d995745
make precommit happy
Illviljan Jan 22, 2022
7882668
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 22, 2022
f9c5aa6
Update plot.py
Illviljan Jan 22, 2022
9ef1a90
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 22, 2022
80956b0
add hist, step
Illviljan Jan 22, 2022
87498e0
handle step using repeat, remove pint errors
Illviljan Jan 22, 2022
a945b21
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 22, 2022
cd26332
handle pint
Illviljan Jan 22, 2022
a38d95c
fix some tests
Illviljan Jan 22, 2022
589c61e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 22, 2022
263d21b
Merge branch 'main' into remove_dataset_scatter
Illviljan Feb 14, 2022
ca77598
use isel instead to be independent of categoricals or not
Illviljan Feb 14, 2022
b9bb100
allow multiple primitives and filter duplicates
Illviljan Feb 14, 2022
a4e5b14
Update test_plot.py
Illviljan Feb 20, 2022
eb4264c
Copy data inside instead at init.
Illviljan Feb 20, 2022
caa9485
Histograms has counted values along y, switch around x and y labels.
Illviljan Feb 20, 2022
58c32f8
output as numpy array
Illviljan Feb 20, 2022
e490ec8
histogram outputs primitive only
Illviljan Feb 20, 2022
8ed977c
Merge branch 'main' into remove_dataset_scatter
Illviljan Mar 6, 2022
59212d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 6, 2022
43a86a0
Merge branch 'main' into remove_dataset_scatter
Illviljan Mar 16, 2022
088bb87
Update utils.py
Illviljan Mar 16, 2022
1f87e66
Merge branch 'main' into remove_dataset_scatter
Illviljan Mar 18, 2022
69ee60a
Merge branch 'main' into remove_dataset_scatter
Illviljan Mar 21, 2022
9ab511d
Merge branch 'main' into remove_dataset_scatter
Illviljan Mar 27, 2022
5d9d37d
Update facetgrid.py
Illviljan Mar 27, 2022
fa21c40
use add_labels inputs, explicit indexes now handles attrs
Illviljan Mar 27, 2022
76b7e90
colorbar in correct position
Illviljan Mar 27, 2022
e164eee
Update plot.py
Illviljan Mar 27, 2022
77fefd8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 27, 2022
cb2c5ca
Merge branch 'main' into remove_dataset_scatter
Illviljan Mar 28, 2022
a09d933
Avoid always stacking
Illviljan Apr 12, 2022
8ffc404
linecollection fixes
Illviljan Apr 12, 2022
91808f7
Merge branch 'main' into remove_dataset_scatter
Illviljan Apr 15, 2022
ff419d1
Update plot.py
Illviljan May 5, 2022
7454372
Merge branch 'main' into remove_dataset_scatter
Illviljan Jun 18, 2022
d1ee8f6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 18, 2022
0f6d2fb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 18, 2022
1023f5d
Merge branch 'main' into remove_dataset_scatter
Illviljan Jun 29, 2022
ca921b7
Merge branch 'main' into remove_dataset_scatter
Illviljan Jul 12, 2022
b85a754
Add datarray scatter
Illviljan Jul 12, 2022
f2f7232
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 12, 2022
3163fde
Update plot.py
Illviljan Jul 12, 2022
3b4be0e
Merge branch 'add_dataarray_scatter' of https://github.com/Illviljan/…
Illviljan Jul 12, 2022
68dde86
Update plot.py
Illviljan Jul 12, 2022
82e179d
out of scope stuff
Illviljan Jul 12, 2022
c37acc6
Update test_plot.py
Illviljan Jul 12, 2022
398c668
Update plot.py
Illviljan Jul 12, 2022
1b983bb
fix some tests
Illviljan Jul 12, 2022
ad7a18e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 12, 2022
378b599
Update utils.py
Illviljan Jul 12, 2022
2d92aed
Update whats-new.rst
Illviljan Jul 12, 2022
db40e40
Update utils.py
Illviljan Jul 12, 2022
3dd4de5
Merge branch 'add_dataarray_scatter' of https://github.com/Illviljan/…
Illviljan Jul 12, 2022
1df369a
Merge branch 'main' into add_dataarray_scatter
Illviljan Jul 12, 2022
c2d68f4
Update xarray/plot/facetgrid.py
Illviljan Jul 14, 2022
b81d1c0
Update plot.py
Illviljan Jul 14, 2022
3f4f24c
Merge branch 'add_dataarray_scatter' of https://github.com/Illviljan/…
Illviljan Jul 14, 2022
bfdd90a
typo
Illviljan Jul 14, 2022
e18f0c1
Apply suggestions from code review
Illviljan Jul 14, 2022
f92c28b
Update xarray/plot/utils.py
Illviljan Jul 14, 2022
0609f82
Update plot.py
Illviljan Jul 14, 2022
7dad684
Merge branch 'add_dataarray_scatter' of https://github.com/Illviljan/…
Illviljan Jul 14, 2022
3812e8e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 14, 2022
b5e6ed0
Update plot.py
Illviljan Jul 14, 2022
b876f30
Merge branch 'add_dataarray_scatter' of https://github.com/Illviljan/…
Illviljan Jul 14, 2022
624af81
Merge branch 'main' into add_dataarray_scatter
Illviljan Jul 18, 2022
a67c98f
some typing
Illviljan Jul 18, 2022
2e4ab00
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 18, 2022
dacf39b
Update facetgrid.py
Illviljan Jul 19, 2022
47e2e80
Update plot.py
Illviljan Jul 19, 2022
01a5995
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 19, 2022
bf0a5db
Convert name to string in label_from_attrs
Illviljan Jul 26, 2022
82313d0
Merge branch 'main' into add_dataarray_scatter
Illviljan Jul 30, 2022
07cb195
Update whats-new.rst
Illviljan Jul 31, 2022
53ebcf1
Merge branch 'main' into add_dataarray_scatter
Illviljan Aug 1, 2022
e5b718f
Add typing to soem interval funcs
Illviljan Aug 2, 2022
b839de5
Merge branch 'main' into add_dataarray_scatter
Illviljan Aug 2, 2022
a8a6a06
undo parse_size edits, not necessary
Illviljan Aug 3, 2022
0737a51
ax not needed
Illviljan Aug 4, 2022
53028f9
Merge branch 'main' into add_dataarray_scatter
Illviljan Aug 4, 2022
b749447
Add some typing
Illviljan Aug 14, 2022
38967da
Update utils.py
Illviljan Aug 14, 2022
0a5fdb1
Cleaner retrieval of add_labels and
Illviljan Aug 15, 2022
951408f
type hints
Illviljan Aug 15, 2022
36ac735
Fix facetgrid and normal plot not matching
Illviljan Aug 15, 2022
2635d07
Update facetgrid.py
Illviljan Aug 15, 2022
8b1c620
Merge branch 'main' into add_dataarray_scatter
Illviljan Aug 15, 2022
5af814a
Merge branch 'main' into add_dataarray_scatter
Illviljan Aug 15, 2022
1e628b4
Update plot.py
Illviljan Aug 15, 2022
f02ead7
Add typing to dataset funcs + some fixes
Illviljan Aug 15, 2022
becaf19
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 15, 2022
dcba1cf
Update dataset_plot.py
Illviljan Aug 15, 2022
2f9fc71
Merge branch 'add_dataarray_scatter' of https://github.com/Illviljan/…
Illviljan Aug 15, 2022
38ce386
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 15, 2022
8470895
Add type hints to plot1d
Illviljan Aug 16, 2022
6b536d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 16, 2022
5b63203
Update facetgrid.py
Illviljan Aug 16, 2022
09ae00d
Merge branch 'main' into add_dataarray_scatter
Illviljan Aug 17, 2022
eaa8af7
Merge branch 'add_dataarray_scatter' of https://github.com/Illviljan/…
Illviljan Aug 17, 2022
16bbce0
Update facetgrid.py
Illviljan Sep 14, 2022
5e65808
Merge branch 'main' into add_dataarray_scatter
Illviljan Sep 14, 2022
db023d9
Update facetgrid.py
Illviljan Sep 15, 2022
40f583e
remove sharex for 3d plots, not supported. Add set_lims so all data i…
Illviljan Sep 16, 2022
64eda2c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2022
25a5f73
Update facetgrid.py
Illviljan Sep 16, 2022
86ff2ab
Merge branch 'add_dataarray_scatter' of https://github.com/Illviljan/…
Illviljan Sep 16, 2022
b3b536e
Update facetgrid.py
Illviljan Sep 16, 2022
7048edd
fix typing
Illviljan Sep 16, 2022
9ba6d8d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2022
7a87958
Self should be any
Illviljan Sep 16, 2022
dd91a2f
more fixes to typing
Illviljan Sep 16, 2022
1be6746
Merge branch 'main' into add_dataarray_scatter
Illviljan Sep 17, 2022
6bd2354
Merge branch 'main' into add_dataarray_scatter
Illviljan Sep 25, 2022
699dd79
Merge branch 'main' into add_dataarray_scatter
Illviljan Sep 28, 2022
7ba3da3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 28, 2022
d8d90b5
Merge branch 'main' into add_dataarray_scatter
Illviljan Sep 29, 2022
f8aec60
Update facetgrid.py
Illviljan Sep 29, 2022
c5f652f
fix some mypy errors
Illviljan Oct 1, 2022
566103b
Merge branch 'main' into add_dataarray_scatter
Illviljan Oct 1, 2022
dbc3368
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2022
f76b5a3
Update plot.py
Illviljan Oct 2, 2022
96ad382
Merge branch 'add_dataarray_scatter' of https://github.com/Illviljan/…
Illviljan Oct 2, 2022
f5660e0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2022
34b1b98
Update plot.py
Illviljan Oct 2, 2022
ea5266c
Update whats-new.rst
Illviljan Oct 5, 2022
bd01f9c
Merge branch 'main' into add_dataarray_scatter
Illviljan Oct 5, 2022
f676f89
Merge branch 'main' into add_dataarray_scatter
andersy005 Oct 6, 2022
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
7 changes: 7 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ New Features
:py:meth:`coarsen`, :py:meth:`weighted`, :py:meth:`resample`,
(:pull:`6702`)
By `Michael Niklas <https://github.com/headtr1ck>`_.
- Add scatter plot for datarrays. Scatter plots now also supports 3d plots with
the z argument. (:pull:`6778`)
By `Jimmy Westling <https://github.com/illviljan>`_.

Breaking changes
~~~~~~~~~~~~~~~~


Deprecations
~~~~~~~~~~~~
Expand Down
196 changes: 100 additions & 96 deletions xarray/plot/dataset_plot.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,20 @@
from __future__ import annotations

import functools

import numpy as np
import pandas as pd
import inspect

from ..core.alignment import broadcast
from .facetgrid import _easy_facetgrid
from .plot import _PlotMethods
from .utils import (
_add_colorbar,
_get_nice_quiver_magnitude,
_infer_meta_data,
_parse_size,
_process_cmap_cbar_kwargs,
get_axis,
)


def _infer_scatter_data(ds, x, y, hue, markersize, size_norm, size_mapping=None):

broadcast_keys = ["x", "y"]
to_broadcast = [ds[x], ds[y]]
if hue:
to_broadcast.append(ds[hue])
broadcast_keys.append("hue")
if markersize:
to_broadcast.append(ds[markersize])
broadcast_keys.append("size")

broadcasted = dict(zip(broadcast_keys, broadcast(*to_broadcast)))

data = {"x": broadcasted["x"], "y": broadcasted["y"], "hue": None, "sizes": None}

if hue:
data["hue"] = broadcasted["hue"]

if markersize:
size = broadcasted["size"]

if size_mapping is None:
size_mapping = _parse_size(size, size_norm)

data["sizes"] = size.copy(
data=np.reshape(size_mapping.loc[size.values.ravel()].values, size.shape)
)

return data


class _Dataset_PlotMethods:
"""
Enables use of xarray.plot functions as attributes on a Dataset.
Expand Down Expand Up @@ -352,67 +319,6 @@ def plotmethod(
return newplotfunc


@_dsplot
def scatter(ds, x, y, ax, **kwargs):
"""
Scatter Dataset data variables against each other.

Wraps :py:func:`matplotlib:matplotlib.pyplot.scatter`.
"""

if "add_colorbar" in kwargs or "add_legend" in kwargs:
raise ValueError(
"Dataset.plot.scatter does not accept "
"'add_colorbar' or 'add_legend'. "
"Use 'add_guide' instead."
)

cmap_params = kwargs.pop("cmap_params")
hue = kwargs.pop("hue")
hue_style = kwargs.pop("hue_style")
markersize = kwargs.pop("markersize", None)
size_norm = kwargs.pop("size_norm", None)
size_mapping = kwargs.pop("size_mapping", None) # set by facetgrid

# Remove `u` and `v` so they don't get passed to `ax.scatter`
kwargs.pop("u", None)
kwargs.pop("v", None)

# need to infer size_mapping with full dataset
data = _infer_scatter_data(ds, x, y, hue, markersize, size_norm, size_mapping)

if hue_style == "discrete":
primitive = []
# use pd.unique instead of np.unique because that keeps the order of the labels,
# which is important to keep them in sync with the ones used in
# FacetGrid.add_legend
for label in pd.unique(data["hue"].values.ravel()):
mask = data["hue"] == label
if data["sizes"] is not None:
kwargs.update(s=data["sizes"].where(mask, drop=True).values.flatten())

primitive.append(
ax.scatter(
data["x"].where(mask, drop=True).values.flatten(),
data["y"].where(mask, drop=True).values.flatten(),
label=label,
**kwargs,
)
)

elif hue is None or hue_style == "continuous":
if data["sizes"] is not None:
kwargs.update(s=data["sizes"].values.ravel())
if data["hue"] is not None:
kwargs.update(c=data["hue"].values.ravel())

primitive = ax.scatter(
data["x"].values.ravel(), data["y"].values.ravel(), **cmap_params, **kwargs
)

return primitive


@_dsplot
def quiver(ds, x, y, ax, u, v, **kwargs):
"""Quiver plot of Dataset variables.
Expand Down Expand Up @@ -497,3 +403,101 @@ def streamplot(ds, x, y, ax, u, v, **kwargs):

# Return .lines so colorbar creation works properly
return hdl.lines


def _attach_to_plot_class(plotfunc):
"""
Set the function to the plot class and add a common docstring.

Use this decorator when relying on DataArray.plot methods for
creating the Dataset plot.

TODO: Reduce code duplication.

* The goal is to reduce code duplication by moving all Dataset
specific plots to the DataArray side and use this thin wrapper to
handle the conversion between Dataset and DataArray.
* Improve docstring handling, maybe reword the DataArray versions to
explain Datasets better.
* Consider automatically adding all _PlotMethods to
_Dataset_PlotMethods.

Parameters
----------
plotfunc : function
Function that returns a finished plot primitive.
"""
# Build on the original docstring:
original_doc = getattr(_PlotMethods, plotfunc.__name__, None)
commondoc = original_doc.__doc__
if commondoc is not None:
doc_warning = (
f"This docstring was copied from xr.DataArray.plot.{original_doc.__name__}."
" Some inconsistencies may exist."
)
# Add indentation so it matches the original doc:
commondoc = f"\n\n {doc_warning}\n\n {commondoc}"
else:
commondoc = ""
plotfunc.__doc__ = (
f" {plotfunc.__doc__}\n\n"
" The `y` DataArray will be used as base,"
" any other variables are added as coords.\n\n"
f"{commondoc}"
)

@functools.wraps(plotfunc)
def plotmethod(self, *args, **kwargs):
return plotfunc(self._ds, *args, **kwargs)

# Add to class _PlotMethods
setattr(_Dataset_PlotMethods, plotmethod.__name__, plotmethod)


def _normalize_args(plotmethod, args, kwargs):
from ..core.dataarray import DataArray

# Determine positional arguments keyword by inspecting the
# signature of the plotmethod:
locals_ = dict(
inspect.signature(getattr(DataArray().plot, plotmethod))
.bind(*args, **kwargs)
.arguments.items()
)
locals_.update(locals_.pop("kwargs", {}))

return locals_


def _temp_dataarray(ds, y, locals_):
"""Create a temporary datarray with extra coords."""
from ..core.dataarray import DataArray

# Base coords:
coords = dict(ds.coords)

# Add extra coords to the DataArray from valid kwargs, if using all
# kwargs there is a risk that we add unneccessary dataarrays as
# coords straining RAM further for example:
# ds.both and extend="both" would add ds.both to the coords:
valid_coord_kwargs = {"x", "z", "markersize", "hue", "row", "col", "u", "v"}
for k in locals_.keys() & valid_coord_kwargs:
Illviljan marked this conversation as resolved.
Show resolved Hide resolved
key = locals_[k]
if ds.data_vars.get(key) is not None:
coords[key] = ds[key]

# The dataarray has to include all the dims. Broadcast to that shape
# and add the additional coords:
_y = ds[y].broadcast_like(ds)

return DataArray(_y, coords=coords)


@_attach_to_plot_class
def scatter(ds, x, y, *args, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how the actual scatter function is only 6 lines or so 😀

"""Line plot Dataset data variables against each other."""
kwargs.update(x=x)
locals_ = _normalize_args("scatter", args, kwargs)
da = _temp_dataarray(ds, y, locals_)

return da.plot.scatter(*locals_.pop("args", ()), **locals_)
Loading