将垂直线覆盖在Altair的地块上

时间:2016-11-02 13:31:38

标签: python pandas plot visualization

我有一个数据框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值为df.violation的垂直条:

example chart

我尝试在this notebook之后将两个Chart()对象叠加在一起,但它似乎不起作用。我希望能够做到这样的事情:

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

我已经检查过单独的图表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

我知道Altair仍然处于起步阶段,因此缺乏文档,但有人知道如何轻松地做到这一点吗?这是ggplot2中无关紧要的任务之一。

1 个答案:

答案 0 :(得分:0)

尝试

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

看看this link,它会进一步解释