使用ggplot将日期或时间绘制为x轴

时间:2014-06-22 18:53:27

标签: python plot pandas python-ggplot

我的数据形式如下:

datetime, count
2011-01-01 00:00:00, 10
2011-01-01 01:00:00, 15
2011-01-01 02:00:00, 20
...

使用ggplot,我想绘制以下2个图表:

  1. 计数随时间的变化
  2. 天数变化
  3. 我能够通过将字符串转换为pandas datetime

    来绘制第一个
    csv_file['datetime'] = pd.to_datetime(csv_file['datetime'])
    

    然后使用ggplot绘制它。

    对于第二个,我将日期时间转换为时间

    csv_file['time'] = csv_file['datetime'].map(methodcaller('time'))
    
    type(csv_file['time'][4]) = <type 'datetime.time'>
    

    但是在绘图时,我收到了这个错误:

    File "/Library/Python/2.7/site-packages/numpy-1.9.0.dev_297f54b-py2.7-macosx-10.9-intel.egg/numpy/core/numeric.py", line 460, in asarray
        return array(a, dtype, copy=False, order=order)
    TypeError: float() argument must be a string or a number
    

    绘图代码:

    ggplot1 =  ggplot(aes(x='time', y='count'), data=csv_file) + \
            geom_point(color='lightblue') + \
            stat_smooth(span=.15, color='black', se=True) + \
            ggtitle("Test") + \
            xlab("time") + \
            ylab("count")
    
        ggplot1.draw() 
    

    目:

         datetime                time        count
    0 2011-01-01 00:00:00      00:00:00       39
    1 2011-01-01 01:00:00      01:00:00       40 
    2 2011-01-01 02:00:00      02:00:00       10  
    3 2011-01-01 03:00:00      03:00:00       14
    4 2011-01-01 04:00:00      04:00:00       18
    

    完成堆栈跟踪:

    File "hello.py", line 43, in <module>
        run()
      File "hello.py", line 41, in run
        ggplot1.draw()    
      File "/Library/Python/2.7/site-packages/ggplot/ggplot.py", line 305, in draw
        callbacks = geom.plot_layer(data, ax)
      File "/Library/Python/2.7/site-packages/ggplot/geoms/geom.py", line 115, in plot_layer
        data = self._calculate_stats(data)
      File "/Library/Python/2.7/site-packages/ggplot/geoms/geom.py", line 275, in _calculate_stats
        new_data = self._stat._calculate(data)
      File "/Library/Python/2.7/site-packages/ggplot/stats/stat_smooth.py", line 43, in _calculate
        x, y, y1, y2 = smoothers.lowess(x, y, span=span)
      File "/Library/Python/2.7/site-packages/ggplot/components/smoothers.py", line 53, in lowess
        result = smlowess(np.array(y), np.array(x), frac=span)
      File "/Library/Python/2.7/site-packages/statsmodels-0.6.0-py2.7-macosx-10.9-intel.egg/statsmodels/nonparametric/smoothers_lowess.py", line 128, in lowess
        exog = np.asarray(exog, float)
      File "/Library/Python/2.7/site-packages/numpy-1.9.0.dev_297f54b-py2.7-macosx-10.9-intel.egg/numpy/core/numeric.py", line 460, in asarray
        return array(a, dtype, copy=False, order=order)
    TypeError: float() argument must be a string or a number
    

0 个答案:

没有答案