首页 > 解决方案 > 将文本和数据图表与构面相结合?

问题描述

我正在处理此示例中的代码,稍作修改:

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. 下面的代码在我单独显示时有效heatmaptext但是当我组合它们时,我得到一个错误。

# 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

标签: pythonaltair

解决方案


问题是分面图表不能分层(这是因为通常不能保证这两个层将具有兼容的分面)。另一方面,分层图表可以是多面的。例如:

# 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 文档中的分面图

这里的错误消息曾经更有帮助......似乎该jsonschema库报告的模式验证错误与以前不同。


推荐阅读