Reputation: 546
I cannot figure this out at all, how do I read a date from csv but I CANNOT represent the date as a label on the x axis. I have tried all the approaches that people have suggested but I cannot get it to work. SO could someone look at the stripped down version of my code and tell me what I am missing please?
a sample of the data being read from csv file
2015-08-04 02:14:05.249392,AA,0.0193103612,0.0193515212,0.0249713335,30.6542480634,30.7195875454,39.640763021,0.2131498442,29.0406746589,13524.5347810182,89,57,99
2015-08-05 02:14:05.325113,AAPL,0.0170506271,0.0137941891,0.0105915637,27.0670313481,21.8975963326,16.8135861893,-19.0986405157,-23.2172064279,21.5647072302,33,26,75
2015-08-06 02:14:05.415193,AIG,0.0080808151,0.0073296055,0.0076213535,12.8278962785,11.635388035,12.0985236788,-9.2962105215,3.980405659,-142.8175077335,71,42,33
2015-08-07 02:14:05.486185,AMZN,0.0235649449,0.0305828226,0.0092703502,37.4081902773,48.5487257749,14.7162247572,29.7810062852,-69.6877219282,-334.0005615016,2,92,10
stripped down code
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm
ax = plt.subplots(1, 1, figsize=(16, 20), dpi=50) #width height inches
data=np.genfromtxt('/home/dave/Desktop/development/hvanal2015s.csv',
dtype='M8[us],S5,float,float,float',delimiter=',',usecols= [0,1,11,12,13])
my_dates = np.array([d[0] for d in data]).astype('datetime64[D]')
dates = np.unique(my_dates)
print(dates)
x_list = []
y_list = [10,11,12,13]
x_list = dates
plt.plot(x_list,y_list)
plt.title('hv 20 to 10 ranks',fontsize=20)
plt.xlabel('dates')
plt.ylabel('symbol ranks',fontsize=30)
plt.show()
and the output as a png file
Upvotes: 1
Views: 356
Reputation: 36655
matplotlib
does not support numpy
datetime64
objects, you need to convert it to python datetime
object and then select formatter
like in code below:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm
from datetime import datetime
import matplotlib.dates as mdates
fig,ax = plt.subplots(1, 1) #width height inches
data=np.genfromtxt('data',
dtype='M8[us],S5,float,float,float',delimiter=',',usecols= [0,1,11,12,13])
my_dates = np.array([d[0] for d in data]).astype('datetime64[D]')
dates = np.unique(my_dates)
print(dates)
x_list = []
x_list[:] = dates.astype(datetime)
y_list = [10,11,12,13]
plt.plot(x_list,y_list)
plt.title('hv 20 to 10 ranks',fontsize=20)
plt.xlabel('dates',fontsize=16)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.ylabel('symbol ranks',fontsize=30)
plt.show()
Upvotes: 1