@@ -736,7 +736,7 @@ def check_results(names, key):
736
736
)
737
737
738
738
739
- def _possible_x_y_plot (obj , key ):
739
+ def _possible_x_y_plot (obj , key , skip = None ):
740
740
"""Guesses a name for an x/y variable if possible."""
741
741
# in priority order
742
742
x_criteria = [
@@ -759,11 +759,20 @@ def _get_possible(accessor, criteria):
759
759
from xarray .core .utils import is_scalar
760
760
761
761
for attr , key in criteria :
762
- value = getattr (accessor , attr ).get (key )
763
- if not value or len (value ) > 1 :
762
+ values = getattr (accessor , attr ).get (key )
763
+ ax_coord_name = getattr (accessor , attr ).get (key )
764
+ if not values :
764
765
continue
765
- if not is_scalar (accessor ._obj [value [0 ]]):
766
- return value [0 ]
766
+ elif ax_coord_name :
767
+ values = [v for v in values if v in ax_coord_name ]
768
+
769
+ values = [v for v in values if v != skip ]
770
+ if len (values ) == 1 and not is_scalar (accessor ._obj [values [0 ]]):
771
+ return values [0 ]
772
+ else :
773
+ for v in values :
774
+ if not is_scalar (accessor ._obj [v ]):
775
+ return v
767
776
return None
768
777
769
778
if key == "x" :
@@ -825,9 +834,9 @@ def _plot_decorator(self, func):
825
834
826
835
@functools .wraps (func )
827
836
def _plot_wrapper (* args , ** kwargs ):
828
- def _process_x_or_y (kwargs , key ):
837
+ def _process_x_or_y (kwargs , key , skip = None ):
829
838
if key not in kwargs :
830
- kwargs [key ] = _possible_x_y_plot (self ._obj , key )
839
+ kwargs [key ] = _possible_x_y_plot (self ._obj , key , skip )
831
840
832
841
value = kwargs .get (key )
833
842
if value :
@@ -847,13 +856,15 @@ def _process_x_or_y(kwargs, key):
847
856
and (kwargs .get ("hue" ) or self ._obj .ndim == 1 )
848
857
)
849
858
if is_line_plot :
850
- if not kwargs .get ("hue" ):
851
- kwargs = _process_x_or_y (kwargs , "x" )
859
+ hue = kwargs .get ("hue" )
860
+ if "x" not in kwargs and "y" not in kwargs :
861
+ kwargs = _process_x_or_y (kwargs , "x" , skip = hue )
852
862
if not kwargs .get ("x" ):
853
- kwargs = _process_x_or_y (kwargs , "y" )
863
+ kwargs = _process_x_or_y (kwargs , "y" , skip = hue )
864
+
854
865
else :
855
- kwargs = _process_x_or_y (kwargs , "x" )
856
- kwargs = _process_x_or_y (kwargs , "y" )
866
+ kwargs = _process_x_or_y (kwargs , "x" , skip = kwargs . get ( "y" ) )
867
+ kwargs = _process_x_or_y (kwargs , "y" , skip = kwargs . get ( "x" ) )
857
868
858
869
return func (* args , ** kwargs )
859
870
0 commit comments