0

I read csv file with panda. On source file i have column with date and time and some outher columns ( financial OHLC ). When i was reading csv file i was using some parse option ( combine columns on parse like this http://pandas.pydata.org/pandas-docs/stable/io.html?highlight=read%20csv#specifying-date-columns ). On the end i try plot simple graphics from new combined datetime column and some other date. All works, but ploting function creates line only from time date on datetime column. Why ploting function don't understand date component on my datetime column? How can i create graphics from all datetime points?

import pandas as pd
import matplotlib.pyplot as plot
sample_date = pd.read_csv('sample-2dayslineEURUSDM1.csv',parse_dates=[[1, 2]],keep_date_col=True)
date_time=sample_date.icol(0)
date_close=sample_date.icol(6)
plot.plot_date(x=date_time,y=date_close,fmt="r-")
plot.xlabel("Close")
plot.ylabel("Date")
plot.show()

Sample from my csv file:

               Time_Open        Data   Time     Open   Hight     Low   Close
0    2015-06-01 01:35:00  2005.02.15  01:35  1.29710  1.2972  1.2969  1.2969   
1    2015-06-01 01:36:00  2005.02.15  01:36  1.29700  1.2971  1.2969  1.2970   
2    2015-06-01 01:37:00  2005.02.15  01:37  1.29700  1.2972  1.2969  1.2970  

My plot with error

enter image description here

I think with my data was all right. I can operate with them and python understand data type. I can do like this:

date_time[0]<date_time[1]
Out[11]: True
date_time[0]-date_time[1]
Out[13]: Timedelta('-1 days +23:59:00')

But matplotlib don't understand my data. I want get graphics that on y axis i see datetime (like this 2015-06-01 23:59:00) and not only time (23:59:00). Now matplotlib plot graphs in 24 hours and don't understand that my data have different days. I get one line in one day, but not 1 line for all my data.

5
  • Sample my date - link to csv file dropbox.com/s/v2oexkmvzuzgc0e/sample-2dayslineEURUSDM1.csv?dl=0 Commented Jun 30, 2015 at 7:59
  • Sorry are you asking why you get 2 lines instead of a single line? so something like sample_date[[0,6]].plot()? Commented Jun 30, 2015 at 8:29
  • @EdChum, Yes! This is it. I don't understand why date_plot get me 2 or more line. Now i see what i want, but on yaxix i get only point number 1...5000, but not date. How can i get yaxix like plot_date() fynction? Commented Jun 30, 2015 at 8:34
  • I'm not sure why the x_axis is formatted like that in pandas, it's possible your 2 line issue is because matplotlib doesn't understand datetime64 dtype correctly, see this: stackoverflow.com/questions/26526230/… Commented Jun 30, 2015 at 8:45
  • @EdChum, thank you. Yes my data have datetime64 type:( I will not close my question, maybe someone has a fresh solution. Thank you for your help and link on this problem. Commented Jun 30, 2015 at 8:55

2 Answers 2

1

I don't have all your data but essentially you need to use DateFormatter to format your x-ticks:

import matplotlib.pyplot as plt
import matplotlib.dates as dates
fig, ax = plt.subplots()
date_time=df.icol(0)
date_close=df.icol(6)
plt.plot_date(x=date_time,y=date_close,fmt="r-")
plt.xlabel("Close")
plt.ylabel("Date")
ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%m-%d'))
plt.show()

enter image description here

My picture isn't great but you should be able to get the general gist here

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I don't understand what you mean, please edit your question with your code, output, errors and desired output
0

I can't resolved problem with datetime64, but i find other way. You should watch Chang She's lection https://vimeo.com/53065093 . This lection for python 2.x, but in this case it' not matter.

Some working code from this lections for python 3.

from datetime import datetime, date, time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
with open('sample-2dayslineEURUSDM1.csv', 'r') as fh:
    print(fh.readline())
    print(fh.readline())
    data = pd.read_csv('sample-2dayslineEURUSDM1.csv',
               parse_dates={'Timestamp':['Date','Time']}, 
               index_col='Timestamp')
data.index
ticks = data.ix[:,['Close']]
ticks.head()
ticks.ix['2005-02-15 01:35:00':'2005-02-16 23:59:00'].plot()

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.