def __init__(self, idx0: int = 2000): self.__datafilepath = \ '\\'.join(os.path.dirname(os.path.abspath(__file__)).split('\\')[:-4]) \ + '\\data\\nrlmsise00_dataprocessed\\nrlmsise00_f107datapros.txt' self.__solardata = self.__loadcsv(self.__datafilepath) self.__solardata.index = pd.to_datetime(self.__solardata['date']) self.__f107data = self.__solardata[['F107']] print(self.__f107data) # self.__date0 = dt.datetime.strptime(self.__solardata['date'][idx0], '%Y-%m-%d') # for idx,date in enumerate(self.__date): # dtime = dt.datetime.strptime(date,'%Y-%m-%d') - self.__date0 # self.__date[idx+2000] = dtime.days*24*60*60 # self.__bestfit = self.fit_sin(self.__date,self.__f107) # self.plotbestfit() self.__f107.index = pd.to_datetime(self.__solardata['date']) print(self.__f107) self.__results = seasonal_decompose(self.__f107, model='multiplicative') fig = self.__results.plot() plot_mpl(fig)
def decompose(data): from plotly.plotly import plot_mpl from statsmodels.tsa.seasonal import seasonal_decompose h = data result = seasonal_decompose(h, model='additive') fig = result.plot() plot_mpl(fig)
def plot(): multiple_bars = plt.figure() x = obj_namelist bar0 = gabarito.objects.values() bar1 = class_dict['SIFT_10_5pct'].objects.values() bar2 = class_dict['SIFT_10_10pct'].objects.values() bar3 = class_dict['SIFT_10_25pct'].objects.values() bar4 = class_dict['SIFT_10_50pct'].objects.values() bar5 = class_dict['SIFT_10_75pct'].objects.values() bar6 = class_dict['SIFT_10_100pct'].objects.values() ax = plt.subplot(1,1,1) ax.bar(x, bar0, width=0.8, color='gray', align='center', label="GABARITO") ax.bar(x, bar1, width=0.6, color='r', align='center', label="SIFT_10_5pct") ax.bar(x, bar2, width=0.5, color='g', align='center', label="SIFT_10_10pct") ax.bar(x, bar3, width=0.4, color='b', align='center', label="SIFT_10_25pct") ax.bar(x, bar4, width=0.3, color='y', align='center', label="SIFT_10_50pct") ax.bar(x, bar5, width=0.2, color='black', align='center', label="SIFT_10_75pct") ax.bar(x, bar6, width=0.1, color='brown', align='center', label="SIFT_10_100pct") plt.grid(True, axis='y') plt.ylabel('Detecções corretas') plt.legend() plt.show() plot_url = py.plot_mpl(multiple_bars, filename='detected_itens')
def basic_fft_example(): Fs = 300.0 # sampling rate Ts = 1.0 / Fs # sampling interval t = np.arange(0, 1, Ts) # time vector #ff = 5; # frequency of the signal #y = np.sin(2*np.pi*5*t) y = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 10 * t) n = len(y) # length of the signal k = np.arange(n) T = n / Fs frq = k / T # two sides frequency range frq = frq[range(n // 2)] # one side frequency range Y = np.fft.fft(y) / n # fft computing and normalization Y = Y[range(n // 2)] fig, ax = plt.subplots(2, 1) ax[0].plot(t, y) ax[0].set_xlabel('Time') ax[0].set_ylabel('Amplitude') ax[1].plot(frq, abs(Y), 'r') # plotting the spectrum ax[1].set_xlabel('Freq (Hz)') ax[1].set_ylabel('|Y(freq)|') plot_url = py.plot_mpl(fig, filename='mpl-basic-fft') print(plot_url)
def Plotly(**options): """Shows the plot. For options, see Config. options: keyword args used to invoke various plt functions """ clf = options.pop('clf', True) Config(**options) import plotly.plotly as plotly url = plotly.plot_mpl(plt.gcf()) if clf: Clf() return url
def histogram(values, title): plt.hist(values) plt.title(title) plt.xlabel("Value") plt.ylabel("Frequency") fig = plt.gcf() plot_url = py.plot_mpl(fig, filename=title + '-histogram')
def generate_histogram(array, title, xlabel, ylabel, filename): plt.hist(array) plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) fig = plt.gcf() plot_url = py.plot_mpl(fig, filename=filename)
def generate_plot(interactive=True, png=False): fig, a1x = plt.subplots(sharex=True) a1x.plot(CALLS['x'], CALLS['y']) a1x.set(xlabel='time (s)', ylabel='No. requests', title='LOIC speed (total requests: {})'.format(TOTAL_CALLS)) a1x.grid() fig.savefig("test.png") if interactive: py.plot_mpl(fig) if png: plt.show()
def print_elong_diagram(elongations): print "printing histogram..." bins = np.linspace(-3, 0, 100) plt.hist(np.array(elongations), bins) plt.title("Ln(Contrast)") plt.xlabel("Value") plt.ylabel("Frequency") fig = plt.gcf() py.sign_in('Dimassio', 'ebukn0pzpq') plot_url = py.plot_mpl(fig, filename='Contrast')
def plot_many(list_of_species_pairs): fig = plt.figure() ypos = 1 for pair in list_of_species_pairs: if pair[0] == pairs[0][0]: ax = fig.add_axes([1,ypos,1,1]) output_loc_hist(pair[0], pair[1], ax) plot_url = py.plot_mpl(fig) print plot_url
def _build_report(self, recognizer, result): image_labels = [SOURCE_FOLDER] if self._debug: image_labels.extend(list(self._labels)) headers = list(image_labels) headers.append("detected") if self._debug: headers.extend(["expected", "M2"]) report = '' total, success, colors = 0, 0, dict() lev, all_lev = 0.0, dict() for img_name in self._image_names: col = "rgba(255, 255, 255, 0.1)" if img_name in self._expected: s_expected, s_res = self._expected[img_name], result[img_name] total += 1 lev_d = calc_d(s_expected, s_res) lev += lev_d all_lev[img_name] = lev_d if self._expected[img_name] == result[img_name]: success += 1 col = "rgba({0}, {1}, 0, 0.1)".format(*list((min(int(2*255*x), 255) for x in [1.0-lev_d, lev_d]))) colors[img_name] = col if total > 0: report += "<h3>Rate %d/%d = %d%% Lev %f </h3>" % (success, total, int(100*success/total), lev/total) try: plt.hist(list(all_lev.values())) plt.title("M2 Histogram") plt.xlabel("M2 Value") plt.ylabel("Number of images") fig = plt.gcf() plot_url = py.plot_mpl(fig, filename=recognizer.__class__.__name__, auto_open=False) report += tls.get_embed(plot_url) except: logging.warn("Failed to generate plot") report += '<table border="1" align="center">' report += "<tr>{0}</tr>".format(''.join(map(lambda l: "<th>{0}</th>".format(l), headers))) for img_name in sorted(self._image_names): rows = list() for lbl in image_labels: rows.append('<img width="300" src="%s">' % os.path.join(lbl, img_name + '.png')) rows.append(result[img_name]) if self._debug: rows.append(self._expected[img_name] if img_name in self._expected else "NOT AVAILABLE") rows.append("{0:.2f}".format(all_lev[img_name])) report += "<tr style=\"background-color:{0};\">{1}</tr>".format(colors[img_name], ''.join(map(lambda l: '<td align="center" style="padding: 5px;">{0}</td>'.format(l), rows))) report += '</table>' self._save_report(report)
def Plotly(**options): """Shows the plot. For options, see Config. options: keyword args used to invoke various pyplot functions """ clf = options.pop('clf', True) Config(**options) import plotly.plotly as plotly url = plotly.plot_mpl(pyplot.gcf()) if clf: Clf() return url
def main(): histArray = [] for i in range(100): p = Perceptron(1000) histArray.append(p.pla(save=False)) print histArray plt.hist(histArray) plt.xlabel("Iteration Number") plt.ylabel("Frequency") flg = plt.gcf() plot_url = py.plot_mpl(flg, filename='Py-Histogram')
def Plotly(clf=True, **options): """Shows the plot. For options, see Config. options: keyword args used to invoke various pyplot functions """ Config(**options) import plotly.plotly as plotly url = plotly.plot_mpl(pyplot.gcf()) if clf: Clf() return url
def plotly(**options): """Show the plot. For options, see Config. options: keyword args used to invoke various pyplot functions """ clf = options.pop('clf', True) config(**options) import plotly.plotly as plotly url = plotly.plot_mpl(pyplot.gcf()) if clf: clear_figure() return url
def plot(self, contributor_email, cid): """make all plots for contribution_id""" plot_contrib = self.contrib_coll.find_one( {'contribution_id': cid}, { 'content.data': 1, 'content.plots': 1, '_id': 0, 'collaborators': 1, 'project': 1 } ) if 'data' not in plot_contrib['content']: return None author = Author.parse_author(contributor_email) project = str(author.name).translate(None, '.').replace(' ','_') \ if 'project' not in plot_contrib else plot_contrib['project'] subfld = 'contributed_data.%s.plotly_urls.%d' % (project, cid) data = plot_contrib['content']['data'] df = pd.DataFrame.from_dict(data) url_list = list(self.mat_coll.find( {subfld: {'$exists': True}}, {'_id': 0, subfld: 1} )) urls = [] if len(url_list) > 0: urls = url_list[0]['contributed_data'][project]['plotly_urls'][str(cid)] for nplot,plotopts in enumerate( plot_contrib['content']['plots'].itervalues() ): filename = 'test%d_%d' % (cid,nplot) fig, ax = plt.subplots(1, 1) df.plot(ax=ax, **plotopts) if len(urls) == len(plot_contrib['content']['plots']): pyfig = py.get_figure(urls[nplot]) for ti,line in enumerate(ax.get_lines()): pyfig['data'][ti]['x'] = list(line.get_xdata()) pyfig['data'][ti]['y'] = list(line.get_ydata()) py.plot(pyfig, filename=filename, auto_open=False) else: update = dict( layout=dict( annotations=[dict(text=' ')], showlegend=True, legend=Legend(x=1.05, y=1) ), ) urls.append(py.plot_mpl( fig, filename=filename, auto_open=False, strip_style=True, update=update, resize=True )) return None if len(url_list) > 0 else urls
def histogram(): global numFile global newFile global matrix plt.hist(np.array(matrix.ravel()), bins=1024, range=(np.array(matrix.ravel()).min(), np.array(matrix.ravel()).max())) plt.title('Histogram') fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='HistogramRaw', height=np.array(matrix.ravel()).max()) #Convert to plotly figure plotly_fig = tls.mpl_to_plotly(fig) py.image.save_as( plotly_fig, str(basename(newFile)) + 'histogram' + str(numFile) + '.png')
def modulation(freq0, freq1, signal, sample_rate=300, quiet=True): ''' signal is a list of 0 or 1. freq0, freq1 represents frequency to decode 0 or 1, respectively. step is time step for each signal. modulation returns concatanation of sine functions accordingly. ''' print('modulation using 0:%d, 1:%d' % (freq0, freq1)) Fs = sample_rate Ts = 1.0 / Fs t = [] y = [] for i in range(len(signal)): tmp = np.arange(i, i + 1, Ts) t.extend(tmp) if signal[i] == 0: y.extend(np.sin(2 * np.pi * freq0 * tmp)) elif signal[i] == 1: y.extend(np.sin(2 * np.pi * freq1 * tmp)) else: assert False, 'Signal should be 0 or 1, %d found' % (singal[i]) n = len(y) # length of the signal k = np.arange(n) T = n / Fs frq = k / T # two sides frequency range frq = frq[range(n // 2)] # one side frequency range Y = np.fft.fft(y) / n # fft computing and normalization Y = Y[range(n // 2)] if not quiet: fig, ax = plt.subplots(2, 1) ax[0].plot(t, y) ax[0].set_xlabel('Time') ax[0].set_ylabel('Amplitude') ax[1].plot(frq, abs(Y), 'r') # plotting the spectrum ax[1].set_xlabel('Freq (Hz)') ax[1].set_ylabel('|Y(freq)|') plot_url = py.plot_mpl(fig, filename='mpl-basic-fft') return y, t
def drawChartBestFitness(gen): bestFit_agent = list() for key, value in gen.items(): bestFit_agent.append(value["best_fitness"]) fig = mat_plt.gcf() x = np.arange(len(gen)) #the x axes values bestFit = tuple(bestFit_agent) #best fitness of the generation ax = mat_plt.subplot(111) ax.bar(x, bestFit, width=0.2, color='g') #set the values for the axes ax.set_ylabel('Fitness') ax.set_xlabel('Generation') ax.set_title('Scores for best fitness per generation') mat_plt.show() plot_url = py.plot_mpl(fig, filename='mpl-basic-bar')
def main(): py.sign_in('rebecca_roisin', 'ay9gikxvge') # load simulated data with open("women_bias.pkl", "r") as f: all_women = cPickle.load(f) tot = 0 for b, women in all_women.iteritems(): # plot histogram fig = plt.figure() ax = plt.subplot(111) ax.hist(women, bins=np.arange(0, max(women)+1, 1), normed=True, histtype="step", label="bias: %s" % b) ax.set_xlabel("Number of Women") ax.set_ylabel("Frequency") plot_url = py.plot_mpl(fig) plt.show()
def para_hist(distrib, label, name): """ Usage: distrib_histogram(distrib, label, name) Argument: Distrib: a list of feature. Label: a list of labels. Name: a string as title of this plot. Return: A URL on `https://plot.ly`. """ plt.style.use('seaborn-deep') bins = np.linspace(-1, 1, 20) data = np.vstack(distrib).T plt.hist(data, bins, label=label) plt.xlabel("Value") plt.ylabel("Frequency") fig = plt.gcf() fig.savefig(name + ".pdf") plot_url = py.plot_mpl(fig, filename=name) return plot_url
def histogram(data, bins, offset=1, plotly_url=False): """ Receives raw data and transforms into a histogram. It can take any set of data and make a single histogram. :param data: List of List of numbers :param bin_size: Number :param y: List of numbers :param range: list [range_min, range_max] :param offset: number :return: """ # init graph histogram = plt.figure() plt.hist(data, bins, alpha=0.5, normed=True) plt.show() if plotly_url: plot_url = py.plot_mpl(histogram, filename='docs/histogram-mpl-same') print plot_url
def main(): os.system('clear') print('Program Start') print('Scraping: NASDAQ for AAPL') det_titles, det_open, det_high, det_low, det_close = stock_report() print('Result: The following information has been pulled from NASDAQ') prices = [] for detail_title, detail_open, detail_high, detail_low, detail_close in zip(det_titles, det_open, det_high, det_low, det_close): date1 = detail_title date1 = date2num(datetime.strptime(date1, '%b %d, %Y')) tup = (date1, float(detail_open), float(detail_close), float(detail_high), float(detail_low)) prices.append(tup) mondays = WeekdayLocator(MONDAY) alldays = DayLocator() weekFormatter = DateFormatter('%b %d') dayFormatter = ('%d') fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) candlestick(ax, prices, width=0.6) ax.xaxis_date() # ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') plt.show() py.sign_in('chriskoh', 'yyy0hoavmt') plot_url = py.plot_mpl(fig)
def overlap_hist(distrib, label, name): """ Usage: overlap_hist(distrib, label, name) Argument: Distrib: a list of features. Label: a list of labels. Name: a string as title of the plot. Return: A pdf was saved. A URL on `https://plot.ly`. """ plt.style.use('seaborn-deep') bins = np.linspace(-1, 1, 100) for idx, i in enumerate(distrib): plt.hist(i, bins, alpha=0.5, label=label[idx]) plt.legend(loc="upper right") plt.title(name) plt.xlabel("Value") plt.ylabel("Frequency") fig = plt.gcf() fig.savefig(name + ".pdf") plot_url = py.plot_mpl(fig, filename=name) return plot_url
def tryAll(): stringLeft= '/home/leyla/photo/IMG_8846.jpg'#'/home/leyla/pyProjects/вапвап/libviso/img/000001_left.jpg' stringRight = '/home/leyla/photo/IMG_8851.jpg' #'/home/leyla/pyProjects/вапвап/libviso/img/000001_right.jpg' # stringRight = '/home/leyla/pyProjects/вапвап/libviso/img/000002_right.jpg' # stringLeft= '/home/leyla/pyProjects/вапвап/libviso/img/000002_left.jpg' stringRightCur = '/home/leyla/pyProjects/вапвап/libviso/img/000002_right.jpg' stringLeftCur= '/home/leyla/pyProjects/вапвап/libviso/img/000002_left.jpg' maximaRight =[] maximaLeft =[] maximaRightCur =[] maximaRightCur =[] maximaLeftCur =[] blob_imgRight=[] corn_imgRight=[] blob_imgLeft=[] corn_imgLeft=[] blob_imgRightCur=[] corn_imgRightCur=[] blob_imgLeftCur=[] corn_imgLeftCur=[] imgRight = cv2.imread(stringRight, 0) imgLeft = cv2.imread(stringLeft, 0) # imgRightCur = cv2.imread(stringRightCur, 0) # imgLeftCur = cv2.imread(stringLeftCur, 0) # print imgRight.shape # imgLeft = np.roll(imgRight,100, axis=1) # imgLeft = np.delete(imgLeft,np.s_[:100:],1) start = time.time() blob_imgRight, corn_imgRight =findFeatures(imgRight, maximaRight, blob_imgRight, corn_imgRight) #right image blob_imgLeft, corn_imgLeft =findFeatures(imgLeft, maximaLeft, blob_imgLeft, corn_imgLeft) #left image # blob_imgRightCur, corn_imgRightCur =findFeatures(imgRightCur, maximaRightCur, blob_imgRightCur, corn_imgRightCur) #right image # blob_imgLeftCur, corn_imgLeftCur =findFeatures(imgLeftCur, maximaLeftCur, blob_imgLeftCur, corn_imgLeftCur) #left image # end = time.time() print end - start keypointsRight =[] keypointsLeft =[] # keypointsRightCur =[] # keypointsLeftCur =[] for i in range (0,len(maximaRight)): keypointsRight.append(cv2.KeyPoint(y= maximaRight[i].u, x = maximaRight[i].v,_size= 1, _angle = -1, _response=0, _octave=0, _class_id = -1)) for i in range (0,len(maximaLeft)): # if maxima1[i].c == 0: keypointsLeft.append(cv2.KeyPoint(y = maximaLeft[i].u,x = maximaLeft[i].v, _size= 1, _angle = -1, _response=0, _octave=0, _class_id = -1)) # for i in range (0,len(maximaRightCur)): # keypointsRightCur.append(cv2.KeyPoint(y= maximaRightCur[i].u, x = maximaRightCur[i].v,_size= 1, _angle = -1, _response=0, _octave=0, _class_id = -1)) # # for i in range (0,len(maximaLeftCur)): # # if maxima1[i].c == 0: # keypointsLeftCur.append(cv2.KeyPoint(y = maximaLeftCur[i].u,x = maximaLeftCur[i].v, _size= 1, _angle = -1, _response=0, _octave=0, _class_id = -1)) im_with_keypointsRight= cv2.drawKeypoints(imgRight, keypointsRight, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) im_with_keypointsLeft = cv2.drawKeypoints(imgLeft, keypointsLeft, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # im_with_keypointsRightCur= cv2.drawKeypoints(imgRightCur, keypointsRightCur, np.array([]), (0,0,255), # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # # im_with_keypointsLeftCur = cv2.drawKeypoints(imgLeftCur, keypointsLeftCur, np.array([]), (0,0,255), # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) #p = match_two_pictures(maximaLeft, maximaRight) #l r #p = match_four_pictures(maximaLeft, maximaRight, maximaLeftCur, maximaRightCur) s = time.clock() dataLeft = reorganize_data(maximaLeft, imgLeft) dataRight = reorganize_data(maximaRight,imgRight) # dataLeftCur= reorganize_data(maximaLeftCur,imgLeftCur) # dataRightCur = reorganize_data(maximaRightCur,imgRightCur) e = time.clock() print e - s s = time.clock() p = match_two_pictures_reorg(dataLeft, dataRight, maximaLeft) #p = match_four_pictures_reorg(dataLeft,dataRight,dataLeftCur,dataRightCur,maximaLeft) # cv2.drawMatchesKnn expects list of lists as matches. e = time.clock() print e - s print len(p) # # p = [] # #` # # files = 'matchingMovedImages.dat' # # f = open(files, 'rb') # # print len(p) # # # # # i = 0 # # while True: # # try: # # p.append( pickle.load(f)) # # except (EOFError): # # break # # f.close() # # print len(descr) keypoints_descr_Right=[] keypoints_descr_Left=[] keypoints_descr_RightCur=[] keypoints_descr_LeftCur=[] for i in range (0, len(p)): # u1p,v1p,u2p,v2p,u1c,v1c,u2c,v2c = p[i] keypoints_descr_Right.append(cv2.KeyPoint(y = p[i].uRp, x = p[i].vRp,_size= 1, _angle = -1, _response=0, _octave=0, _class_id = -1)) keypoints_descr_Left.append(cv2.KeyPoint( y= p[i].uLp, x = p[i].vLp,_size= 1, _angle = -1, _response=0, _octave=0, _class_id = -1)) # keypoints_descr_RightCur.append(cv2.KeyPoint(y = p[i].uRc, x = p[i].vRc,_size= 1, _angle = -1, _response=0, _octave=0, _class_id = -1)) # keypoints_descr_LeftCur.append(cv2.KeyPoint( y= p[i].uLc, x = p[i].vLc,_size= 1, _angle = -1, _response=0, _octave=0, _class_id = -1)) # kpts_descr_Right = cv2.drawKeypoints(imgRight, keypoints_descr_Right, np.array([]), (255,0,0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) kpts_descr_Left = cv2.drawKeypoints(imgLeft, keypoints_descr_Left, np.array([]), (0,255,0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # kpts_descr_RightCur = cv2.drawKeypoints(imgRightCur, keypoints_descr_RightCur, np.array([]), (0,0,255), # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # kpts_descr_LeftCur = cv2.drawKeypoints(imgLeftCur, keypoints_descr_LeftCur, np.array([]), (0,255,0), # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # cv2.imshow('features matched in Right', kpts_descr_Right) # cv2.imshow('features matched in Left', kpts_descr_Left) # # cv2.imshow('features matched in RightC', kpts_descr_RightCur) # cv2.imshow('features matched in LeftC', kpts_descr_LeftCur) # cv2.imshow('kptsL', im_with_keypointsLeft) # cv2.imshow('kptsR', im_with_keypointsRight) # cv2.imshow('kptsLc', im_with_keypointsLeftCur) # cv2.imshow('kptsRc', im_with_keypointsRightCur) # # both0 = kpts_descr_Left.copy() #l r # both1 = np.hstack((kpts_descr_LeftCur,kpts_descr_RightCur)) # # both2 = np.vstack((kpts_descr_Left,kpts_descr_LeftCur)) # both3 = np.vstack((kpts_descr_Right,kpts_descr_RightCur)) a = np.zeros(len(p)) # files = 'tan.dat' # # #f = open(files, 'a+b') # print both0.shape for i in range (0, len(p),5): # cv2.line(both2,( int(p[i].vLp),int(p[i].uLp)),(int(p[i].vLc),int(p[i].uLc+196 )),(255,0,0),1) # cv2.line(both3,( int(p[i].vRp),int(p[i].uRp)),(int(p[i].vRc),int(p[i].uRc+196 )),(255,0,0),1) #cv2.line(both0,( int(p[i].vLp),int(p[i].uLp)),(int(p[i].vRp+imgLeft.shape[1]),int(p[i].uRp )),(255,0,0),1) cv2.line(both0,( int(p[i].vLp),int(p[i].uLp)),(int(p[i].vRp),int(p[i].uRp )),(255,0,0),1) if p[i].vLp-p[i].vRp != 0: a[i] = float(float((p[i].uLp - p[i].uRp)) / float((p[i].vLp-p[i].vRp))) else: a[i] = np.nan; # cv2.line(both1,( int(p[i].vLc),int(p[i].uLc)),(int(p[i].vRc+672),int(p[i].uRc )),(255,0,0),1) # a[i] = (p[i].uLp - p[i].uRp) / (p[i].vLp-p[i].vRp) # pickle.dump(a[i], f) # f.close() # for i in range (0, len(p)): # if abs(p[i].vLp-p[i].vRp) >100: # pass # # cv2.line(both0,( p[i].vLp,p[i].uLp),(p[i].vRp,p[i].uRp+192 ),(255,0,0),1) # a[i] = float(float((p[i].uLp - p[i].uRp+196)) / float((p[i].vLp-p[i].vRp))) # # pickle.dump(a[i], f) # f.close() # f = open(files, 'rb') # i = 0 # while True: # try: # a.append( pickle.load(f)) # except (EOFError): # break a = np.array(a) a[a==-np.inf] = 50 a[np.isnan(a)] = 50 a[a==np.inf] = 50 # f.close() print a minimum = np.amin(a) maximum = np.amax(a) print 'min =', minimum, "max = ", maximum plt.hist(a,bins = 200) plt.title('plot_shelve_5cm') plt.xlabel("value") plt.ylabel('frequency') fig = plt.gcf() # plot_url = py.plot_mpl(fig, filename ='plot_shelve_5cm') # # # print len(a) #py.sign_in('username', 'api_key') # # # # print a # # if p[i].c1 == 1: # # cv2.line(both1,(p[i].v2p, p[i].u2p),(p[i].v1p, p[i].u1p+196),(0,255,0),1) # # if p[i].c1 == 2: # # cv2.line(both2,(p[i].v2p, p[i].u2p),(p[i].v1p, p[i].u1p+196),(0,100,100),1) # # if p[i].c1 == 3: # # cv2.line(both3,(p[i].v2p, p[i].u2p),(p[i].v1p, p[i].u1p+196),(255,200,0),1) plt.imshow(kpts_descr_Left, cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show() plt.imshow(kpts_descr_Right,cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show() # cv2.imshow("test1",both1) # cv2.imshow("test2",both2) # cv2.imshow("test3",both3) cv2.namedWindow('test', cv2.WINDOW_NORMAL) cv2.imshow('test',both0) k = cv2.waitKey(0) & 0xFF if k == 27: # wait for ESC key to exit cv2.destroyAllWindows() elif k == ord('s'): # wait for 's' key to save and exit cv2.imwrite('lines5cm.png',both0) cv2.destroyAllWindows() cv2.namedWindow('features matched in Right',cv2.WINDOW_NORMAL) cv2.imshow('features matched in Right', kpts_descr_Right) k = cv2.waitKey(0) & 0xFF if k == 27: # wait for ESC key to exit cv2.destroyAllWindows() elif k == ord('s'): # wait for 's' key to save and exit cv2.imwrite('right5cm.png',kpts_descr_Right) cv2.destroyAllWindows() cv2.namedWindow('features matched in Left',cv2.WINDOW_NORMAL) cv2.imshow('features matched in Left', kpts_descr_Left) k = cv2.waitKey(0) & 0xFF if k == 27: # wait for ESC key to exit cv2.destroyAllWindows() elif k == ord('s'): # wait for 's' key to save and exit cv2.imwrite('left5cm.png',kpts_descr_Left) cv2.destroyAllWindows() # cv2.waitKey(0) # cv2.destroyAllWindows() cv2.destroyAllWindows()
def plot_2d_graph(self, data_frame, gp, chart_type): if gp is None: gp = GraphProperties() if gp.chart_type is None and chart_type is None: chart_type = 'line' if gp.resample is not None: data_frame = data_frame.asfreq(gp.resample) self.apply_style_sheet(gp) # create figure & add a subplot fig = plt.figure(figsize = ((gp.width * gp.scale_factor)/gp.dpi, (gp.height * gp.scale_factor)/gp.dpi), dpi = gp.dpi) ax = fig.add_subplot(111) if gp.x_title != '': ax.set_xlabel(gp.x_title) if gp.y_title != '': ax.set_ylabel(gp.y_title) plt.xlabel(gp.x_title) plt.ylabel(gp.y_title) fig.suptitle(gp.title, fontsize = 14 * gp.scale_factor) # format Y axis y_formatter = matplotlib.ticker.ScalarFormatter(useOffset = False) ax.yaxis.set_major_formatter(y_formatter) # create a second y axis if necessary ax2 = [] if gp.y_axis_2_series != []: ax2 = ax.twinx() # do not use a grid with multiple y axes ax.yaxis.grid(False) ax2.yaxis.grid(False) # matplotlib 1.5 try: cyc = matplotlib.rcParams['axes.prop_cycle'] color_cycle = [x['color'] for x in cyc] except KeyError: # pre 1.5 pass # color_cycle = matplotlib.rcParams['axes.color_cycle'] bar_ind = np.arange(0, len(data_frame.index)) # for bar charts, create a proxy x-axis (then relabel) xd, bar_ind, has_bar, no_of_bars = self.get_bar_indices(data_frame, gp, chart_type, bar_ind) # plot the lines (using custom palettes as appropriate) try: # get all the correct colors (and construct gradients if necessary eg. from 'blues') color_spec = self.create_color_list(gp, data_frame) # for stacked bar yoff_pos = np.zeros(len(data_frame.index.values)) # the bottom values for stacked bar chart yoff_neg = np.zeros(len(data_frame.index.values)) # the bottom values for stacked bar chart zeros = np.zeros(len(data_frame.index.values)) # for bar chart bar_space = 0.2 bar_width = (1 - bar_space) / (no_of_bars) bar_index = 0 has_matrix = False # some lines we should exclude from the color and use the default palette for i in range(0, len(data_frame.columns.values)): if gp.chart_type is not None: if isinstance(gp.chart_type, list): chart_type = gp.chart_type[i] else: chart_type = gp.chart_type if chart_type == 'heatmap': # TODO experimental! # ax.set_frame_on(False) ax.pcolor(data_frame, cmap=plt.cm.Blues, alpha=0.8) # plt.colorbar() has_matrix = True break label = str(data_frame.columns[i]) ax_temp = self.get_axis(ax, ax2, label, gp.y_axis_2_series) yd = data_frame.ix[:,i] if color_spec[i] is None: color_spec[i] = color_cycle[i % len(color_cycle)] if (chart_type == 'line'): linewidth_t = self.get_linewidth(label, gp.linewidth, gp.linewidth_2, gp.linewidth_2_series) if linewidth_t is None: linewidth_t = matplotlib.rcParams['axes.linewidth'] ax_temp.plot(xd, yd, label = label, color = color_spec[i], linewidth = linewidth_t) elif(chart_type == 'bar'): # for multiple bars we need to allocate space properly bar_pos = [k - (1 - bar_space) / 2. + bar_index * bar_width for k in range(0,len(bar_ind))] ax_temp.bar(bar_pos, yd, bar_width, label = label, color = color_spec[i]) bar_index = bar_index + 1 elif(chart_type == 'stacked'): bar_pos = [k - (1 - bar_space) / 2. + bar_index * bar_width for k in range(0,len(bar_ind))] yoff = np.where(yd > 0, yoff_pos, yoff_neg) ax_temp.bar(bar_pos, yd, label = label, color = color_spec[i], bottom = yoff) yoff_pos = yoff_pos + np.maximum(yd, zeros) yoff_neg = yoff_neg + np.minimum(yd, zeros) # bar_index = bar_index + 1 elif(chart_type == 'scatter'): ax_temp.scatter(xd, yd, label = label, color = color_spec[i]) if gp.line_of_best_fit is True: self.trendline(ax_temp, xd.values, yd.values, order=1, color= color_spec[i], alpha=1, scale_factor = gp.scale_factor) # format X axis self.format_x_axis(ax, data_frame, gp, has_bar, bar_ind, has_matrix) except: pass if gp.display_source_label == True: ax.annotate('Source: ' + gp.source, xy = (1, 0), xycoords='axes fraction', fontsize=7 * gp.scale_factor, xytext=(-5 * gp.scale_factor, 10 * gp.scale_factor), textcoords='offset points', ha='right', va='top', color = gp.source_color) if gp.display_brand_label == True: self.create_brand_label(ax, anno = gp.brand_label, scale_factor = gp.scale_factor) leg = [] leg2 = [] loc = 'best' # if we have two y-axis then make sure legends are in opposite corners if ax2 != []: loc = 2 try: leg = ax.legend(loc = loc, prop={'size':10 * gp.scale_factor}) leg.get_frame().set_linewidth(0.0) leg.get_frame().set_alpha(0) if ax2 != []: leg2 = ax2.legend(loc = 1, prop={'size':10 * gp.scale_factor}) leg2.get_frame().set_linewidth(0.0) leg2.get_frame().set_alpha(0) except: pass try: if gp.display_legend is False: if leg != []: leg.remove() if leg2 != []: leg.remove() except: pass try: plt.savefig(gp.file_output, transparent=False) except: pass ####### various matplotlib converters are unstable # convert to D3 format with mpld3 try: # output matplotlib charts externally to D3 based libraries import mpld3 if gp.display_mpld3 == True: mpld3.save_d3_html(fig, gp.html_file_output) mpld3.show(fig) except: pass # FRAGILE! convert to Bokeh format # better to use direct Bokeh renderer try: if (gp.convert_matplotlib_to_bokeh == True): from bokeh.plotting import output_file, show from bokeh import mpl output_file(gp.html_file_output) show(mpl.to_bokeh()) except: pass # FRAGILE! convert matplotlib chart to Plotly format # recommend using AdapterCufflinks instead to directly plot to Plotly try: import plotly.plotly as py import plotly import plotly.tools as tls if gp.convert_matplotlib_to_plotly == True: plotly.tools.set_credentials_file(username = gp.plotly_username, api_key = gp.plotly_api_key) py_fig = tls.mpl_to_plotly(fig, strip_style = True) plot_url = py.plot_mpl(py_fig, filename = gp.plotly_url) except: pass # display in matplotlib window try: if GraphicsConstants.plotfactory_silent_display == True: return fig elif gp.silent_display == False: plt.show() else: return fig except: pass
# Compute the silhouette scores for each sample sample_silhouette_values = silhouette_samples(vectors, cluster_labels) print "The silhouette_avg is: ", silhouette_avg print sample_silhouette_values, len(sample_silhouette_values) x = range(len(vectors)) plt.bar(x,sample_silhouette_values,width=1, color='green') plt.xlabel("Sample number in the 20-NG dataset") plt.ylabel("Sample Silhouette Coefficient Values") plt.title("Individual silhoutte coefficient values (20-NG dataset)") fig = plt.gcf() fig.show() plot_url = py.plot_mpl(fig, filename='Sample silhouette values 20-NG dataset') # print sample_silhouette_values[cluster_labels == 0] # print sample_silhouette_values[cluster_labels == 1] # print sample_silhouette_values[cluster_labels == 2] # print sample_silhouette_values[cluster_labels == 3] # print sample_silhouette_values[cluster_labels == 4] # print "KMeans done!" # # print "Score: ", clusters.score(vectors[0.9*len(vectors):]) #, dataset.target[0.9*len(vectors):]) # print "Silhoutte score (total): ", silhouette_score(vectors, clusters.labels_) # print "Silhoutte sample_scores: ", silhouette_samples(vectors[:10],clusters.labels_[:10])
def plot_2d_graph(self, data_frame, gp, chart_type): if gp is None: gp = GraphProperties() if gp.chart_type is None and chart_type is None: chart_type = 'line' if gp.resample is not None: data_frame = data_frame.asfreq(gp.resample) self.apply_style_sheet(gp) # create figure & add a subplot fig = plt.figure(figsize=((gp.width * gp.scale_factor) / gp.dpi, (gp.height * gp.scale_factor) / gp.dpi), dpi=gp.dpi) ax = fig.add_subplot(111) if gp.x_title != '': ax.set_xlabel(gp.x_title) if gp.y_title != '': ax.set_ylabel(gp.y_title) plt.xlabel(gp.x_title) plt.ylabel(gp.y_title) fig.suptitle(gp.title, fontsize=14 * gp.scale_factor) # format Y axis y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False) ax.yaxis.set_major_formatter(y_formatter) # create a second y axis if necessary ax2 = [] if gp.y_axis_2_series != []: ax2 = ax.twinx() # do not use a grid with multiple y axes ax.yaxis.grid(False) ax2.yaxis.grid(False) # matplotlib 1.5 try: cyc = matplotlib.rcParams['axes.prop_cycle'] color_cycle = [x['color'] for x in cyc] except KeyError: # pre 1.5 pass # color_cycle = matplotlib.rcParams['axes.color_cycle'] bar_ind = np.arange(0, len(data_frame.index)) # for bar charts, create a proxy x-axis (then relabel) xd, bar_ind, has_bar, no_of_bars = self.get_bar_indices( data_frame, gp, chart_type, bar_ind) # plot the lines (using custom palettes as appropriate) try: # get all the correct colors (and construct gradients if necessary eg. from 'blues') color_spec = self.create_color_list(gp, data_frame) # for stacked bar yoff_pos = np.zeros(len(data_frame.index.values) ) # the bottom values for stacked bar chart yoff_neg = np.zeros(len(data_frame.index.values) ) # the bottom values for stacked bar chart zeros = np.zeros(len(data_frame.index.values)) # for bar chart bar_space = 0.2 bar_width = (1 - bar_space) / (no_of_bars) bar_index = 0 has_matrix = False # some lines we should exclude from the color and use the default palette for i in range(0, len(data_frame.columns.values)): if gp.chart_type is not None: if isinstance(gp.chart_type, list): chart_type = gp.chart_type[i] else: chart_type = gp.chart_type if chart_type == 'heatmap': # TODO experimental! # ax.set_frame_on(False) ax.pcolor(data_frame, cmap=plt.cm.Blues, alpha=0.8) # plt.colorbar() has_matrix = True break label = str(data_frame.columns[i]) ax_temp = self.get_axis(ax, ax2, label, gp.y_axis_2_series) yd = data_frame.ix[:, i] if color_spec[i] is None: color_spec[i] = color_cycle[i % len(color_cycle)] if (chart_type == 'line'): linewidth_t = self.get_linewidth(label, gp.linewidth, gp.linewidth_2, gp.linewidth_2_series) if linewidth_t is None: linewidth_t = matplotlib.rcParams['axes.linewidth'] ax_temp.plot(xd, yd, label=label, color=color_spec[i], linewidth=linewidth_t) elif (chart_type == 'bar'): # for multiple bars we need to allocate space properly bar_pos = [ k - (1 - bar_space) / 2. + bar_index * bar_width for k in range(0, len(bar_ind)) ] ax_temp.bar(bar_pos, yd, bar_width, label=label, color=color_spec[i]) bar_index = bar_index + 1 elif (chart_type == 'stacked'): bar_pos = [ k - (1 - bar_space) / 2. + bar_index * bar_width for k in range(0, len(bar_ind)) ] yoff = np.where(yd > 0, yoff_pos, yoff_neg) ax_temp.bar(bar_pos, yd, label=label, color=color_spec[i], bottom=yoff) yoff_pos = yoff_pos + np.maximum(yd, zeros) yoff_neg = yoff_neg + np.minimum(yd, zeros) # bar_index = bar_index + 1 elif (chart_type == 'scatter'): ax_temp.scatter(xd, yd, label=label, color=color_spec[i]) if gp.line_of_best_fit is True: self.trendline(ax_temp, xd.values, yd.values, order=1, color=color_spec[i], alpha=1, scale_factor=gp.scale_factor) # format X axis self.format_x_axis(ax, data_frame, gp, has_bar, bar_ind, has_matrix) except: pass if gp.display_source_label == True: ax.annotate('Source: ' + gp.source, xy=(1, 0), xycoords='axes fraction', fontsize=7 * gp.scale_factor, xytext=(-5 * gp.scale_factor, 10 * gp.scale_factor), textcoords='offset points', ha='right', va='top', color=gp.source_color) if gp.display_brand_label == True: self.create_brand_label(ax, anno=gp.brand_label, scale_factor=gp.scale_factor) leg = [] leg2 = [] loc = 'best' # if we have two y-axis then make sure legends are in opposite corners if ax2 != []: loc = 2 try: leg = ax.legend(loc=loc, prop={'size': 10 * gp.scale_factor}) leg.get_frame().set_linewidth(0.0) leg.get_frame().set_alpha(0) if ax2 != []: leg2 = ax2.legend(loc=1, prop={'size': 10 * gp.scale_factor}) leg2.get_frame().set_linewidth(0.0) leg2.get_frame().set_alpha(0) except: pass try: if gp.display_legend is False: if leg != []: leg.remove() if leg2 != []: leg.remove() except: pass try: plt.savefig(gp.file_output, transparent=False) except: pass ####### various matplotlib converters are unstable # convert to D3 format with mpld3 try: # output matplotlib charts externally to D3 based libraries import mpld3 if gp.display_mpld3 == True: mpld3.save_d3_html(fig, gp.html_file_output) mpld3.show(fig) except: pass # FRAGILE! convert to Bokeh format # better to use direct Bokeh renderer try: if (gp.convert_matplotlib_to_bokeh == True): from bokeh.plotting import output_file, show from bokeh import mpl output_file(gp.html_file_output) show(mpl.to_bokeh()) except: pass # FRAGILE! convert matplotlib chart to Plotly format # recommend using AdapterCufflinks instead to directly plot to Plotly try: import plotly.plotly as py import plotly import plotly.tools as tls if gp.convert_matplotlib_to_plotly == True: plotly.tools.set_credentials_file(username=gp.plotly_username, api_key=gp.plotly_api_key) py_fig = tls.mpl_to_plotly(fig, strip_style=True) plot_url = py.plot_mpl(py_fig, filename=gp.plotly_url) except: pass # display in matplotlib window try: if Constants.plotfactory_silent_display == True: pass elif gp.silent_display == False: plt.show() except: pass
import numpy as np import pylab import plotly.plotly as py legend_fig = plt.figure() x = np.linspace(0, 20, 1000) y1 = np.sin(x) y2 = np.cos(x) pylab.plot(x, y1, '-b', label='sine') pylab.plot(x, y2, '-r', label='cosine') pylab.ylim(-1.5, 2.0) plot_url = py.plot_mpl(legend_fig, filename='mpl-sine-cosine')
def plot_2d_graph(self, data_frame, gp, chart_type): if gp.resample is not None: data_frame = data_frame.asfreq(gp.resample) # set the matplotlib style sheet & defaults matplotlib.rcdefaults() # first search PyThalesians styles, then try matplotlib try: plt.style.use(Constants().plotfactory_pythalesians_style_sheet[gp.style_sheet]) except: plt.style.use(gp.style_sheet) matplotlib.rcParams.update({"font.size": matplotlib.rcParams["font.size"] * gp.scale_factor}) # create figure & add a subplot fig = plt.figure( figsize=((gp.width * gp.scale_factor) / gp.dpi, (gp.height * gp.scale_factor) / gp.dpi), dpi=gp.dpi ) ax = fig.add_subplot(111) # format Y axis y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False) ax.yaxis.set_major_formatter(y_formatter) # create a second y axis if necessary ax2 = [] if gp.y_axis_2_series != []: ax2 = ax.twinx() # do not use a grid with multiple y axes ax.yaxis.grid(False) ax2.yaxis.grid(False) # plot the lines (using custom palettes as appropriate) try: # get all the correct colors (and construct gradients if necessary eg. from 'blues') color_spec = self.create_color_list(gp, data_frame) if type == "bar": # bottom = np.cumsum(np.vstack((np.zeros(data_frame.values.shape[1]), data_frame.values)), axis=0)[:-1] # bottom = np.vstack((np.zeros((data_frame.values.shape[1],), dtype=data_frame.dtype), # np.cumsum(data_frame.values, axis=0)[:-1])) yoff = np.zeros(len(data_frame.index.values)) # the bottom values for stacked bar chart # some lines we should exclude from the color and use the default palette for i in range(0, len(data_frame.columns.values)): if chart_type is not None: if gp.chart_type is not None: if isinstance(gp.type, list): chart_type = gp.chart_type[i] else: chart_type = gp.chart_type label = str(data_frame.columns[i]) ax_temp = self.get_axis(ax, ax2, label, gp.y_axis_2_series) xd = data_frame.index yd = data_frame.ix[:, i] if chart_type == "line": linewidth_t = self.get_linewidth(label, gp.linewidth, gp.linewidth_2, gp.linewidth_2_series) if linewidth_t is None: linewidth_t = matplotlib.rcParams["axes.linewidth"] ax_temp.plot(xd, yd, label=label, color=color_spec[i], linewidth=linewidth_t) elif chart_type == "bar": ax_temp.bar(xd, yd, label=label, color=color_spec[i], bottom=yoff) yoff = yoff + yd elif chart_type == "scatter": ax_temp.scatter(xd, yd, label=label, color=color_spec[i]) if gp.line_of_best_fit is True: self.trendline( ax_temp, xd.values, yd.values, order=1, color=color_spec[i], alpha=1, scale_factor=gp.scale_factor, ) except: pass # format X axis self.format_x_axis(ax, data_frame, gp) fig.suptitle(gp.title, fontsize=14 * gp.scale_factor) if gp.display_source_label == True: ax.annotate( "Source: " + gp.source, xy=(1, 0), xycoords="axes fraction", fontsize=7 * gp.scale_factor, xytext=(-5 * gp.scale_factor, 10 * gp.scale_factor), textcoords="offset points", ha="right", va="top", color=gp.source_color, ) if gp.display_brand_label == True: self.create_brand_label(ax, anno=gp.brand_label, scale_factor=gp.scale_factor) leg = [] leg2 = [] loc = "best" # if we have two y-axis then make sure legends are in opposite corners if ax2 != []: loc = 2 try: leg = ax.legend(loc=loc, prop={"size": 10 * gp.scale_factor}) leg.get_frame().set_linewidth(0.0) leg.get_frame().set_alpha(0) if ax2 != []: leg2 = ax2.legend(loc=1, prop={"size": 10 * gp.scale_factor}) leg2.get_frame().set_linewidth(0.0) leg2.get_frame().set_alpha(0) except: pass try: if gp.display_legend is False: if leg != []: leg.remove() if leg2 != []: leg.remove() except: pass try: plt.savefig(gp.file_output, transparent=False) except: pass ####### various matplotlib converters are unstable # convert to D3 format with mpld3 try: if gp.display_mpld3 == True: mpld3.save_d3_html(fig, gp.html_file_output) mpld3.show(fig) except: pass # FRAGILE! convert to Bokeh format # better to use direct Bokeh renderer try: if gp.convert_matplotlib_to_bokeh == True: from bokeh.plotting import output_file, show from bokeh import mpl output_file(gp.html_file_output) show(mpl.to_bokeh()) except: pass # FRAGILE! convert matplotlib chart to Plotly format # recommend using AdapterCufflinks instead to directly plot to Plotly try: if gp.convert_matplotlib_to_plotly == True: plotly.tools.set_credentials_file(username=gp.plotly_username, api_key=gp.plotly_api_key) py_fig = tls.mpl_to_plotly(fig, strip_style=True) plot_url = py.plot_mpl(py_fig, filename=gp.plotly_url) except: pass # display in Matplotlib try: if gp.silent_display == False: plt.show() except: pass
plt.close() print 'plot creation complete' axt.set_title('Zone # ' + str(k + 1) + ' Estimated integration time: ' + str(len(goodtime)*10 + len(badtime)*10) + '(s)') #axt.errorbar(goodtime, goodflux, yerr=gooderr, fmt='o') #axt.errorbar(badtime, badflux, yerr=baderr, # fmt='ro') # Work on getting flags running, your close but not there axt.plot(gAppOutput[k], fluxfile[k], 'ko-') axt.plot(goodtime, goodflux, 'ko') axt.plot(badtime, badflux, 'ro') plotly_fig = tls.mpl_to_plotly(fig) plotly_fig["data"][0]["error_y"].update({ "visible": True, "color":"rgb(255,127,14)", "array":fluxfile[k] }) url = py.plot_mpl(fig, filename = readfile[i][0] + '_ZONE_' + str(k+1), auto_open = False) urlout.append(readfile[i][0] + '_ZONE_' + str(k+1) + '\t' + url + '\n') currentdir = os.path.abspath('.') os.chdir(start) try: logfile = open('HTMLOUT.log', 'a') except IOError: logfile = open('HTMLOUT.log', 'w') logfile.write(readfile[i][0] + '\t' + str(k+1) + '\n') logfile.close() os.chdir(currentdir) else: print 'Outputing single zone plot for target ', readfile[i][0] plt.figure(1) fig, axt = plt.subplots()
democratProbabilityList[j] += probabilityListSVM[j][0] for i in range(0, len(democratProbabilityList)): democratProbabilityList[i] /= 10 print democratProbabilityList numpy_hist = plt.figure() plt.hist(democratProbabilityList, bins=[ 0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.0 ]) plot_url = py.plot_mpl(numpy_hist, filename='numpy-bins') print np.mean(democratProbabilityList) predicted = clf.predict(X_new_tfidf) democratCount = 0 republicanCount = 0 for prediction in predicted: if prediction == 'Democrat': democratCount += 1 else: republicanCount += 1 print 'Democrat/Republican: ' + str(democratCount) + '/' + str(republicanCount) print print "SVM:"
import matplotlib.pyplot as plt import plotly.plotly as py import numpy as np py.sign_in('TestBot', 'r1neazxo9w') fig, ax = plt.subplots() ax.scatter(np.linspace(-1, 1, 50), np.random.randn(50)) plot_url = py.plot_mpl(fig, filename="mpl-scatter")
for i in range(len(Embed)): Cluster[Cluster_ind[i]].append(dataNames[i]) # Processing clustered data Uniq = [Cluster[0][0]] for i in range(1,len(Cluster[0])): if Cluster[0][i] != Cluster[0][i-1]: Uniq.append(Cluster[0][i]) Representation = [nameInd[n] for n in Cluster[0]] print("starting to plot") plt.hist(Representation, len(UniqNames)) plt.title("Gaussian Histogram") plt.xlabel("Value") plt.ylabel("Frequency") fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='mpl-basic-histogram') text = open('Names.txt', 'w') for s in Uniq: for c in s: text.write(c) text.write('\n')
def plot_2d_graph(self, data_frame, gp, chart_type): if gp.resample is not None: data_frame = data_frame.asfreq(gp.resample) # set the matplotlib style sheet & defaults matplotlib.rcdefaults() # first search PyThalesians styles, then try matplotlib try: plt.style.use(Constants().plotfactory_pythalesians_style_sheet[gp.style_sheet]) except: plt.style.use(gp.style_sheet) matplotlib.rcParams.update({'font.size': matplotlib.rcParams['font.size'] * gp.scale_factor}) # create figure & add a subplot fig = plt.figure(figsize = ((gp.width * gp.scale_factor)/gp.dpi, (gp.height * gp.scale_factor)/gp.dpi), dpi = gp.dpi) ax = fig.add_subplot(111) # format Y axis y_formatter = matplotlib.ticker.ScalarFormatter(useOffset = False) ax.yaxis.set_major_formatter(y_formatter) if gp.x_title != '': ax.set_xlabel(gp.x_title) if gp.y_title != '': ax.set_ylabel(gp.y_title) # create a second y axis if necessary ax2 = [] if gp.y_axis_2_series != []: ax2 = ax.twinx() # do not use a grid with multiple y axes ax.yaxis.grid(False) ax2.yaxis.grid(False) color_cycle = matplotlib.rcParams['axes.color_cycle'] bar_ind = np.arange(0, len(data_frame.index)) xd, bar_ind, has_bar, no_of_bars = self.get_bar_indices(data_frame, gp, chart_type, bar_ind) # plot the lines (using custom palettes as appropriate) try: # get all the correct colors (and construct gradients if necessary eg. from 'blues') color_spec = self.create_color_list(gp, data_frame) # for stacked bar yoff = np.zeros(len(data_frame.index.values)) # the bottom values for stacked bar chart # for bar chart # bar_ind = np.arange(len(data_frame.index)) # has_bar = False bar_space = 0.2 bar_width = (1 - bar_space) / (no_of_bars) bar_index = 0 # some lines we should exclude from the color and use the default palette for i in range(0, len(data_frame.columns.values)): if chart_type is not None: if gp.chart_type is not None: if isinstance(gp.chart_type, list): chart_type = gp.chart_type[i] else: chart_type = gp.chart_type label = str(data_frame.columns[i]) ax_temp = self.get_axis(ax, ax2, label, gp.y_axis_2_series) yd = data_frame.ix[:,i] if (chart_type == 'line'): linewidth_t = self.get_linewidth(label, gp.linewidth, gp.linewidth_2, gp.linewidth_2_series) if linewidth_t is None: linewidth_t = matplotlib.rcParams['axes.linewidth'] ax_temp.plot(xd, yd, label = label, color = color_spec[i], linewidth = linewidth_t) elif(chart_type == 'bar'): bar_pos = [k - (1 - bar_space) / 2. + bar_index * bar_width for k in range(0,len(bar_ind))] if color_spec[i] is not None: ax_temp.bar(bar_pos, yd, bar_width, label = label, color = color_spec[i]) else: ax_temp.bar(bar_pos, yd, bar_width, label = label, color = color_cycle[i % len(color_cycle)]) bar_index = bar_index + 1 # bar_ind = bar_ind + bar_width has_bar = True elif(chart_type == 'stacked'): ax_temp.bar(xd, yd, label = label, color = color_spec[i], bottom = yoff) yoff = yoff + yd has_bar = True elif(chart_type == 'scatter'): ax_temp.scatter(xd, yd, label = label, color = color_spec[i]) if gp.line_of_best_fit is True: self.trendline(ax_temp, xd.values, yd.values, order=1, color= color_spec[i], alpha=1, scale_factor = gp.scale_factor) # format X axis self.format_x_axis(ax, data_frame, gp, has_bar, bar_ind) except: pass plt.xlabel(gp.x_title) plt.ylabel(gp.y_title) fig.suptitle(gp.title, fontsize = 14 * gp.scale_factor) if gp.display_source_label == True: ax.annotate('Source: ' + gp.source, xy = (1, 0), xycoords='axes fraction', fontsize=7 * gp.scale_factor, xytext=(-5 * gp.scale_factor, 10 * gp.scale_factor), textcoords='offset points', ha='right', va='top', color = gp.source_color) if gp.display_brand_label == True: self.create_brand_label(ax, anno = gp.brand_label, scale_factor = gp.scale_factor) leg = [] leg2 = [] loc = 'best' # if we have two y-axis then make sure legends are in opposite corners if ax2 != []: loc = 2 try: leg = ax.legend(loc = loc, prop={'size':10 * gp.scale_factor}) leg.get_frame().set_linewidth(0.0) leg.get_frame().set_alpha(0) if ax2 != []: leg2 = ax2.legend(loc = 1, prop={'size':10 * gp.scale_factor}) leg2.get_frame().set_linewidth(0.0) leg2.get_frame().set_alpha(0) except: pass try: if gp.display_legend is False: if leg != []: leg.remove() if leg2 != []: leg.remove() except: pass try: plt.savefig(gp.file_output, transparent=False) except: pass ####### various matplotlib converters are unstable # convert to D3 format with mpld3 try: if gp.display_mpld3 == True: mpld3.save_d3_html(fig, gp.html_file_output) mpld3.show(fig) except: pass # FRAGILE! convert to Bokeh format # better to use direct Bokeh renderer try: if (gp.convert_matplotlib_to_bokeh == True): from bokeh.plotting import output_file, show from bokeh import mpl output_file(gp.html_file_output) show(mpl.to_bokeh()) except: pass # FRAGILE! convert matplotlib chart to Plotly format # recommend using AdapterCufflinks instead to directly plot to Plotly try: if gp.convert_matplotlib_to_plotly == True: plotly.tools.set_credentials_file(username = gp.plotly_username, api_key = gp.plotly_api_key) py_fig = tls.mpl_to_plotly(fig, strip_style = True) plot_url = py.plot_mpl(py_fig, filename = gp.plotly_url) except: pass # display in Matplotlib try: if gp.silent_display == False: plt.show() except: pass
''' data = [ go.Bar( x=['SMS', 'MAIL'], y=[arr_action['SMS'], arr_action['mail']] ) ] plot_url = py.plot(data, filename='basic-bar') for i in keywords: y1.append(arr_keyword[i]) data1 = [ go.Bar( x = keywords, y = y1 ) ] plot_url = py.plot(data1, filename='basic-bar1') ''' date = plt.figure() x = [0, 1] y = [arr_action['SMS'], arr_action['mail']] ax = plt.subplot(111) ax.bar(x,y) plt.show() plot_url = py.plot_mpl(date, filename='mpl-date-example')
import matplotlib.pyplot as plt import numpy as np import plotly.plotly as py py.sign_in('TestBot', 'r1neazxo9w') # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='mpl-line-style')
def savefigure(f, fname, figdir, figsave='local', plotlyprivate=False): if figsave == 'local': f.savefig(os.path.join(figdir, fname + '.png'), bbox_inches='tight', dpi=400) elif figsave == 'plotly': plotly.sign_in('askerry', 'tn0zp19ph1') plotly.plot_mpl(f, filename=fname, auto_open=False, world_readable=plotlyprivate)
import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt import plotly.plotly as py # tools to communicate with Plotly's server fig = plt.figure() # example data mu = 100 # mean of distribution sigma = 15 # standard deviation of distribution x = mu + sigma * np.random.randn(10000) num_bins = 50 # the histogram of the data n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5) # add a 'best fit' line y = mlab.normpdf(bins, mu, sigma) plt.plot(bins, y, 'r--') plt.xlabel('Smarts') plt.ylabel('Probability') # Tweak spacing to prevent clipping of ylabel plt.subplots_adjust(left=0.15) plot_url = py.plot_mpl(fig, filename='docs/histogram-mpl-legend')
py.sign_in('TestBot', 'r1neazxo9w') Fs = 150.0 # sampling rate Ts = 1.0 / Fs # sampling interval t = np.arange(0, 1, Ts) # time vector ff = 5 # frequency of the signal y = np.sin(2 * np.pi * ff * t) n = len(y) # length of the signal k = np.arange(n) T = n / Fs frq = k / T # two sides frequency range frq = frq[range(n / 2)] # one side frequency range Y = np.fft.fft(y) / n # fft computing and normalization Y = Y[range(n / 2)] fig, ax = plt.subplots(2, 1) ax[0].plot(t, y) ax[0].set_xlabel('Time') ax[0].set_ylabel('Amplitude') ax[1].plot(frq, abs(Y), 'r') # plotting the spectrum ax[1].set_xlabel('Freq (Hz)') ax[1].set_ylabel('|Y(freq)|') plot_url = py.plot_mpl(fig, filename='mpl-basic-fft')
import matplotlib.pyplot as plt import numpy as np import plotly.plotly as py py.sign_in('TestBot', 'r1neazxo9w') ax = plt.subplot(111) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05)) plt.ylim(-2,2) fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='mpl-simple-annotation')
ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() filename= sys.argv[1] headers=['pos', 'x', 'y', 'z'] df=pd.read_csv(filename, names=headers) df['c'] = df.apply(lambda row: math.sqrt(row.x**2 + row.y**2 + row.z**2), axis=1) duration, _ =df.shape n=duration epoch_start_time=int(sys.argv[2]) freq=int(sys.argv[3]) t_inc=duration/freq print("duration minutes=", t_inc/60) ts=time.mktime(time.localtime(epoch_start_time)) timestamps=np.linspace(ts,ts+t_inc,n) dates=[dt.datetime.fromtimestamp(ts) for ts in timestamps] datenums=md.date2num(dates) plt.ylabel('Accelerometer vector') plt.xlabel('Time') plt.title('Plot of '+ filename) plt.subplots_adjust(bottom=0.2) plt.xticks( rotation=25 ) ax=plt.gca() xfmt = md.DateFormatter('%d/%m %H:%M') ax.xaxis.set_major_formatter(xfmt) plt.plot(datenums, df.c,c=tableau20[0], ls='dotted') mpl_fig_obj= plt.figure() py.plot_mpl(mpl_fig_obj) #plt.show()
kmeansClusterer1 = KMeans(n_clusters=2) cluster_labels = kmeansClusterer1.fit_predict(X) sample_silhouette_values = silhouette_samples(X, cluster_labels) print sample_silhouette_values x = [2, 3, 4, 5, 6, 7] plt.bar(x, silhouette_avg, width=1, color="blue") plt.xlabel("Silhouette_avg") plt.ylabel("Number of clusters") plt.title("Silhoutte-avg vs Number of clusters") fig = plt.gcf() fig.show() plot_url = py.plot_mpl(fig, filename="silhouette_avg") x = range(150) y = sample_silhouette_values plt.bar(x, y, width=1, color="yellow") plt.xlabel("Sample number in the iris dataset") plt.ylabel("Sample Silhouette Coefficient Values") plt.title("Individual silhoutte coefficient values") fig = plt.gcf() fig.show() plot_url = py.plot_mpl(fig, filename="Sample silhouette values") # print "Clustering using only the first 2 dimensions..."
words_of_interest = [u"τὸ", u"ὦ"] # read the text into memory and split it into a list of words with codecs.open("greek.xml", 'r', 'utf-8') as f: f = f.read() words = f.split() for w in words: # here we can write one of two options # if w == u"\u1f00\u03bd\u03b1\u03c0\u03bb\u03b7\u03c1\u03bf\u1fe6\u03c4\u03b5": # or # if w == u"ἀναπληροῦτε" # if the word is of interest, indicate that fact by adding # 1 to the words_in_text list, else add a 0 to the list if w in words_of_interest: words_in_text.append(1) else: words_in_text.append(0) # plot the results n_observations = len(words_in_text) x_axis_vals = range(n_observations) width = 1/1.5 plt.bar(x_axis_vals, words_in_text, width, color="blue") # call the plotting service to show the results fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='time_series')
import matplotlib.pyplot as plt import numpy as np import plotly.plotly as py py.sign_in('TestBot', 'r1neazxo9w') x = np.linspace(0, 10) line, = plt.plot(x, np.sin(x), '--', linewidth=2) dashes = [10, 5, 100, 5] # 10 points on, 5 off, 100 on, 5 off line.set_dashes(dashes) fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='mpl-basic-line')
import matplotlib.pyplot as plt import numpy as np import plotly.plotly as py # Learn about API authentication here: https://plot.ly/python/getting-started # Find your api_key here: https://plot.ly/settings/api gaussian_numbers = np.random.randn(1000) plt.hist(gaussian_numbers) plt.title("Gaussian Histogram") plt.xlabel("Value") plt.ylabel("Frequency") fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='mpl-basic-histogram')
ax.plot(x.real, x.imag , 'k-', linewidth = 2) ax.plot((0.5*w**0.5*x).real, (0.5*w**0.5*x).imag , 'k-', linewidth = 2) # ax.plot( n0.real, n0.imag , 'k-') # ax.plot( n1.real, n1.imag , 'g-') # ax.plot( n2.real, n2.imag , 'y-', linewidth=4) # ax.plot( n3.real, n3.imag , 'g-', linewidth=3) ax.plot( n5_3.real, n5_3.imag , 'g-', linewidth=1.5) ax.plot( n5_2.real, n5_2.imag , 'b-', linewidth=1.5) ax.plot( n5_1.real, n5_1.imag , 'r-', linewidth=1.5) # ploting the start and end points... # ax.plot( n5[0].real, n5[0].imag , 'go', markersize=8) # ax.plot( n5[-1].real, n5[-1].imag , 'ro', markersize=8) # just a plot of the origin for reference... # plt.plot( [0], [0], 'bo') plt.axis('Equal') # plt.grid(True) # plt.show() # sending it to plotly... plot_url = py.plot_mpl(fig)
y_pos += 0.5 elif column == "Data_column_2_name": y_pos += 0.5 elif column == "Data_column_3_name": y_pos += 0.25 elif column == "Data_column_4_name": y_pos += 0.75 elif column == "Data_column_5_name": y_pos += 1.25 # Again, make sure that all labels are large enough to be easily read plt.text(2011.5, y_pos, column, fontsize=14, color=tableau20[rank]) # Plot the text title plt.text(1995, 93, " This is the title. Will appear in the top of the graph", fontsize=17, ha="center") # Plot the references plt.text(1966, -8, "Here you can put the data source" "Authors : " "and notes : " , fontsize=10) # Save the figure as PNG. # You can also save it as PDF, JPEG. plt.savefig("the_name_of_the_data.png", bbox_inches="tight") ## make it an interactive object with plotly (see again) import plotly.plotly as py mpl_fig_obj = plt.figure() py.plot_mpl(mpl_fig_obj)
names = ['date', 'time', 'sat', 'S1C', 'ELE'], header=None, skipfooter=1, engine='python') except: print("failure in parsing file ", filename) sys.exit(1) py.sign_in('nuthaus', '2c9uspwge8') # find the unique SVs in view sats = np.unique(data['sat']) # create a plot of all the PRNs to frame the detection problem plt.figure() svgroup = data.groupby('sat') svgroup['S1C'].plot() plt.title('Carrier-to-Noise Ratio: Is GPS jamming present?') plot_url = py.plot_mpl(plt.gcf()) # compute the jamming metrics indicator = (data['S1C'].median()-pd.rolling_mean(data['S1C'], 30).values) > 4 intensity = data['S1C'].median() - pd.rolling_mean(data['S1C'], 30).values dates = data['date_time'].values # plot the jamming metrics plt.figure(tight_layout=True) plt.subplot(211) plt.ylabel('Intensity') plt.plot(intensity) plt.title('Developing a jamming intensity and detection metric') plt.subplot(212) plt.plot(indicator) plt.ylabel('Indicator')
from statsmodels.tsa.seasonal import seasonal_decompose data = Series.from_csv('IPG2211A2N.csv', header=0) result = seasonal_decompose(data, model='multiplicative') result.plot() pyplot.show() import plotly # plotly.tools.set_credentials_file() from plotly.plotly import plot_mpl from statsmodels.tsa.seasonal import seasonal_decompose result=sm.tsa.seasonal_decompose(data) result = seasonal_decompose(data, model='multiplicative') fig = result.plot() plot_mpl(fig) #pip install pyramid-arima from pyramid.arima import auto_arima #m greater is much great m=1000 is good stepwise_model = auto_arima(data, start_p=1, start_q=1, max_p=1, max_q=1, m=3, start_P=0, seasonal=True, d=1, D=1, trace=True, error_action='ignore', suppress_warnings=True, stepwise=True) stepwise_model.aic()
import matplotlib.pyplot as plt from matplotlib import gridspec import plotly.plotly as py py.sign_in('TestBot', 'r1neazxo9w') fig = plt.figure() gs = gridspec.GridSpec(3, 3) ax1 = fig.add_subplot(gs[0,:]) ax1.plot([1,2,3,4,5], [10,5,10,5,10], 'r-') ax2 = fig.add_subplot(gs[1,:-1]) ax2.plot([1,2,3,4], [1,4,9,16], 'k-') ax3 = fig.add_subplot(gs[1:, 2]) ax3.plot([1,2,3,4], [1,10,100,1000], 'b-') ax4 = fig.add_subplot(gs[2,0]) ax4.plot([1,2,3,4], [0,0,1,1], 'g-') ax5 = fig.add_subplot(gs[2,1]) ax5.plot([1,2,3,4], [1,0,0,1], 'c-') gs.update(wspace=0.5, hspace=0.5) fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='mpl-simple-subplot')
w += s * x if save: self.plot(vec=w) plt.title('N = %s, Iteration %s\n' \ % (str(N),str(it))) plt.savefig('p_N%s_it%s' % (str(N),str(it)), \ dpi=200, bbox_inches='tight') self.w = w print it iterations.append(it) def check_error(self, M, vec): check_pts = self.generate_points(M) return self.classification_error(vec, pts=check_pts) iterations = [] for x in range(0, 100): p = Perceptron(1000) p.pla(save=False) plt.hist(iterations) plt.title("10th Dimension Perceptron Iterations") plt.xlabel("Value") plt.ylabel("Frequency") fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='Iterations')
import matplotlib.pyplot as plt import plotly.plotly as py import numpy as np py.sign_in('TestBot', 'r1neazxo9w') Fs = 150.0; # sampling rate Ts = 1.0/Fs; # sampling interval t = np.arange(0,1,Ts) # time vector ff = 5; # frequency of the signal y = np.sin(2*np.pi*ff*t) n = len(y) # length of the signal k = np.arange(n) T = n/Fs frq = k/T # two sides frequency range frq = frq[range(n/2)] # one side frequency range Y = np.fft.fft(y)/n # fft computing and normalization Y = Y[range(n/2)] fig, ax = plt.subplots(2, 1) ax[0].plot(t,y) ax[0].set_xlabel('Time') ax[0].set_ylabel('Amplitude') ax[1].plot(frq,abs(Y),'r') # plotting the spectrum ax[1].set_xlabel('Freq (Hz)') ax[1].set_ylabel('|Y(freq)|') plot_url = py.plot_mpl(fig, filename='mpl-basic-fft')
def plot_2d_graph(self, data_frame, gp, type): matplotlib.rcdefaults() # set the matplotlib style sheet plt.style.use(Constants().plotfactory_pythalesians_style_sheet[Constants().plotfactory_default_stylesheet]) if hasattr(gp, 'style_sheet'): plt.style.use(Constants().plotfactory_pythalesians_style_sheet[gp.style_sheet]) scale_factor = Constants().plotfactory_scale_factor if hasattr(gp, 'scale_factor'): scale_factor = gp.scale_factor dpi = Constants().plotfactory_dpi if hasattr(gp, 'dpi'): dpi = gp.dpi width = Constants().plotfactory_width; height = Constants().plotfactory_height if hasattr(gp, 'width'): width = gp.width if hasattr(gp, 'height'): width = gp.height fig = plt.figure(figsize = ((width * scale_factor)/dpi, (height * scale_factor)/dpi), dpi = dpi) # add a subplot ax = fig.add_subplot(111) matplotlib.rcParams.update({'font.size': matplotlib.rcParams['font.size'] * scale_factor}) # format Y axis y_formatter = matplotlib.ticker.ScalarFormatter(useOffset = False) ax.yaxis.set_major_formatter(y_formatter) y_axis_2_series = [] ax2 = [] color_2_series = [] linewidth_2_series = [] if hasattr(gp, 'resample'): data_frame = data_frame.asfreq(gp.resample) # create a second y axis if necessary if hasattr(gp, 'y_axis_2_series'): if gp.y_axis_2_series == []: pass else: y_axis_2_series = gp.y_axis_2_series ax2 = ax.twinx() # matplotlib.rcParams.update({'figure.subplot.right': matplotlib.rcParams['figure.subplot.right'] - 0.05}) # do not use a grid with multiple y axes ax.yaxis.grid(False) ax2.yaxis.grid(False) # is there a second palette? if hasattr(gp, 'color_2_series'): if hasattr(gp.color_2_series, 'values'): color_2_series = [str(x) for x in gp.color_2_series.values] else: color_2_series = [str(x) for x in gp.color_2_series] # is there a second linewidth series if hasattr(gp, 'linewidth_2_series'): if hasattr(gp.linewidth_2_series, 'values'): linewidth_2_series = [str(x) for x in gp.linewidth_2_series.values] else: linewidth_2_series = [str(x) for x in gp.linewidth_2_series] # plot the lines (using custom palettes as appropriate) try: color = []; color_2 = [] linewidth_2 = matplotlib.rcParams['axes.linewidth'] exclude_from_color = [] if hasattr(gp, 'color'): if isinstance(gp.color, list): color = gp.color else: try: color = self.create_colormap( len(data_frame.columns.values) - len(color_2_series), gp.color) except: pass if hasattr(gp, 'width'): if isinstance(gp.width, list): color = gp.color else: try: color = self.create_colormap( len(data_frame.columns.values) - len(color_2_series), gp.color) except: pass if hasattr(gp, 'color_2'): if isinstance(gp.color_2, list): color_2 = gp.color_2 else: try: color_2 = self.create_colormap(len(color_2_series), gp.color_2) except: pass if hasattr(gp, 'exclude_from_color'): if not(isinstance(gp.exclude_from_color, list)): gp.exclude_from_color = [gp.exclude_from_color] exclude_from_color = [str(x) for x in gp.exclude_from_color] if hasattr(gp, 'linewidth_2'): linewidth_2 = gp.linewidth_2 axis_1_color_index = 0 axis_2_color_index = 0 if type == 'bar': # bottom = np.cumsum(np.vstack((np.zeros(data_frame.values.shape[1]), data_frame.values)), axis=0)[:-1] # bottom = np.vstack((np.zeros((data_frame.values.shape[1],), dtype=data_frame.dtype), # np.cumsum(data_frame.values, axis=0)[:-1])) yoff = np.zeros(len(data_frame.index.values)) # the bottom values for stacked bar chart # some lines we should exclude from the color and use the default palette for i in range(0, len(data_frame.columns.values)): label = str(data_frame.columns[i]) ax_temp = self.get_axis(ax, ax2, label, y_axis_2_series) color_spec, axis_1_color_index, axis_2_color_index = \ self.get_color(label, axis_1_color_index, axis_2_color_index, color, color_2, exclude_from_color, color_2_series) xd = data_frame.index; yd = data_frame.ix[:,i] if (type == 'line'): linewidth = self.get_linewidth(label, linewidth_2, linewidth_2_series) ax_temp.plot(xd, yd, label = label, color = color_spec, linewidth = linewidth) elif(type == 'bar'): ax_temp.bar(xd, yd, label = label, color = color_spec, bottom = yoff) yoff = yoff + yd elif(type == 'scatter'): ax_temp.scatter(xd, yd, label = label, color = color_spec) if hasattr(gp, 'line_of_best_fit'): if gp.line_of_best_fit == True: self.trendline(ax_temp, xd.values, yd.values, order=1, color= color_spec, alpha=1, scale_factor = scale_factor) except: pass # format X axis self.format_x_axis(ax, data_frame, gp) try: fig.suptitle(gp.title, fontsize = 14 * scale_factor) except: pass try: source = Constants().plotfactory_source source_color = 'black' display_brand_label = False if hasattr(gp, 'source'): source = gp.source display_brand_label = True if hasattr(gp, 'source_color'): source_color = self.get_color_code(gp.source_color) if display_brand_label or Constants().plotfactory_display_brand_label: ax.annotate('Source: ' + source, xy = (1, 0), xycoords='axes fraction', fontsize=7 * scale_factor, xytext=(-5 * scale_factor, 10 * scale_factor), textcoords='offset points', ha='right', va='top', color = source_color) except: pass if hasattr(gp, 'display_brand_label'): if gp.display_brand_label is True: self.create_brand_label(ax, anno = Constants().plotfactory_brand_label, scale_factor = scale_factor) else: if Constants().plotfactory_display_brand_label is True: self.create_brand_label(ax, anno = Constants().plotfactory_brand_label, scale_factor = scale_factor) leg = [] leg2 = [] loc = 'best' # if we have two y-axis then make sure legends are in opposite corners if ax2 != []: loc = 2 try: leg = ax.legend(loc = loc, prop={'size':10 * scale_factor}) leg.get_frame().set_linewidth(0.0) leg.get_frame().set_alpha(0) if ax2 is not []: leg2 = ax2.legend(loc = 1, prop={'size':10 * scale_factor}) leg2.get_frame().set_linewidth(0.0) leg2.get_frame().set_alpha(0) except: pass try: if gp.display_legend == False: if leg is not[]: leg.remove() if leg2 is not[]: leg.remove() except: pass try: plt.savefig(gp.file_output, transparent=False) except: pass try: if hasattr(gp, 'silent_display'): if gp.silent_display is False: plt.show() else: plt.show() except: pass # convert to D3 format with mpld3 try: if hasattr(gp, 'html_file_output'): mpld3.save_d3_html(fig, gp.html_file_output) if hasattr(gp, 'display_mpld3'): if gp.display_mpld3 == True: mpld3.show(fig) except: pass # convert to Plotly format (fragile!) # TODO better to create Plotly graphs from scratch rather than convert from matplotlib # TODO also dependent on matplotlib version for support try: if hasattr(gp, 'plotly_url'): plotly.tools.set_credentials_file(username = Constants().plotly_username, api_key = Constants().plotly_api_key) py_fig = tls.mpl_to_plotly(fig, strip_style = True) plot_url = py.plot_mpl(py_fig, filename = gp.plotly_url) except: pass
import matplotlib.pyplot as plt import plotly.plotly as py py.sign_in('TestBot', 'r1neazxo9w') fig, ax = plt.subplots() ax.plot([2,1,3,1,2]) update = {'data':[{'fill': 'tozeroy'}]} # this updates the trace plot_url = py.plot_mpl(fig, update=update, filename='mpl-basic-area')
print(jpm_sp500_regression.summary()) print('Pearson covariance: ' + str((jpm_df['Adj Close']).corr(sp500_df['Adj Close']))) # --------------------------------------------- # Forecast S&P/Case-Schiller: unit root exist shiller_df = shiller_df.resample('M').last() # shiller_df.plot() # plt.show() adf_shiller = adfuller(shiller_df['CSUSHPINSA']) print('ADF test p-value ' + str(adf_shiller[1])) # Trend, Seasonal check figure = seasonal_decompose(shiller_df, model='multiplicative').plot() plot_mpl(figure) # ACF PACF: proposed ARIMA(0,1,4) # fig, axes = plt.subplots(4, 3) # shiller_df['CSUSHPINSA'].plot(ax=axes[0,0], title='Original Series') # plot_acf(shiller_df['CSUSHPINSA'], ax=axes[0,1]) # plot_pacf(shiller_df['CSUSHPINSA'], ax=axes[0,2]) # shiller_df['CSUSHPINSA'].diff().dropna().plot(ax=axes[1,0], title='1st Order Differencing') # plot_acf(shiller_df['CSUSHPINSA'].diff().dropna(), ax=axes[1,1]) # plot_pacf(shiller_df['CSUSHPINSA'].diff().dropna(), ax=axes[1,2]) # shiller_df['CSUSHPINSA'].diff().diff().dropna().plot(ax=axes[2,0], title='2nd Order Differencing') # plot_acf(shiller_df['CSUSHPINSA'].diff().diff().dropna(), ax=axes[2,1]) # plot_pacf(shiller_df['CSUSHPINSA'].diff().diff().dropna(), ax=axes[2,2])
def test_hash_function(func, keys, bucket): results = [0] * bucket #[0,0,0,0...] for counts keys_used = [] for w in keys: if w not in keys_used: hv = func(w, bucket) results[hv] += 1 keys_used.append(w) return results # counts for each bucket words = get_page('http://www.gutenberg.org/cache/epub/1661/pg1661.txt').split() counts = test_hash_function(bad_hash_string, words, 12) # print counts # [730, 1541, 1055, 1752, 1784, 839, 1452, 2074, 1409, 754, 924, 899] # counts = test_hash_function(hash_string,words,12) # print counts # [1368, 1268, 1273, 1279, 1284, 1245, 1207, 1228, 1281, 1232, 1233, 1315] counts = test_hash_function(hash_string, words, 100) import matplotlib.pyplot as plt # download from cmd using pip install import plotly.plotly as py N = 100 x = range(N) width = 1 / 1.5 plt.bar(x, counts, width, color='blue') fig = plt.gcf() plot_url = py.plot_mpl(fig, filename='hash string')
counter += 1 plt.legend(loc='best', fontsize=fs) # plt.savefig(save_names[1]+'.png') # Accuracy acc = plt.figure() plt.title('Accuracy measures for different traing data size') plt.xlabel('Proportion of training data used') plt.ylabel('accuracy') X_proportion = np.linspace(0.1, 1.0, 8) counter = 0 for key in mvd.measure_data.keys(): plt.plot(X_proportion, mvd.measure_data[key]['accuracy'], c=colors[counter], ls=linestyles[counter], marker=markers[counter], label=key) counter += 1 plt.legend(loc='best', fontsize=fs) # plt.savefig(save_names[2]+'.png') # plotly_roc_auc = tls.mpl_to_plotly(roc_auc) # plotly_f1 = tls.mpl_to_plotly(f1) # plotly_acc = tls.mpl_to_plotly(acc) # # url_roc_auc = plpl.plot(plotly_roc_auc) # url_f1 = plpl.plot(plotly_f1) # url_acc = plpl.plot(plotly_acc) url_roc_auc = plpl.plot_mpl(roc_auc, filename='roc_auc_plotly') url_f1 = plpl.plot_mpl(f1, filename='f1_plotly') url_acc = plpl.plot_mpl(acc, filename='acc_plotly')
import plotly.plotly as py import numpy as np import matplotlib.pyplot as plt py.sign_in('isak.falk', 'c5db1lskr9') mpl_fig = plt.figure() x = np.random.randn(20) y = np.random.randn(20) plt.scatter(x, y) py.plot_mpl(mpl_fig, filename="plotly_figure")