Is anyone aware of an issue in using the step function pyplot.step(x,y) where the x values are given by datetime instances??? I can get the function to plot easily except when I use a datetime series as the x values. I have a matrix that looks something like the following:
In [3]: Minor_trend_matrix[0:20]
Out[3]:
matrix([[ 0, 1],
[ 1, 2],
[ 1, 3],
[ 1, 4],
[-1, 5],
[ 0, 6],
[ 1, 7],
[ 2, 8],
[ 0, 9],
[ 1, 10],
[ 1, 11],
[ 1, 12],
[ 1, 13],
[ 1, 14],
[-1, 15],
[-1, 16],
[-1, 17],
[ 1, 18],
[ 1, 19],
[-1, 21]])
Note the first column is comprised only of -2,-1,0,1,2 These represent different actions required by the step function; -2 is high of indices range and then low of indices range -1 is low of indices range 0 is no action 1 is high of indices range 2 is low of indices range and then high of indices range
The indices refer to datetime objects. I think this is where the step function gets into trouble. Any suggestions or alternatives would be appreciated. Furthermore, in relation to the specifics of the actions represented by the integers do you think that since for example a run of the matrix may look like the following
[ 1, 2],
[ 1, 3],
[ 1, 4]
but I only want the action to take place on the final item before a change occurs excluding zero that I should eliminate all preceding entries prior to the pertinent item (that precedes the different entry i.e. in this case -1,2,-2) and if so how I would go about doing this?? Additionally any help on implementing the appropriate code to realise this goal would also be appreciated.
# Package Imports
import csv
import datetime
import matplotlib.pyplot as plt
import numpy as np
import dateutil.relativedelta as rd
import bisect
import scipy as sp
## File Settings
ipath = "/home/kane/Downloads/Charts/Wheat/W2/"
fname = "W2_0_L1N.CSV"
dateList = []
openList = []
highList = []
lowList = []
closeList = []
x = []
g = []
portfolio_list = []
result = []
portfolio = csv.DictReader(open(ipath + fname, "rb"))
portfolio_list.extend(portfolio)
for data in portfolio_list:
dateList.append(data['Date'])
openList.append(data[' Open'])
highList.append(data[' High'])
lowList.append(data[' Low'])
closeList.append(data[' Close'])
for item in dateList:
d = datetime.datetime.strptime(item, '%Y%m%d')
t = d.toordinal()
x.append(d)
g.append(t)
dataArray = np.asarray([x, openList, highList, lowList, closeList])
dataArrayOrd = np.array([g, openList, highList, lowList, closeList],
dtype = float)
The plotting section of the code is as follows:
# Plot settings
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111)
line0 = plt.plot(x, simple_slope, 'k', label = "High Slope")
line1 = plt.plot(x, dataArray[1,:], 'g', label = "Open")
line2 = plt.plot(x, dataArray[2,:], 'r', label = "High")
line3 = plt.plot(x, dataArray[3,:], 'y', label = "Low")
line4 = plt.plot(x, dataArray[4,:], 'b', label = "Close")
lns = line0 + line1 + line2 + line3 + line4
labs = [l.get_label() for l in lns]
ax.legend(lns,
labs,
loc='upper left',
frameon = False,
shadow=False,
fancybox=False,
labelspacing=0.1,
ncol=4,
prop={'size':9}
)
ax.set_title(fname , fontsize = 12)
plt.xticks(fontsize = 8)
plt.yticks(fontsize = 8)
ax.set_ylabel( 'Cents', fontsize = 10 )
ax.grid()
plt.show()
The x axis in this plot is time and as such datetime values so the step function should work as follows:
plt.step(x[Minor_trend_matrix[:,1]],dataArray[2,Minor_trend_matrix[:,1]])
Ofcourse the exact problem is a bit more complicated and will require to access
dataArray[3,Minor_trend_matrix[:,1]]
values when the step functions vertical line should move down to the low of the range and even more complex when it needs to access both the high and low or low and then high. This will be a matter of developing the correct conditional statement. However, the current problem lies in getting a simple version to work correctly with datetime values before I can progress.