Отображение даты или времени по оси x с помощью ggplot

#python #plot #pandas #python-ggplot

#python #график #панды #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. Изменение количества за день

Я смог построить первый, преобразовав строку в 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
  

Комментарии:

1. Это похоже на ошибку в ggplot. Можете ли вы опубликовать полный код построения и заголовок вашего файла данных ( csv_file.head() ). Пожалуйста, также запустите <ggplotobject>.draw() явно ( p = ggplot(...) geom_something(...) ...; p.draw() ), чтобы получить полную трассировку стека, если вы используете ipython. Спасибо!

2. @JanSchulz Я обновил вопрос с помощью графика и заголовка csv

3. Можете ли вы также показать полную трассировку стека, а не только последнюю строку?

4. @JanSchulz Я обновил вопрос с помощью полной трассировки стека

5. Спасибо! Я добавил ошибку: github.com/yhat/ggplot/issues/340 . в настоящее время я не уверен, как обойти…