0

I have a pandas dataframe that I want to use in a vincent visualization. I can visualize the data, however, the X axis should be displayed as dates and instead the dates are just given an integer index of 500, 1000, 1500, etc.

The dataframe looks like this:

    weight      date
0   125.200000  2013-11-18

Truncated for brevity.

My vincent code in my ipython notebook:

chart = vincent.Line(df[['weight']])
chart.legend(title='weight')
chart.axis_titles(x='Date', y='Weight')
chart.display()

How can I tell vincent that my dataframe contains dates such that the X axis labels are just like the dataframe's dates above, i.e. 2013-11-18?

4

1 回答 1

0

好的,这就是我所做的。我之前在使用 matplotlib 时遇到过这个问题,写了一篇关于它的博客文章(http://codrspace.com/szeitlin/biking-data-from-xml-to-plots-part-2/)非常痛苦。文森特并不完全相同,但基本上你必须做 4 个步骤:

  1. 将您的日期转换为日期时间对象,如果您还没有

df['date_objs'] = df['date'].apply(pandas.to_datetime)

  1. 将您的日期时间对象转换为您想要的任何格式。
  2. 将您的日期时间对象放入您的索引中

    df.index = df.index.values.astype('M8[D]')

  3. 告诉 vincent 你想将你的数据(权重)绘制为 y 轴。它将自动使用数据框的索引作为 x 轴。

    chart = vincent.Line(plot[['weight']]) 
    chart.axis_titles(x='dates', y='weight') 
    chart.display()
    
于 2015-03-26T22:26:05.293 回答