Python:绘制日期时间条目的数据框 [英] Python: plot dataframe of datetime entries

查看:132
本文介绍了Python:绘制日期时间条目的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下:

  900324492 900405679 900472531 
1 2017-04-03 08:04: 09 2017-04-03 07:49:53 2017-04-03 07:52:39
2 2017-04-03 08:05:36 2017-04-03 07:54:36 2017-04- 03 07:52:19
3 2017-04-03 08:05:28 2017-04-03 07:43:00 2017-04-03 07:50:52
4 2017-04- 03 08:06:05 2017-04-03 07:49:42 2017-04-03 07:53:55

因此,对于每一列,我都有一组时间戳(准确地说是datetime对象)。我喜欢做一个散点图,其中x是df索引或行号(即 x = [1,2,3,4,...]),并且y是一个时间点。例如,如果df中有4行和10列,则x轴应为 1、2、3、4
(对于 x = 1 第一行中的每个条目应有一个点。



这似乎很简单,但是我有点挣扎。到目前为止我的代码:

  df = pd.read_csv('test.csv')
df2 = df.apply (lambda x:pd.to_datetime(x))

fig = plt.figure()
ax = fig.add_subplot(111)
y = df2.ix [:, 1 ]
x = df2.index.values
#返回废话
ax.plo t(x,y)
#TypeError:类型提升无效
ax.scatter(x = x,y = df2.ix [:,1])$ ​​b $ b#TypeError:空'DataFrame' :没有数字数据可以绘制
df2.ix [:,1] .plot()

测试文件链接:


My data looks like this :

    900324492   900405679   900472531
1   2017-04-03 08:04:09 2017-04-03 07:49:53 2017-04-03 07:52:39
2   2017-04-03 08:05:36 2017-04-03 07:54:36 2017-04-03 07:52:19
3   2017-04-03 08:05:28 2017-04-03 07:43:00 2017-04-03 07:50:52
4   2017-04-03 08:06:05 2017-04-03 07:49:42 2017-04-03 07:53:55

So, for each column, I have a set of time stamps (datetime objects, to be exact). I like to make a scatter plot, where x is the df index or row number (i.e. x=[1,2,3,4,...]), and y is a time point. For example, If there are 4 rows and 10 columns in df, x axis should be 1, 2, 3, 4, and for x=1 there should be one point per entry in the first row.

It seemed like a simple task, but I'm struggling a bit. My code so far:

df = pd.read_csv('test.csv')
df2 = df.apply(lambda x : pd.to_datetime(x))

fig = plt.figure()                                                                                                                                                                                                                                                             
ax = fig.add_subplot(111)                                                                                                                                                                                                                                                      
y = df2.ix[:, 1]                                                                                                                                                                                                                                           
x = df2.index.values
# returns nonsense
ax.plot(x,y)
# TypeError: invalid type promotion
ax.scatter(x=x, y = df2.ix[:,1])
# TypeError: Empty 'DataFrame': no numeric data to plot
df2.ix[:,1].plot()

Test file link : test.csv

解决方案

Please check my example from yours. You should focus on to_pydatetime() and date2num() and np.nan. (You have to tag y axis to datetime format finally.)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates


df = pd.read_csv('test.csv', header=None)
df2 = df.apply(lambda x : pd.to_datetime(x))

fig = plt.figure()                                                                                                                                                                                                                                                             
ax = fig.add_subplot(111)                                                                                                                                                                                                                                                      
y = df2.ix[:, 1]                                                                                                                                                                                                                                         
x = df2.index.values

def fix(x):
    try:
        return dates.date2num(x.to_pydatetime())
    except:
        return np.nan

y_lab = [str(e) for e in y]
y_ = [fix(e) for e in y]

ax.scatter(x=x, y=y_)

plt.yticks(y_, y_lab, rotation=0, size=6)
plt.show()

这篇关于Python:绘制日期时间条目的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆