我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用matplotlib.mlab.csv2rec()。
def _load_spike_times(self, fetfilename): """Reads and returns the spike times and features""" f = file(fetfilename, 'r') # Number of clustering features is integer on first line nbFeatures = int(f.readline().strip()) # Each subsequent line consists of nbFeatures values, followed by # the spike time in samples. names = ['fet%d' % n for n in xrange(nbFeatures)] names.append('spike_time') # Load into recarray data = mlab.csv2rec(f, names=names, skiprows=1, delimiter=' ') f.close() # get features features = np.array([data['fet%d' % n] for n in xrange(nbFeatures)]) # Return the spike_time column return data['spike_time'], features.transpose()
def test_transform_data(): """ Testing the transformation of the data from raw data to functions used for fitting a function. """ # We start with actual data. We test here just that reading the data in # different ways ultimately generates the same arrays. from matplotlib import mlab ortho = mlab.csv2rec(op.join(data_path, 'ortho.csv')) x1, y1, n1 = mli.transform_data(ortho) x2, y2, n2 = mli.transform_data(op.join(data_path, 'ortho.csv')) npt.assert_equal(x1, x2) npt.assert_equal(y1, y2) # We can also be a bit more critical, by testing with data that we # generate, and should produce a particular answer: my_data = pd.DataFrame( np.array([[0.1, 2], [0.1, 1], [0.2, 2], [0.2, 2], [0.3, 1], [0.3, 1]]), columns=['contrast1', 'answer']) my_x, my_y, my_n = mli.transform_data(my_data) npt.assert_equal(my_x, np.array([0.1, 0.2, 0.3])) npt.assert_equal(my_y, np.array([0.5, 0, 1.0])) npt.assert_equal(my_n, np.array([2, 2, 2]))
def insert_data(parameter): db = MySQLUtils(user='root', passwd='1988', dbname='stock', source='stock') # print parameter for stock in parameter: data = mlab.csv2rec(stock, delimiter='\t') pattern = re.compile("[SH]*[SZ]*[0-9]{6}", re.IGNORECASE) m = pattern.findall(stock) if len(m) == 1: # print data db.insert_ohlc_data(data, m[0])
def get_stock(ticker, startdate, enddate): fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh) fh.close() r.sort() print 'the length of data:', len(r.close) get_stock_data = [] for i in xrange(0, len(r.close)-1): if (r.volume[i] != 0): get_stock_data.append(r.close[i].tolist()) print 'after removing the datas with zero volume, the length of data:', len(get_stock_data) return get_stock_data
def get_stock(ticker, startdate, enddate): fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: (date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh) fh.close() r.sort() print 'the length of data:', len(r.close) get_stock_data = [] for i in xrange(0, len(r.close)-1): if (r.volume[i] != 0): get_stock_data.append(r.close[i].tolist()) print 'after removing the datas with zero volume, the length of data:', len(get_stock_data) return get_stock_data
def plotSingleTickerWithVolume(ticker, startdate, enddate): global ax fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: # date, open, high, low, close, volume, adj_close r = mlab.csv2rec(fh); fh.close() r.sort() plt.rc('axes', grid=True) plt.rc('grid', color='0.78', linestyle='-', linewidth=0.5) axt = ax.twinx() prices = r.adj_close fcolor = 'darkgoldenrod' ax.plot(r.date, prices, color=r'#1066ee', lw=2, label=ticker) ax.fill_between(r.date, prices, 0, prices, facecolor='#BBD7E5') ax.set_ylim(0.5*prices.max()) ax.legend(loc='upper right', shadow=True, fancybox=True) volume = (r.close*r.volume)/1e6 # dollar volume in millions vmax = volume.max() axt.fill_between(r.date, volume, 0, label='Volume', facecolor=fcolor, edgecolor=fcolor) axt.set_ylim(0, 5*vmax) axt.set_yticks([]) for axis in ax, axt: for label in axis.get_xticklabels(): label.set_rotation(30) label.set_horizontalalignment('right') axis.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
def plotTicker(ticker, startdate, enddate, fillcolor): """ matplotlib.finance has fetch_historical_yahoo() which fetches stock price data the url where it gets the data from is http://ichart.yahoo.com/table.csv stores in a numpy record array with fields: date, open, high, low, close, volume, adj_close """ fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh); fh.close() r.sort() ### plot the relative strength indicator prices = r.adj_close ### plot the price and volume data ax.plot(r.date, prices, color=fillcolor, lw=2, label=ticker) ax.legend(loc='top right', shadow=True, fancybox=True) # set the labels rotation and alignment for label in ax.get_xticklabels(): # To display date label slanting at 30 degrees label.set_rotation(30) label.set_horizontalalignment('right') ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d') #plot the tickers now