我有一个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 |
+---+------------+-----------+--------+
我想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
我检查了单独的图表points
,rules
两者看起来都很好。然而该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
我知道 Altair 仍处于起步阶段,因此缺乏文档,但有人知道如何轻松做到这一点吗?这是 . 中微不足道的任务之一ggplot2
。