def plotResults(strategiesToPlot, numTests, which=None): plt.xkcd() plt.figure() if which is None: # Which tells us to plot a subset of the results which = range(numTests) if len(strategiesToPlot) > 1: colors = plt.cm.rainbow(linspace(0, 1, len(strategiesToPlot))) else: colors = plt.cm.rainbow(linspace(0, 1, len(which))) colors = {a: b for (a, b) in zip(which, colors)} for nstrat, strategy in enumerate(strategiesToPlot): for n in xrange(numTests): if n not in which: continue if n == 0 or len(strategiesToPlot) == 1: title = '%s #%d' % (strategy, n) else: title = None f = shelve.open(r'data\%s_%d.dat' % (strategy, n)) if len(strategiesToPlot) > 1: pltAgainst(f, title, colors[nstrat]) else: pltAgainst(f, title, colors[n]) f.close() plt.legend(loc='upper left') plt.title('All Strategies') plt.show()
def plotResults(strategiesToPlot, numTests, which=None): plt.xkcd() plt.figure() if which is None: # Which tells us to plot a subset of the results which = range(numTests) if len(strategiesToPlot) > 1: colors = plt.cm.rainbow(linspace(0, 1, len(strategiesToPlot))) else: colors = plt.cm.rainbow(linspace(0, 1, len(which))) colors = {a: b for (a, b) in zip(which, colors)} for nstrat, strategy in enumerate(strategiesToPlot): for n in xrange(numTests): if n not in which: continue if n == 0 or len(strategiesToPlot) == 1: title = "%s #%d" % (strategy, n) else: title = None f = shelve.open(r"data\%s_%d.dat" % (strategy, n)) if len(strategiesToPlot) > 1: pltAgainst(f, title, colors[nstrat]) else: pltAgainst(f, title, colors[n]) f.close() plt.legend(loc="upper left") plt.title("All Strategies") plt.show()
def draw_xkcd(xx, yy, xlabel, ylabel, olabel, ext): """ xx is a date list """ pl.xkcd() matplotlib.rc('font', family='Arial', size=32) ax = pl.axes() x = np.array([date2num(vv) for vv in xx]) y = np.array(yy) #ax.plot(x, y, 'b', lw=1, label=olabel) pl.bar(x, y) ax.set_title(olabel, fontsize=128) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_xlim(datetime.date(2014, 8, 18), max(xx)) ax.set_ylim(0, max(yy)) fig = matplotlib.pyplot.gcf() fig.set_size_inches(24,12) fig.savefig(olabel+ext,dpi=120) pl.clf()
import matplotlib.pylab as plt from numpy import linspace, sin, pi plt.ion() plt.xkcd( ) #should work, but doesn't: AttributeError: GraphicsContextBase instance has no attribute 'draw_path' x = linspace(-pi, pi, 101) #plt.plot(x, sin(x), '--x', linewidth =2 ,label = "sin(x)") plt.plot(x, sin(x), '-') #plt.legend() plt.pause(0.1) raw_input()
def main( symbols, sDate, eDate, maxDaysHeld, numSim=5000, daySpacing=10, startDays=10, cumulative=True, outputFilename="simulation.dat", silent=False, ): ########### # Multiprocessing Stuff # raise KeyError # Establish communication queues tasks = multiprocessing.Queue() results = multiprocessing.Queue() # Start Workers num_workers = multiprocessing.cpu_count() * 1 if not silent: print "Creating %d workers" % num_workers consumers = [Worker(tasks, results) for i in xrange(num_workers)] for w in consumers: w.start() # Enqueue jobs num_jobs = 0 if type(symbols) is str: # Pull only a single stock df = grabSymbol(symbols, sDate, eDate) else: # Pull portfollio with tuples in form of ('symbol', pctAllocation) if abs(sum([a[1] for a in symbols]) - 1) > 0.01: raise RuntimeError("Sum of allocations must be 1.0") df = pandas.DataFrame() for symbol, pctAlloc in symbols: df = df.add(grabSymbol(symbol, sDate, eDate) * pctAlloc, fill_value=0) for daysHeld in range(startDays, maxDaysHeld, daySpacing): tasks.put(Task(daysHeld, numSim, df, cumulative)) num_jobs += 1 # Add a poison pill for each worker for i in xrange(num_workers): tasks.put(None) # Start printing results jobsTime = pandas.Series(pandas.datetime.now()) means = np.zeros(num_jobs) medians = np.zeros(num_jobs) low_25 = np.zeros(num_jobs) high_25 = np.zeros(num_jobs) wastedTime = np.zeros(num_jobs) while num_jobs: ans = results.get() if isinstance(ans, ErrorHolder): # We got an error, print it print ans else: (mn, mds, Xn, Yn, Cn, dH, l_25, h_25, w) = ans ix = (dH - startDays) / daySpacing means[ix] = mn medians[ix] = mds low_25[ix] = l_25 high_25[ix] = h_25 wastedTime[ix] = w if "X" not in vars().keys(): # Initalize results matrixes X = np.zeros([num_jobs, int(Xn.shape[0])]) Y = np.zeros([num_jobs, int(Yn.shape[0])]) C = np.zeros([num_jobs, int(Cn.shape[0])]) X[ix, :] = Xn Y[ix, :] = Yn C[ix, :] = Cn num_jobs -= 1 jobsTime = jobsTime.append(pandas.Series(pandas.datetime.now())) if not silent: print "Jobs left: %s (ETA %0.2f min)" % ( num_jobs, np.average([(jobsTime.iloc[n] - jobsTime.iloc[n - 1]).seconds for n in range(1, len(jobsTime))]) * num_jobs / 60.0, ) # Plot Results # Convert time (X) to years X = X.astype("f") / 365.25 if not silent: plt.xkcd() plt.pcolor(X, Y, C, cmap=plt.cm.YlOrRd) # means = pandas.rolling_mean(pandas.Series(means), 10) plt.plot(X[:, 1], means, color="k", label="Mean") plt.plot(X[:, 1], medians, color="b", label="Median") plt.plot(X[:, 1], low_25, color="g", label="Low25") plt.plot(X[:, 1], high_25, color="g", label="High25") plt.colorbar().set_label("Probability Higher/Lower than Median") plt.legend(loc="upper left") plt.xlabel("Years") plt.ylabel("Return") plt.grid(axis="y") plt.gca().yaxis.set_major_formatter(plt.FuncFormatter(lambda x, pos: "%0.2f%%" % (x * 100))) plt.show() plt.figure() plt.plot(X[:, 1], wastedTime) plt.title("Average number of days I lost waiting to invest") plt.show() # Save data to dat file for reading my plotMultipleGainsOverTime if not silent: print "Exporing data to file %s..." % outputFilename import shelve sh = shelve.open(outputFilename) sh["X"] = X sh["Y"] = Y sh["C"] = C sh["means"] = means sh["medians"] = medians sh["low_25"] = low_25 sh["high_25"] = high_25 sh["wastedTime"] = wastedTime sh["portfolio"] = symbols sh.close() return high_25, means, medians, low_25
import matplotlib.pylab as plt if __name__ == '__main__': A = np.array([[-1, 1], [0, -1]]) B = np.array([[-1, 5], [0, -2]]) print("Condition Number of A: {}".format(cond(A))) npts = 100 xmin, xmax = -3.0, 1.0 ymin, ymax = -2.0, 2.0 # x = np.arange(xmin, xmax, (xmax-xmin) / (npts-1)) # y = np.arange(ymin, ymax, (ymax-ymin) / (npts-1)) # X, Y = np.meshgrid(x, y) X, Y = np.meshgrid(np.linspace(xmin, xmax, npts), np.linspace(ymin, ymax, npts)) spectra_a, s_max = psa(A, X, Y, method='svd') spectra_b, s_max = psa(B, X, Y, method='svd') plt.xkcd() fig = plt.figure(figsize=(9, 4)) ax = fig.add_subplot(121) circles = np.logspace(np.log10(0.05), np.log10(2.1), 10)[:-3] CS = ax.contour(X, Y, spectra_a + 1e-20, levels=circles) plt.clabel(CS, inline=1, fontsize=10) ax = fig.add_subplot(122) CS = ax.contour(X, Y, spectra_b + 1e-20, levels=circles) plt.clabel(CS, inline=1, fontsize=10) plt.savefig('psuedo.pdf', axis='tight') plt.show()
plt.plot([1, 2]) plt.title(1) plt.subplot(222) plt.plot([1, 2]) plt.title(2) plt.subplot(223) plt.plot([1, 2]) plt.title(3) plt.subplot(224) plt.plot([1, 2]) plt.title(4) plt.tight_layout() #약간 margin을 주는 느낌 plt.show() #xkcd 스타일 with plt.xkcd(): plt.title('XKCD style plot!!!') plt.plot(X, C, label='cosine') t = 2 * np.pi / 3 plt.scatter(t, np.cos(t), 50, color='b') plt.annotate(r'0.5 Here!', xy=(t, np.cos(t)), xycoords='data', xytext=(-90, -50), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle='->')) plt.show() #============================================================================== # matplotlib의 여러가지 플롯
def main(symbols, sDate, eDate, maxDaysHeld, numSim=5000, daySpacing=10, startDays=10, cumulative=True, outputFilename='simulation.dat', silent=False): ########### # Multiprocessing Stuff #raise KeyError # Establish communication queues tasks = multiprocessing.Queue() results = multiprocessing.Queue() # Start Workers num_workers = multiprocessing.cpu_count() * 1 if not silent: print 'Creating %d workers' % num_workers consumers = [Worker(tasks, results) for i in xrange(num_workers)] for w in consumers: w.start() # Enqueue jobs num_jobs = 0 if type(symbols) is str: # Pull only a single stock df = grabSymbol(symbols, sDate, eDate) else: # Pull portfollio with tuples in form of ('symbol', pctAllocation) if abs(sum([a[1] for a in symbols]) - 1) > 0.01: raise RuntimeError('Sum of allocations must be 1.0') df = pandas.DataFrame() for symbol, pctAlloc in symbols: df = df.add(grabSymbol(symbol, sDate, eDate) * pctAlloc, fill_value=0) for daysHeld in range(startDays, maxDaysHeld, daySpacing): tasks.put(Task(daysHeld, numSim, df, cumulative)) num_jobs += 1 # Add a poison pill for each worker for i in xrange(num_workers): tasks.put(None) # Start printing results jobsTime = pandas.Series(pandas.datetime.now()) means = np.zeros(num_jobs) medians = np.zeros(num_jobs) low_25 = np.zeros(num_jobs) high_25 = np.zeros(num_jobs) wastedTime = np.zeros(num_jobs) while num_jobs: ans = results.get() if isinstance(ans, ErrorHolder): # We got an error, print it print ans else: (mn, mds, Xn, Yn, Cn, dH, l_25, h_25, w) = ans ix = (dH - startDays) / daySpacing means[ix] = mn medians[ix] = mds low_25[ix] = l_25 high_25[ix] = h_25 wastedTime[ix] = w if 'X' not in vars().keys(): # Initalize results matrixes X = np.zeros([num_jobs, int(Xn.shape[0])]) Y = np.zeros([num_jobs, int(Yn.shape[0])]) C = np.zeros([num_jobs, int(Cn.shape[0])]) X[ix, :] = Xn Y[ix, :] = Yn C[ix, :] = Cn num_jobs -= 1 jobsTime = jobsTime.append(pandas.Series(pandas.datetime.now())) if not silent: print "Jobs left: %s (ETA %0.2f min)"% \ (num_jobs, np.average([(jobsTime.iloc[n] - jobsTime.iloc[n-1]).seconds for n in range(1,len(jobsTime))])*num_jobs/60.0 ) # Plot Results # Convert time (X) to years X = X.astype('f') / 365.25 if not silent: plt.xkcd() plt.pcolor(X, Y, C, cmap=plt.cm.YlOrRd) #means = pandas.rolling_mean(pandas.Series(means), 10) plt.plot(X[:, 1], means, color='k', label='Mean') plt.plot(X[:, 1], medians, color='b', label='Median') plt.plot(X[:, 1], low_25, color='g', label='Low25') plt.plot(X[:, 1], high_25, color='g', label='High25') plt.colorbar().set_label('Probability Higher/Lower than Median') plt.legend(loc='upper left') plt.xlabel('Years') plt.ylabel('Return') plt.grid(axis='y') plt.gca().yaxis.set_major_formatter( plt.FuncFormatter(lambda x, pos: '%0.2f%%' % (x * 100))) plt.show() plt.figure() plt.plot(X[:, 1], wastedTime) plt.title("Average number of days I lost waiting to invest") plt.show() # Save data to dat file for reading my plotMultipleGainsOverTime if not silent: print "Exporing data to file %s..." % outputFilename import shelve sh = shelve.open(outputFilename) sh['X'] = X sh['Y'] = Y sh['C'] = C sh['means'] = means sh['medians'] = medians sh['low_25'] = low_25 sh['high_25'] = high_25 sh['wastedTime'] = wastedTime sh['portfolio'] = symbols sh.close() return high_25, means, medians, low_25
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pylab as plt plt.xkcd() # HMTK Catalogue Import/Export Libraries from hmtk.parsers.catalogue.csv_catalogue_parser import CsvCatalogueParser, CsvCatalogueWriter input_catalogue_file = '../data_input/hmtk_chile.csv' #input_catalogue_file = 'data_input/hmtk_sa.csv' parser = CsvCatalogueParser(input_catalogue_file) catalogue = parser.read_file() print 'Input complete: %s events in catalogue' % catalogue.get_number_events() print 'Catalogue Covers the Period: %s to %s' % (catalogue.start_year, catalogue.end_year) valid_magnitudes = catalogue.data['magnitude'] <> np.nan catalogue.select_catalogue_events(valid_magnitudes) valid_magnitudes = catalogue.data['magnitude'] >= 2.0 catalogue.select_catalogue_events(valid_magnitudes) print catalogue.data['magnitude'] def gr(m, a, b=1): return 10**(a) - 10**(b*m) def F_gr(m, b=1, m_min=2.0):