@@ -654,40 +654,37 @@ def save(self, fname, **kwargs) -> Plot:
654
654
655
655
def plot (self , pyplot = False ) -> Plotter :
656
656
"""
657
- Compile the plot and return the :class:`Plotter` engine.
658
-
657
+ Compile the plot spec and return a Plotter object.
659
658
"""
660
659
# TODO if we have _target object, pyplot should be determined by whether it
661
660
# is hooked into the pyplot state machine (how do we check?)
662
661
663
662
plotter = Plotter (pyplot = pyplot )
664
663
664
+ # Process the variable assignments and initialize the figure
665
665
common , layers = plotter ._extract_data (self )
666
666
plotter ._setup_figure (self , common , layers )
667
667
668
+ # Process the scale spec for coordinate variables and transform their data
668
669
coord_vars = [v for v in self ._variables if re .match (r"^x|y" , v )]
669
670
plotter ._setup_scales (self , common , layers , coord_vars )
670
671
672
+ # Apply statistical transform(s)
671
673
plotter ._compute_stats (self , layers )
672
674
673
- other_vars = set () # TODO move this into a method
674
- for layer in layers :
675
- if layer ["data" ].frame .empty and layer ["data" ].frames :
676
- for df in layer ["data" ].frames .values ():
677
- other_vars .update (df .columns )
678
- else :
679
- other_vars .update (layer ["data" ].frame .columns )
680
- other_vars -= set (coord_vars )
681
- plotter ._setup_scales (self , common , layers , list (other_vars ))
675
+ # Process scale spec for semantic variables and coordinates computed by stat
676
+ plotter ._setup_scales (self , common , layers )
682
677
683
678
# TODO Remove these after updating other methods
684
679
# ---- Maybe have debug= param that attaches these when True?
685
680
plotter ._data = common
686
681
plotter ._layers = layers
687
682
683
+ # Process the data for each layer and add matplotlib artists
688
684
for layer in layers :
689
685
plotter ._plot_layer (self , layer )
690
686
687
+ # Add various figure decorations
691
688
plotter ._make_legend (self )
692
689
plotter ._finalize_figure (self )
693
690
@@ -696,7 +693,6 @@ def plot(self, pyplot=False) -> Plotter:
696
693
def show (self , ** kwargs ) -> None :
697
694
"""
698
695
Render and display the plot.
699
-
700
696
"""
701
697
# TODO make pyplot configurable at the class level, and when not using,
702
698
# import IPython.display and call on self to populate cell output?
@@ -1001,9 +997,23 @@ def _get_subplot_data(self, df, var, view, share_state):
1001
997
return seed_values
1002
998
1003
999
def _setup_scales (
1004
- self , p : Plot , common : PlotData , layers : list [Layer ], variables : list [str ],
1000
+ self , p : Plot ,
1001
+ common : PlotData ,
1002
+ layers : list [Layer ],
1003
+ variables : list [str ] | None = None ,
1005
1004
) -> None :
1006
1005
1006
+ if variables is None :
1007
+ # Add variables that have data but not a scale, which happens
1008
+ # because this method can be called multiple time, to handle
1009
+ # variables added during the Stat transform.
1010
+ variables = []
1011
+ for layer in layers :
1012
+ variables .extend (layer ["data" ].frame .columns )
1013
+ for df in layer ["data" ].frames .values ():
1014
+ variables .extend (v for v in df if v not in variables )
1015
+ variables = [v for v in variables if v not in self ._scales ]
1016
+
1007
1017
for var in variables :
1008
1018
1009
1019
# Determine whether this is a coordinate variable
@@ -1028,11 +1038,9 @@ def _setup_scales(
1028
1038
cols = [var , "col" , "row" ]
1029
1039
parts = [common .frame .filter (cols )]
1030
1040
for layer in layers :
1031
- if layer ["data" ].frame .empty and layer ["data" ].frames :
1032
- for df in layer ["data" ].frames .values ():
1033
- parts .append (df .filter (cols ))
1034
- else :
1035
- parts .append (layer ["data" ].frame .filter (cols ))
1041
+ parts .append (layer ["data" ].frame .filter (cols ))
1042
+ for df in layer ["data" ].frames .values ():
1043
+ parts .append (df .filter (cols ))
1036
1044
var_df = pd .concat (parts , ignore_index = True )
1037
1045
1038
1046
prop = PROPERTIES [prop_key ]
0 commit comments