将文本和数据图表与 facet 结合?
Combine text and data charts with facet?
我正在处理这个例子中的代码,稍作修改:
https://altair-viz.github.io/gallery/layered_heatmap_text.html
我想弄清楚为什么当我尝试分面到列时我的代码会中断。
# Import data
import altair as alt
from vega_datasets import data
source = data.cars()
# Configure common options
base = alt.Chart(source)
scale = alt.Scale(paddingInner=0)
原始版本运行良好:
# Configure heatmap
heatmap = base.mark_rect().encode(
alt.X('Cylinders:O', scale=scale),
alt.Y('Year:O', scale=scale),
color='count()'
)
# Configure text
text = base.mark_text(baseline='middle').encode(
x='Cylinders:O',
y='Year:O',
text='count()',
color=alt.value('white')
)
# Draw the chart
heatmap+text
现在我想 Origin
分面。当我分别显示 heatmap
和 text
时,下面的代码有效,但是当我组合它们时,出现错误。
# Configure heatmap
heatmap = base.mark_rect().encode(
alt.X('Cylinders:O', scale=scale),
alt.Y('Year:O', scale=scale),
color='count()',
column = 'Origin'
)
# Configure text
text = base.mark_text(baseline='middle').encode(
x='Cylinders:O',
y='Year:O',
text='count()',
color=alt.value('white'),
column = 'Origin'
)
# Draw the chart
heatmap+text
这是错误消息 - 我不太明白问题出在哪里。
---------------------------------------------------------------------------
SchemaValidationError Traceback (most recent call last)
~/miniconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in _repr_mimebundle_(self, include, exclude)
1111 try:
-> 1112 dct = self.to_dict()
1113 except Exception:
~/miniconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in to_dict(self, *args, **kwargs)
420 kwargs['validate'] = 'deep'
--> 421 dct = super(TopLevelMixin, copy).to_dict(*args, **kwargs)
422
~/miniconda3/lib/python3.6/site-packages/altair/utils/schemapi.py in to_dict(self, validate, ignore, context)
253 except jsonschema.ValidationError as err:
--> 254 raise SchemaValidationError(self, err)
255 return result
<class 'str'>: (<class 'TypeError'>, TypeError('sequence item 1: expected str instance, int found',))
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
~/miniconda3/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj, include, exclude)
968
969 if method is not None:
--> 970 return method(include=include, exclude=exclude)
971 return None
972 else:
~/miniconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in _repr_mimebundle_(self, include, exclude)
1112 dct = self.to_dict()
1113 except Exception:
-> 1114 utils.display_traceback(in_ipython=True)
1115 return {}
1116 else:
~/miniconda3/lib/python3.6/site-packages/altair/utils/core.py in display_traceback(in_ipython)
403
404 if ip is not None:
--> 405 ip.showtraceback(exc_info)
406 else:
407 traceback.print_exception(*exc_info)
~/miniconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py in showtraceback(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)
2036 value, tb, tb_offset=tb_offset)
2037
-> 2038 self._showtraceback(etype, value, stb)
2039 if self.call_pdb:
2040 # drop into debugger
~/miniconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py in _showtraceback(self, etype, evalue, stb)
544 u'traceback' : stb,
545 u'ename' : unicode_type(etype.__name__),
--> 546 u'evalue' : py3compat.safe_unicode(evalue),
547 }
548
~/miniconda3/lib/python3.6/site-packages/ipython_genutils/py3compat.py in safe_unicode(e)
63 """
64 try:
---> 65 return unicode_type(e)
66 except UnicodeError:
67 pass
~/miniconda3/lib/python3.6/site-packages/altair/utils/schemapi.py in __unicode__(self)
67 schema_path = ['{}.{}'.format(cls.__module__, cls.__name__)]
68 schema_path.extend(self.schema_path)
---> 69 schema_path = '->'.join(val for val in schema_path[:-1]
70 if val not in ('properties',
71 'additionalProperties',
TypeError: sequence item 1: expected str instance, int found
问题是分面图无法分层(这是因为一般来说,无法保证两层具有兼容的分面)。另一方面,分层图表可以是分面的。例如:
# Import data
import altair as alt
from vega_datasets import data
source = data.cars()
# Configure common options
scale = alt.Scale(paddingInner=0)
# Configure heatmap
heatmap = alt.Chart().mark_rect().encode(
alt.X('Cylinders:O', scale=scale),
alt.Y('Year:O', scale=scale),
color='count()'
)
# Configure text
text = alt.Chart().mark_text(baseline='middle').encode(
x='Cylinders:O',
y='Year:O',
text='count()',
color=alt.value('white')
)
# Draw the chart.
alt.layer(heatmap, text, data=source).facet(
column='Origin'
)
请注意,使用此方法时,重要的是在构面级别而不是子层中指定数据。有关详细信息,请参阅 Altair 文档中的 Faceted Charts。
此处的错误消息过去更有帮助...似乎 jsonschema
库报告模式验证错误的方式与以往不同。
我正在处理这个例子中的代码,稍作修改:
https://altair-viz.github.io/gallery/layered_heatmap_text.html
我想弄清楚为什么当我尝试分面到列时我的代码会中断。
# Import data
import altair as alt
from vega_datasets import data
source = data.cars()
# Configure common options
base = alt.Chart(source)
scale = alt.Scale(paddingInner=0)
原始版本运行良好:
# Configure heatmap
heatmap = base.mark_rect().encode(
alt.X('Cylinders:O', scale=scale),
alt.Y('Year:O', scale=scale),
color='count()'
)
# Configure text
text = base.mark_text(baseline='middle').encode(
x='Cylinders:O',
y='Year:O',
text='count()',
color=alt.value('white')
)
# Draw the chart
heatmap+text
现在我想 Origin
分面。当我分别显示 heatmap
和 text
时,下面的代码有效,但是当我组合它们时,出现错误。
# Configure heatmap
heatmap = base.mark_rect().encode(
alt.X('Cylinders:O', scale=scale),
alt.Y('Year:O', scale=scale),
color='count()',
column = 'Origin'
)
# Configure text
text = base.mark_text(baseline='middle').encode(
x='Cylinders:O',
y='Year:O',
text='count()',
color=alt.value('white'),
column = 'Origin'
)
# Draw the chart
heatmap+text
这是错误消息 - 我不太明白问题出在哪里。
---------------------------------------------------------------------------
SchemaValidationError Traceback (most recent call last)
~/miniconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in _repr_mimebundle_(self, include, exclude)
1111 try:
-> 1112 dct = self.to_dict()
1113 except Exception:
~/miniconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in to_dict(self, *args, **kwargs)
420 kwargs['validate'] = 'deep'
--> 421 dct = super(TopLevelMixin, copy).to_dict(*args, **kwargs)
422
~/miniconda3/lib/python3.6/site-packages/altair/utils/schemapi.py in to_dict(self, validate, ignore, context)
253 except jsonschema.ValidationError as err:
--> 254 raise SchemaValidationError(self, err)
255 return result
<class 'str'>: (<class 'TypeError'>, TypeError('sequence item 1: expected str instance, int found',))
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
~/miniconda3/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj, include, exclude)
968
969 if method is not None:
--> 970 return method(include=include, exclude=exclude)
971 return None
972 else:
~/miniconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in _repr_mimebundle_(self, include, exclude)
1112 dct = self.to_dict()
1113 except Exception:
-> 1114 utils.display_traceback(in_ipython=True)
1115 return {}
1116 else:
~/miniconda3/lib/python3.6/site-packages/altair/utils/core.py in display_traceback(in_ipython)
403
404 if ip is not None:
--> 405 ip.showtraceback(exc_info)
406 else:
407 traceback.print_exception(*exc_info)
~/miniconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py in showtraceback(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)
2036 value, tb, tb_offset=tb_offset)
2037
-> 2038 self._showtraceback(etype, value, stb)
2039 if self.call_pdb:
2040 # drop into debugger
~/miniconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py in _showtraceback(self, etype, evalue, stb)
544 u'traceback' : stb,
545 u'ename' : unicode_type(etype.__name__),
--> 546 u'evalue' : py3compat.safe_unicode(evalue),
547 }
548
~/miniconda3/lib/python3.6/site-packages/ipython_genutils/py3compat.py in safe_unicode(e)
63 """
64 try:
---> 65 return unicode_type(e)
66 except UnicodeError:
67 pass
~/miniconda3/lib/python3.6/site-packages/altair/utils/schemapi.py in __unicode__(self)
67 schema_path = ['{}.{}'.format(cls.__module__, cls.__name__)]
68 schema_path.extend(self.schema_path)
---> 69 schema_path = '->'.join(val for val in schema_path[:-1]
70 if val not in ('properties',
71 'additionalProperties',
TypeError: sequence item 1: expected str instance, int found
问题是分面图无法分层(这是因为一般来说,无法保证两层具有兼容的分面)。另一方面,分层图表可以是分面的。例如:
# Import data
import altair as alt
from vega_datasets import data
source = data.cars()
# Configure common options
scale = alt.Scale(paddingInner=0)
# Configure heatmap
heatmap = alt.Chart().mark_rect().encode(
alt.X('Cylinders:O', scale=scale),
alt.Y('Year:O', scale=scale),
color='count()'
)
# Configure text
text = alt.Chart().mark_text(baseline='middle').encode(
x='Cylinders:O',
y='Year:O',
text='count()',
color=alt.value('white')
)
# Draw the chart.
alt.layer(heatmap, text, data=source).facet(
column='Origin'
)
请注意,使用此方法时,重要的是在构面级别而不是子层中指定数据。有关详细信息,请参阅 Altair 文档中的 Faceted Charts。
此处的错误消息过去更有帮助...似乎 jsonschema
库报告模式验证错误的方式与以往不同。