Python 使用ggplot以x轴打印日期或时间

Python 使用ggplot以x轴打印日期或时间,python,plot,pandas,python-ggplot,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,我想绘制以下两个图形: 计数随时间的变化 日积月累 我可以通过将字符串转换为日期时间来绘制第一个 csv_file['datetime'] = pd.to_datetime(csv_file['datetime']) 然后使用ggplot绘制它 对于第二个,我将dateti

我的数据格式如下:

datetime, count
2011-01-01 00:00:00, 10
2011-01-01 01:00:00, 15
2011-01-01 02:00:00, 20
...
使用ggplot,我想绘制以下两个图形:

  • 计数随时间的变化
  • 日积月累
  • 我可以通过将字符串转换为日期时间来绘制第一个

    csv_file['datetime'] = pd.to_datetime(csv_file['datetime'])
    
    然后使用ggplot绘制它

    对于第二个,我将datetime转换为time

    csv_file['time'] = csv_file['datetime'].map(methodcaller('time'))
    
    type(csv_file['time'][4]) = <type 'datetime.time'>
    
    绘图代码:

    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() 
    
    负责人:

    完整堆栈跟踪:

    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
    
    文件“hello.py”,第43行,在
    运行()
    文件“hello.py”,第41行,在运行中
    ggplot1.draw()
    文件“/Library/Python/2.7/site packages/ggplot/ggplot.py”,第305行,在绘图中
    回调=几何绘图层(数据,ax)
    文件“/Library/Python/2.7/site packages/ggplot/geoms/geom.py”,第115行,在plot_层中
    数据=自身。计算统计数据(数据)
    文件“/Library/Python/2.7/site packages/ggplot/geoms/geom.py”,第275行,在统计中
    新数据=自我统计计算(数据)
    文件“/Library/Python/2.7/site packages/ggplot/stats/stat_smooth.py”,第43行,in_计算
    x、 y,y1,y2=更平滑的。下(x,y,span=span)
    文件“/Library/Python/2.7/site packages/ggplot/components/smoothers.py”,第53行,小写
    结果=smlowess(np.数组(y)、np.数组(x)、分形=跨度)
    文件“/Library/Python/2.7/site packages/statsmodels-0.6.0-py2.7-macosx-10.9-intel.egg/statsmodels/nonparametric/smoothers_lowss.py”,第128行,小写
    exog=np.asarray(exog,浮动)
    asarray中的文件“/Library/Python/2.7/site packages/numpy-1.9.0.dev_297f54b-py2.7-macosx-10.9-intel.egg/numpy/core/numeric.py”,第460行
    返回数组(a,数据类型,copy=False,order=order)
    TypeError:float()参数必须是字符串或数字
    
    这看起来像ggplot中的一个bug。您可以发布完整的绘图代码和数据文件的头部(
    csv\u file.head()
    )。如果使用ipython,请显式运行
    .draw()
    p=ggplot(…)+geom_something(…)+…;p.draw()
    )以获取完整的堆栈跟踪。谢谢@Janshulz我用plot和csv head更新了问题,你能不能也显示完整的堆栈跟踪,而不仅仅是最后一行?@Janshulz我用完整的堆栈跟踪更新了问题!我添加了一个bug:。目前我不知道如何解决。。。
         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