altair.Key#
- class altair.Key(shorthand=Undefined, aggregate=Undefined, bandPosition=Undefined, bin=Undefined, field=Undefined, timeUnit=Undefined, title=Undefined, type=Undefined, **kwds)#
Key 模式封装器。
用于定义数据字段、其类型以及编码通道转换的对象。
- 参数:
- shorthandstr, dict, Sequence[str],
RepeatRef
field, aggregate, 和 type 的简写
- aggregatedict,
Aggregate
,ArgmaxDef
,ArgminDef
,NonArgAggregateOp
, Literal[‘average’, ‘count’, ‘distinct’, ‘max’, ‘mean’, ‘median’, ‘min’, ‘missing’, ‘product’, ‘q1’, ‘q3’, ‘ci0’, ‘ci1’, ‘stderr’, ‘stdev’, ‘stdevp’, ‘sum’, ‘valid’, ‘values’, ‘variance’, ‘variancep’, ‘exponential’, ‘exponentialb’] 字段的聚合函数(例如,
"mean"
,"sum"
,"median"
,"min"
,"max"
,"count"
)。默认值:
undefined
(None)另请参阅: aggregate 文档。
- bandPositionfloat
在堆叠、分箱、时间单位或带状比例尺(band scale)上的相对位置。例如,如果设置为
0
,标记将位于带状的起始位置;如果设置为0.5
,则位于带状的中间位置。- binbool, dict, Literal[‘binned’],
BinParams
, None 一个用于对
quantitative
(定量)字段进行分箱的标志,一个定义分箱参数的对象,或者表示x
或y
通道的数据在导入 Vega-Lite 之前已经分箱("binned"
)。如果为
true
,将应用默认的分箱参数。如果为
"binned"
,表示x
(或y
)通道的数据已经分箱。可以将箱的起始字段映射到x
(或y
),将箱的结束字段映射到x2
(或y2
)。比例尺和轴的格式将类似于 Vega-Lite 中的分箱。要根据分箱步长调整轴刻度,还可以设置轴的 tickMinStep 属性。
默认值:
false
另请参阅: bin 文档。
- fieldstr, dict,
Field
,FieldName
,RepeatRef
必需。一个字符串,定义从中提取数据值的字段名称,或一个对象,定义从 repeat 运算符获得的迭代值。
另请参阅: field 文档。
注意: 1) 点 (
.
) 和方括号 ([
和]
) 可用于访问嵌套对象(例如,"field": "foo.bar"
和"field": "foo['bar']"
)。如果字段名称包含点或方括号但不是嵌套的,可以使用\\
转义点和方括号(例如,"a\\.b"
和"a\\[0\\]"
)。有关转义的更多详细信息,请参阅field 文档。2) 如果aggregate
是count
,则field
不是必需的。- timeUnitdict,
TimeUnit
,MultiTimeUnit
,BinnedTimeUnit
,SingleTimeUnit
,TimeUnitParams
,UtcMultiTimeUnit
,UtcSingleTimeUnit
,LocalMultiTimeUnit
,LocalSingleTimeUnit
, Literal[‘binnedutcyear’, ‘binnedutcyearquarter’, ‘binnedutcyearquartermonth’, ‘binnedutcyearmonth’, ‘binnedutcyearmonthdate’, ‘binnedutcyearmonthdatehours’, ‘binnedutcyearmonthdatehoursminutes’, ‘binnedutcyearmonthdatehoursminutesseconds’, ‘binnedutcyearweek’, ‘binnedutcyearweekday’, ‘binnedutcyearweekdayhours’, ‘binnedutcyearweekdayhoursminutes’, ‘binnedutcyearweekdayhoursminutesseconds’, ‘binnedutcyeardayofyear’, ‘binnedyear’, ‘binnedyearquarter’, ‘binnedyearquartermonth’, ‘binnedyearmonth’, ‘binnedyearmonthdate’, ‘binnedyearmonthdatehours’, ‘binnedyearmonthdatehoursminutes’, ‘binnedyearmonthdatehoursminutesseconds’, ‘binnedyearweek’, ‘binnedyearweekday’, ‘binnedyearweekdayhours’, ‘binnedyearweekdayhoursminutes’, ‘binnedyearweekdayhoursminutesseconds’, ‘binnedyeardayofyear’, ‘utcyear’, ‘utcquarter’, ‘utcmonth’, ‘utcweek’, ‘utcday’, ‘utcdayofyear’, ‘utcdate’, ‘utchours’, ‘utcminutes’, ‘utcseconds’, ‘utcmilliseconds’, ‘year’, ‘quarter’, ‘month’, ‘week’, ‘day’, ‘dayofyear’, ‘date’, ‘hours’, ‘minutes’, ‘seconds’, ‘milliseconds’, ‘utcyearquarter’, ‘utcyearquartermonth’, ‘utcyearmonth’, ‘utcyearmonthdate’, ‘utcyearmonthdatehours’, ‘utcyearmonthdatehoursminutes’, ‘utcyearmonthdatehoursminutesseconds’, ‘utcyearweek’, ‘utcyearweekday’, ‘utcyearweekdayhours’, ‘utcyearweekdayhoursminutes’, ‘utcyearweekdayhoursminutesseconds’, ‘utcyeardayofyear’, ‘utcquartermonth’, ‘utcmonthdate’, ‘utcmonthdatehours’, ‘utcmonthdatehoursminutes’, ‘utcmonthdatehoursminutesseconds’, ‘utcweekday’, ‘utcweekdayhours’, ‘utcweekdayhoursminutes’, ‘utcweekdayhoursminutesseconds’, ‘utcdayhours’, ‘utcdayhoursminutes’, ‘utcdayhoursminutesseconds’, ‘utchoursminutes’, ‘utchoursminutesseconds’, ‘utcminutesseconds’, ‘utcsecondsmilliseconds’, ‘yearquarter’, ‘yearquartermonth’, ‘yearmonth’, ‘yearmonthdate’, ‘yearmonthdatehours’, ‘yearmonthdatehoursminutes’, ‘yearmonthdatehoursminutesseconds’, ‘yearweek’, ‘yearweekday’, ‘yearweekdayhours’, ‘yearweekdayhoursminutes’, ‘yearweekdayhoursminutesseconds’, ‘yeardayofyear’, ‘quartermonth’, ‘monthdate’, ‘monthdatehours’, ‘monthdatehoursminutes’, ‘monthdatehoursminutesseconds’, ‘weekday’, ‘weekdayhours’, ‘weekdayhoursminutes’, ‘weekdayhoursminutesseconds’, ‘dayhours’, ‘dayhoursminutes’, ‘dayhoursminutesseconds’, ‘hoursminutes’, ‘hoursminutesseconds’, ‘minutesseconds’, ‘secondsmilliseconds’] 时间单位(例如,
year
,yearmonth
,month
,hours
)用于 temporal(时间)字段。或将temporal 字段强制转换为 ordinal(有序)类型。默认值:
undefined
(None)另请参阅: timeUnit 文档。
- titlestr,
Text
, Sequence[str], None 字段的标题。如果为
null
,则移除标题。默认值: 从字段名称和转换函数(
aggregate
、bin
和timeUnit
)派生。如果字段具有聚合函数,则函数作为标题的一部分显示(例如,"Sum of Profit"
)。如果字段经过分箱或应用了时间单位,则应用的函数显示在括号中(例如,"Profit (binned)"
,"Transaction Date (year-month)"
)。否则,标题仅为字段名称。注意:
1) 可以通过在 config 中提供 fieldTitle 属性或通过 compile 函数的选项提供 fieldTitle 函数来自定义默认的字段标题格式。
2) 如果字段定义的
title
和轴、头部或图例的title
都已定义,则使用轴/头部/图例的标题。- type
StandardType
, Literal[‘quantitative’, ‘ordinal’, ‘temporal’, ‘nominal’] 编码字段或常数值(
datum
)的测量类型("quantitative"
(定量),"temporal"
(时间),"ordinal"
(有序), 或"nominal"
(名义))。对于编码‘geoshape’,也可以是"geojson"
类型。Vega-Lite 在许多情况下会自动推断数据类型,如下文所述。但是,以下情况需要为字段指定类型:(1) 字段不是名义类型,并且字段编码未指定
aggregate
(argmin
和argmax
除外)、bin
、比例尺类型、自定义sort
顺序或timeUnit
;或 (2) 如果希望对带有bin
或timeUnit
的字段使用有序比例尺。默认值
1) 对于数据
field
,除非字段编码具有满足以下条件的aggregate
、channel
、bin
、比例尺类型、sort
或timeUnit
,否则默认数据类型为"nominal"
(名义)。如果 (1) 编码字段包含
bin
或aggregate
("argmin"
和"argmax"
除外),(2) 编码通道是latitude
(纬度)或longitude
(经度)通道,或 (3) 指定的比例尺类型是定量比例尺,则默认类型为"quantitative"
(定量)。如果 (1) 编码字段包含
timeUnit
(时间单位),或 (2) 指定的比例尺类型是时间或 UTC 比例尺,则默认类型为"temporal"
(时间)。如果 (1) 编码字段包含自定义排序顺序,(2) 指定的比例尺类型是有序/点状/带状比例尺,或 (3) 编码通道是
order
,则默认类型为"ordinal"
(有序)。
对于数据域中的常数值(
datum
)
如果 datum 是数字,则为
"quantitative"
(定量)如果 datum 是字符串,则为
"nominal"
(名义)如果 datum 是日期时间对象,则为
"temporal"
(时间)
注意
数据
type
描述的是数据的语义,而不是原始数据类型(数字、字符串等)。相同的原始数据类型可以有不同的测量类型。例如,数值数据可以表示定量、有序或名义数据。temporal 字段的数据值可以是日期时间字符串(例如,
"2015-03-07 12:32:17"
,"17:01"
,"2015-03-16"
."2015"
)或时间戳数字(例如,1552199579097
)。与 bin 一起使用时,
type
属性可以是"quantitative"
(定量,用于线性分箱比例尺)或“ordinal”(有序,用于有序分箱比例尺)。与 timeUnit 一起使用时,
type
属性可以是"temporal"
(时间,默认,用于时间比例尺)或“ordinal”(有序,用于有序比例尺)。与 aggregate 一起使用时,
type
属性指聚合后的数据类型。例如,我们可以使用{"aggregate": "distinct", "field": "cat"}
计算分类字段"cat"
的distinct
(唯一值计数)。聚合输出的"type"
是"quantitative"
(定量)。辅助通道(例如,
x2
,y2
,xError
,yError
)没有type
属性,因为它们的类型必须与其主通道(例如,x
,y
)完全相同。
另请参阅: type 文档。
- shorthandstr, dict, Sequence[str],
- __init__(shorthand=Undefined, aggregate=Undefined, bandPosition=Undefined, bin=Undefined, field=Undefined, timeUnit=Undefined, title=Undefined, type=Undefined, **kwds)#
方法
__init__
([shorthand, aggregate, ...])copy
([deep, ignore])返回对象的副本。
from_dict
(dct[, validate])从字典表示构造类。
from_json
(json_string[, validate])从有效的 JSON 字符串实例化对象。
resolve_references
([schema])在此对象的模式或根模式的上下文中解析引用。
to_dict
([validate, ignore, context])返回对象的字典表示。
to_json
([validate, indent, sort_keys, ...])将此对象的 JSON 表示形式作为字符串输出。
validate
(instance[, schema])在根模式的上下文中,根据类模式验证实例。
validate_property
(name, value[, schema])在根模式的上下文中,根据属性模式验证属性。
属性
aggregate
Aggregate 模式封装器。
bandPosition
在堆叠、分箱、时间单位或带状比例尺上的相对位置。
bin
BinParams 模式封装器。
field
Field 模式封装器。
timeUnit
TimeUnit 模式封装器。
title
TitleParams 模式封装器。
type
Type 模式封装器。