python pandas plot with uneven timeseries index (with count evenly distributed)

2024/10/1 19:19:38

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()

even time

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)

even count

example of data format as requested

Time,Bid

2014-03-05 21:56:05:924300,1.37275

2014-03-05 21:56:05:924351,1.37272

2014-03-05 21:56:06:421906,1.37275

2014-03-05 21:56:06:421950,1.37272

2014-03-05 21:56:06:920539,1.37275

2014-03-05 21:56:06:920580,1.37272

2014-03-05 21:56:09:071981,1.37275

2014-03-05 21:56:09:072019,1.37272

and here's the link http://code.google.com/p/eu-ats/source/browse/trunk/data/new/eur-fix.csv

here's the code, I used to plot

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()

Update:

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.

By ticks or data points

By time

Answer

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?

If so:

import pandas as pd
import urllib
import matplotlib.pyplot as plt
import seaborn as sbncontent = 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)

enter image description here

https://en.xdnf.cn/q/70936.html

Related Q&A

python OpenAI gym monitor creates json files in the recording directory

I am implementing value iteration on the gym CartPole-v0 environment and would like to record the video of the agents actions in a video file. I have been trying to implement this using the Monitor wra…

Install xgboost under python with 32-bit msys failing

Trying to install xgboost is failing..? The version is Anaconda 2.1.0 (64-bit) on Windows & enterprise. How do I proceed? I have been using R it seems its quite easy to install new package in R …

Pythons _winapi module

I was trying to write some python code that requires calls to native WINAPI functions. At first I came across the pypiwin32 package. Then, somewhere on the internet I saw someone using the _winapi modu…

list comprehension with numpy arrays - bad practice?

I am wondering if the below approach would be considered bad practice, and if so, if someone could give some guidance towards another approach. Here is the code in question:a = np.array([[1,2,3],[4,5,6…

pandas: write dataframe to excel file *object* (not file)?

I have a dataframe that I want to convert to excel file, and return it using HTTP. Dataframes to_excel method accepts either a path, or an ExcelWriter, which, in turn, refers to a path.Is there any way…

win32: moving mouse with SetCursorPos vs. mouse_event

Is there any difference between moving the mouse in windows using the following two techniques?win32api.SetCursorPos((x,y))vs:nx = x*65535/win32api.GetSystemMetrics(0) ny = y*65535/win32api.GetSystemM…

Pandas: Unstacking One Column of a DataFrame

I want to unstack one column in my Pandas DataFrame. The DataFrame is indexed by the Date and I want to unstack the Country column so each Country is its own column. The current pandas DF looks like t…

python-polars split string column into many columns by delimiter

In pandas, the following code will split the string from col1 into many columns. is there a way to do this in polars? d = {col1: ["a/b/c/d", "a/b/c/d"]} df= pd.DataFrame(data=d) df…

pylint giving not-callable error for object property that is callable

Not sure if I am doing something wrong or if this is a problem with pylint. In the code below I get a linting error that self.type is not callable E1102.Although I could just ignore it and keep workin…

ModuleNotFoundError: No module named api

I created a Django project inside of api folder called bucks:api |____ categories/|____ __init__.py|____ ...|____ models.py|____ tests.py|____ views.py |____ .../ |____ bucks/ |____ users/|____ __init_…