20
20
from .themes import theme
21
21
22
22
if typing .TYPE_CHECKING :
23
- from typing import Any , Iterable , Literal , Optional
23
+ from typing import Any , Iterable , Literal , Optional , Sequence
24
24
25
25
from plotnine .typing import DataLike
26
26
@@ -32,8 +32,8 @@ def qplot(
32
32
y : Optional [str | Iterable [Any ] | range ] = None ,
33
33
data : Optional [DataLike ] = None ,
34
34
facets : str = "" ,
35
- margins : bool | list [str ] = False ,
36
- geom : str | list [ str ] | tuple [str ] = "auto" ,
35
+ margins : bool | Sequence [str ] = False ,
36
+ geom : str | Sequence [str ] = "auto" ,
37
37
xlim : Optional [tuple [float , float ]] = None ,
38
38
ylim : Optional [tuple [float , float ]] = None ,
39
39
log : Optional [Literal ["x" , "y" , "xy" ]] = None ,
@@ -126,18 +126,15 @@ def I(value: Any) -> Any:
126
126
data = pd .DataFrame ()
127
127
128
128
# Work out plot data, and modify aesthetics, if necessary
129
- def replace_auto (lst : list [str ], str2 : str ) -> list [str ]:
129
+ def replace_auto (lst : Sequence [str ], str2 : str ) -> Sequence [str ]:
130
130
"""
131
131
Replace all occurrences of 'auto' in with str2
132
132
"""
133
- for i , value in enumerate (lst ):
134
- if value == "auto" :
135
- lst [i ] = str2
136
- return lst
133
+ return tuple (str2 if x == "auto" else x for x in lst )
137
134
138
135
if "auto" in geom :
139
136
if "sample" in aesthetics :
140
- replace_auto (geom , "qq" )
137
+ geom = replace_auto (geom , "qq" )
141
138
elif y is None :
142
139
# If x is discrete we choose geom_bar &
143
140
# geom_histogram otherwise. But we need to
@@ -156,11 +153,8 @@ def replace_auto(lst: list[str], str2: str) -> list[str]:
156
153
elif not hasattr (aesthetics ["x" ], "dtype" ):
157
154
x = np .asarray (aesthetics ["x" ])
158
155
159
- if array_kind .discrete (x ):
160
- replace_auto (geom , "bar" )
161
- else :
162
- replace_auto (geom , "histogram" )
163
-
156
+ name = "bar" if array_kind .discrete (x ) else "histogram"
157
+ geom = replace_auto (geom , name )
164
158
else :
165
159
if x is None :
166
160
if isinstance (aesthetics ["y" ], typing .Sized ):
@@ -171,7 +165,7 @@ def replace_auto(lst: list[str], str2: str) -> list[str]:
171
165
# We could solve the issue in layer.compute_aesthetics
172
166
# but it is not worth the extra complexity
173
167
raise PlotnineError ("Cannot infer how long x should be." )
174
- replace_auto (geom , "point" )
168
+ geom = replace_auto (geom , "point" )
175
169
176
170
p : ggplot = ggplot (data , aes (** aesthetics ))
177
171
p .environment = environment
0 commit comments