在 Altair 中将垂直线叠加到绘图上

gog*_*urt 6 python plot visualization pandas altair

我有一个如下所示的数据框df

+---+------------+-----------+--------+
|   |    date    | violation | pounds |
+---+------------+-----------+--------+
| 0 | 2010-05-13 | N         | NaN    |
| 1 | 2015-04-22 | Y         | NaN    |
| 2 | 2009-08-12 | Y         | NaN    |
| 3 | 2006-06-01 | NaN       | 3732.0 |
| 4 | 2006-08-01 | NaN       | 1340.0 |
| 5 | 2006-10-01 | NaN       | 1310.0 |
+---+------------+-----------+--------+
Run Code Online (Sandbox Code Playgroud)

我想pounds用时间序列给出的水平坐标在垂直轴上绘制变量date,并将垂直线覆盖在violation不是 Nan 的地方。基本上,我想要下面的图表,除了在非 NaN 值为 的垂直条df.violation

示例图表

我尝试Chart()此笔记本之后将两个对象相互叠加,但似乎不起作用。我希望能够做这样的事情:

points = Chart(df).mark_point().encode(y='pounds', x='date')
rules = Chart(df[df['violation']=='Y']).mark_rule().encode(x='date')
points + rules
Run Code Online (Sandbox Code Playgroud)

我检查了单独的图表pointsrules两者看起来都很好。然而,该points + rules命令导致以下错误:

ValueError                                Traceback (most recent call last)
~/anaconda3/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
    907             method = _safe_get_formatter_method(obj, self.print_method)
    908             if method is not None:
--> 909                 method()
    910                 return True
    911 

~/anaconda3/lib/python3.5/site-packages/altair/api.py in _ipython_display_(self)
    186         from IPython.display import display
    187         from vega import VegaLite
--> 188         display(VegaLite(self.to_dict()))
    189 
    190     def display(self):

~/anaconda3/lib/python3.5/site-packages/vega/base.py in __init__(self, spec, data)
     21         """Initialize the visualization object."""
     22         spec = utils.nested_update(copy.deepcopy(self.DEFAULTS), spec)
---> 23         self.spec = self._prepare_spec(spec, data)
     24 
     25     def _prepare_spec(self, spec, data):

~/anaconda3/lib/python3.5/site-packages/vega/vegalite.py in _prepare_spec(self, spec, data)
     22 
     23     def _prepare_spec(self, spec, data):
---> 24         return prepare_spec(spec, data)
     25 
     26 

~/anaconda3/lib/python3.5/site-packages/vega/utils.py in prepare_spec(spec, data)
     91         # Data is either passed in spec or error
     92         if 'data' not in spec:
---> 93             raise ValueError('No data provided')
     94     else:
     95         # As a last resort try to pass the data to a DataFrame and use it

ValueError: No data provided
Run Code Online (Sandbox Code Playgroud)

我知道 Altair 仍处于起步阶段,因此缺乏文档,但有人知道如何轻松做到这一点吗?这是在ggplot2.

小智 4

尝试

points = Chart(df).mark_point().encode(y='pounds', x='date')
rules = Chart(df).mark_rule().encode(x='date').transform_filter(datum.violation == 'Y')
points + rules
Run Code Online (Sandbox Code Playgroud)

看一下这个链接,它进一步解释了它