时间序列索引不均匀的python pandas图(计数均匀分布) [英] python pandas plot with uneven timeseries index (with count evenly distributed)
问题描述
我的数据帧的时间索引不均匀.
My dataframe has uneven time index.
我如何找到一种方法来绘制数据并自动对索引进行本地化?我在这里搜索,我知道我可以绘制类似
how could I find a way to plot the data, and local the index automatically? I searched here, and I know I can plot something like
e.plot()
,但是时间索引(x轴)将是偶数间隔,例如每5分钟.如果前 5 分钟需要 100 个数据,后 5 分钟需要 6 个数据,我该如何绘制与均匀的数据数量.并在x轴上找到正确的时间戳.
but the time index (x axis) will be even interval, for example per 5 minutes. if I have to 100 data in first 5 minutes and 6 data for the second 5 minutes, how do I plot with number of data evenly. and locate the right timestamp on x axis.
这里甚至算在内,但我不知道如何添加时间索引.
here's even count, but I don't know how to add time index.
plot(e['Bid'].values)
请求的数据格式示例
时间,出价
2014-03-05 21:56:05:924300,1.37275
2014-03-05 21:56:05:924300,1.37275
2014-03-05 21:56:05:924351,1.37272
2014-03-05 21:56:05:924351,1.37272
2014-03-05 21:56:06:421906,1.37275
2014-03-05 21:56:06:421906,1.37275
2014-03-05 21:56:06:421950,1.37272
2014-03-05 21:56:06:421950,1.37272
2014-03-05 21:56:06:920539,1.37275
2014-03-05 21:56:06:920539,1.37275
2014-03-05 21:56:06:920580,1.37272
2014-03-05 21:56:06:920580,1.37272
2014-03-05 21:56:09:071981,1.37275
2014-03-05 21:56:09:071981,1.37275
2014-03-05 21:56:09:072019,1.37272
2014-03-05 21:56:09:072019,1.37272
这是链接http://code.google.com/p/eu-ats/source/browse/trunk/data/new/eur-fix.csv
这是我以前绘制的代码
import numpy as np
import pandas as pd
import datetime as dt
e = pd.read_csv("data/ecb/eur.csv", dtype={'Time':object})
e.Time = pd.to_datetime(e.Time, format='%Y-%m-%d %H:%M:%S:%f')
e.plot()
f = e.copy()
f.index = f.Time
x = [str(s)[:-7] for s in f.index]
ff = f.set_index(pd.Series(x))
ff.index.name = 'Time'
ff.plot()
更新:
我添加了两个新图进行比较以澄清问题.现在,我尝试用蛮力将时间戳记索引转换回字符串,并将字符串绘制为x轴.格式很容易搞砸.似乎很难自定义x标签的位置.
I added two new plots for comparison to clarify the issue. Now I tried brute force to convert timestamp index back to string, and plot string as x axis. the format easily got messed up. it seems hard to customize location of x label.
推荐答案
好吧,看来您想要的是要在x-tick位置附近移动,以便每个位置之间有相等数量的点打钩.您希望在这些适当位置的刻度上绘制网格.我有这个权利吗?
Ok, it seems like what you're after is that you want to move around the x-tick locations so that there are an equal number of points between each tick. And you'd like to have the grid drawn on these appropriately-located ticks. Do I have that right?
如果是这样:
import pandas as pd
import urllib
import matplotlib.pyplot as plt
import seaborn as sbn
content = urllib.urlopen('https://eu-ats.googlecode.com/svn/trunk/data/new/eur-fix.csv')
df = pd.read_csv(content, header=0)
df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
every30 = df.loc[df.index % 30 == 0, 'Time'].values
fig, ax = plt.subplots(1, 1, figsize=(9, 5))
df.plot(x='Time', y='Bid', ax=ax)
ax.set_xticks(every30)
这篇关于时间序列索引不均匀的python pandas图(计数均匀分布)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!