def get_scale(self, gg):
"""
Create a scale
"""
# This method does some introspection to save users from
# scale mismatch error. This could happen when the
# aesthetic is mapped to a categorical but the limits
# are not provided in categorical form. We only handle
# the case where the mapping uses an expression to
# conver to categorical e.g `aes(color='factor(cyl)')`.
# However if `'cyl'` column is a categorical and the
# mapping is `aes(color='cyl')`, that will result in
# an error. If later case proves common enough then we
# could inspect the data and be clever based on that too!!
ae = self.aesthetic
series = pd.Series(self.limits)
ae_values = []
# Look through all the mappings for this aesthetic,
# if we detect any factor stuff then we convert the
# limits data to categorical so that the right scale
# can be choosen. This should take care of the most
# common use cases.
for layer in gg.layers:
with suppress(KeyError):
value = layer.mapping[ae]
if isinstance(value, six.string_types):
ae_values.append(value)
for value in ae_values:
if ('factor(' in value or
'Categorical(' in value):
series = pd.Categorical(series)
break
return make_scale(self.aesthetic,
series,
limits=self.limits,
trans=self.trans)
评论列表
文章目录