def draw_freq_plot(values): x1 = values[0][0] y1 = values[0][1] x2 = values[1][0] y2 = values[1][1] x3 = values[2][0] y3 = values[2][1] x4 = values[3][0] y4 = values[3][1] secs1 = mdate.epoch2num(x1) secs2 = mdate.epoch2num(x2) secs3 = mdate.epoch2num(x3) secs4 = mdate.epoch2num(x4) datas = ((secs1, y1), (secs2, y2), (secs3, y3), (secs4, y4)) colors = ("red", "blue", "green", "orange") groups = ("call income", "call outcome", "messages from", "messages to") fig, ax = plt.subplots() for data, color, group in zip(datas, colors, groups): x, y = data ax.plot_date(x, y, alpha=0.8, c=color, label=group) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.grid(color='grey', linestyle='-', linewidth=0.25, alpha=0.5) ax.legend(groups) date_fmt = '%Y-%m-%d %I:%M:%S%p' date_formatter = mdate.DateFormatter(date_fmt) ax.xaxis.set_major_formatter(date_formatter) fig.autofmt_xdate() plt.title('Call and Message frequency') plt.xlabel('date') plt.ylabel('numbers') plt.show()
def plotRawVersusInterpolated(oldtimes, oldvalues, newtimes, newvalues, title): """ Plot a comparison of raw values and times versus interpolated values and times. Plots raw values and times with a red line. Plots interpolated values and times with blue crosses. Parameters ---------- oldtimes : NumPy array of times (seconds since epoch) oldvalues : NumPy array of values newtimes : NumPy array of times (seconds since epoch) newvalues : NumPy array of interpolated values tite : String, the plot title. """ # Date format is "year:month:day hour:minute:second" formatter = DateFormatter("%y:%m:%d %H:%M:%S") fig, ax = plt.subplots() ax.plot(mdate.epoch2num(np.asarray(oldtimes)), np.asarray(oldvalues), 'r-', mdate.epoch2num(newtimes), newvalues, 'b*') ax.xaxis.set_major_formatter(formatter) plt.title(title) fig.canvas.set_window_title(title) # Create the plot legend red_patch = mpatches.Patch(color='red', label='Raw Data') blue_patch = mpatches.Patch(color='blue', label='Interpolated Data') plt.legend(handles=[red_patch, blue_patch]) plt.show()
def extract_daily_interaction(interaction): tweets_per_day = defaultdict(int) days = [mdates.epoch2num(long(el - el%SEC_IN_DAY)) for el in interaction] days = set(days) for day in days: tweets_per_day[day] = sum(1 for el in interaction if mdates.epoch2num(long(el - el%SEC_IN_DAY)) == day) return tweets_per_day
def set_signals(self, allSignals): timestamp = time.time() if timestamp - self._timestamp > self._delayDraw: t1 = time.time() self._timestamp = timestamp height = SAMPLE_RATE / BINS height /= 1e6 self._axes.set_color_cycle(None) self.__clear_plots() for freq, signals in allSignals: barsX = [] for start, end, _level in signals: tStart = epoch2num(start) tEnd = epoch2num(end) barsX.append([tStart, tEnd - tStart]) colour = self._axes._get_lines.color_cycle.next() self._axes.broken_barh(barsX, [freq - height / 2, height], color=colour, gid='plot') self._axes.axhspan(freq, freq, color=colour) self._axes.get_figure().autofmt_xdate() self._axes.relim() self._canvas.draw() delay = time.time() - t1 self._delayDraw += delay * 2. self._delayDraw /= 2. if self._delayDraw < 1. / MAX_TIMELINE_FPS: self._delayDraw = 1. / MAX_TIMELINE_FPS
def animate(i): btcusd = (asyncio.get_event_loop().run_until_complete(ccxt.bittrex().fetch_ticker('BTC/USDT'))) print(btcusd) print(btcusd['bid']) print(btcusd['ask']) # try: # startdate # except NameError: # startdate = mdate.epoch2num(btcusd['timestamp'] / 1000) xaxis = mdate.epoch2num(btcusd['timestamp'] / 1000) yaxis = btcusd['bid'] # x = datetime.datetime.fromtimestamp(btcusd['timestamp'].astype(str)) ######ax1.clear() #ax1.set_xticklabels(xs) #ax1.axis([0, 20, 0, 20]) ##ax1.plot_date(x, btcusd['bid']) #####ax1.plot_date(x = xaxis, y = yaxis, lw=1, linestyle = 'solid') #fmt = '.r-') #, linestyle='solid', marker='None') #, fmt='-', linewidth=2) ax1.plot_date(xaxis, yaxis, ls='-', marker='.') ##ax1.yaxis.set_major_formatter(FormatStrFormatter('%.8f')) plt.axes().tick_params(labelsize=6) plt.axes().yaxis.set_major_formatter(FormatStrFormatter('%.2f')) ### ETH/BTC plt.axes().yaxis.set_major_formatter(FormatStrFormatter('%.8f')) plt.axes().xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S')) plt.axes().xaxis_date() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='center') #ax1.plot(btcusd['bid']) plt.xlim(mdate.epoch2num((btcusd['timestamp'] / 1000) - 300), mdate.epoch2num((btcusd['timestamp'] / 1000) + 300)) ##plt.xlim(startdate, datetime.date.today()) # ETC/BTC plt.ylim(btcusd['bid'] - 0.0000001, btcusd['bid'] + 0.0000001) plt.ylim(btcusd['bid'] - 50, btcusd['bid'] + 50)
def try_to_draw_vms_cache_refresh_lines(): if(isNetflixInternal()): try: fp = open(vmsGCReportDirectory + os.path.sep + 'vms-cache-refresh-overall-events-milliseconds') except IOError: return for line in fp: line = line.rstrip('\r\n') try: (finish_time_ms_str, duration_ms_str) = line.split() except ValueError: continue finish_time_ms = long(finish_time_ms_str) duration_ms = long(duration_ms_str) start_time_ms = finish_time_ms - duration_ms start_time_secs = start_time_ms/1000.0 start_time_days = mdates.epoch2num(start_time_secs) start_time_line = lines.Line2D([start_time_days,start_time_days], [0,maxGCEventDuration], color='r') ax.add_line(start_time_line) finish_time_secs = finish_time_ms/1000.0 finish_time_days = mdates.epoch2num(finish_time_secs) finish_time_line = lines.Line2D([finish_time_days,finish_time_days], [0,maxGCEventDuration], color='c') ax.add_line(finish_time_line) fp.close() # draw some fake lines just to get them into the legend fake_vms_start_line = lines.Line2D([jvmBootDays,0], [jvmBootDays,0], label='VMS cache refresh start', color='r') fake_vms_end_line = lines.Line2D([jvmBootDays,0], [jvmBootDays,0], label='VMS cache refresh end', color='c') ax.add_line(fake_vms_start_line) ax.add_line(fake_vms_end_line)
def Print_Packet_Details(decoded,SrcPort,DstPort,ts2): if timestamp: ts = '[%f] ' % time.time() else: ts = '' #print decoded['data'] if "RCPT TO:" in decoded['data']: try: mail_try[decoded['source_address'],int(mp.epoch2num(ts2))] += 1 except: mail_try[decoded['source_address'],int(mp.epoch2num(ts2))] = 1 try: match = re.search(r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+", decoded['data']) return '%sprotocol: %s %s:%s > %s:%s %s RCPT TO: %s' % (ts, protocols[decoded['protocol']],decoded['source_address'],SrcPort,decoded['destination_address'], DstPort, str(datetime.datetime.utcfromtimestamp(ts2)), match.group()) except: return '%s%s:%s > %s:%s %s RCPT TO: %s' % (ts,decoded['source_address'],SrcPort,decoded['destination_address'], DstPort, str(datetime.datetime.utcfromtimestamp(ts2)), match.group()) if "MAIL FROM:" in decoded['data']: try: match = re.search(r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+", decoded['data']) return '%sprotocol: %s %s:%s > %s:%s %s MAIL FROM: %s' % (ts, protocols[decoded['protocol']],decoded['source_address'],SrcPort,decoded['destination_address'], DstPort, str(datetime.datetime.utcfromtimestamp(ts2)), match.group()) except: return '%s%s:%s > %s:%s %s MAIL FROM: %s' % (ts,decoded['source_address'],SrcPort,decoded['destination_address'], DstPort, str(datetime.datetime.utcfromtimestamp(ts2)), match.group()) if "From: " in decoded['data']: try: match = re.findall(r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+", decoded['data']) return '%sprotocol: %s %s:%s > %s:%s %s mail body from / to: %s - %s' % (ts, protocols[decoded['protocol']],decoded['source_address'],SrcPort,decoded['destination_address'], DstPort, str(datetime.datetime.utcfromtimestamp(ts2)), match[0], match[1]) except: return '%s%s:%s > %s:%s %s mail body from / to: %s - %s' % (ts,decoded['source_address'],SrcPort,decoded['destination_address'], DstPort, str(datetime.datetime.utcfromtimestamp(ts2)), match[0], match[1]) return " "
def plot_dataset(self, name=None, ax=None): """Function to plot data for a specified Data Center.""" values = [] times = [] max_value = 0 max_value_time = 0 for record in self.dataset[:]: if record[0] == name: times.append(dt.epoch2num(record[1])) values.append(record[2]) if record[2] > max_value: max_value = record[2] max_value_time = dt.epoch2num(record[1]) self.dataset.remove(record) else: pass value_mean = sum(values) / float(len(values)) print( "Data Center:{}\nMax Value:{}, {}\nAverage Value:{}\n".format( name, max_value, dt.num2date(max_value_time), value_mean ) ) ax.plot_date(times, values, xdate=True)
def plot_two_graphs_hod(): utc, rate_measure, rate_estimate, hexagons = np.loadtxt(file_hod_1, unpack=True) utc1, rate_measure1, rate_estimate1, hexagons1 = np.loadtxt(file_hod_2, unpack=True) rate_measure1, rate_estimate1 = 2*rate_measure1, 2*rate_estimate1, # fig, ax = plt.subplots(2, sharex=True) fig = plt.figure() ax1 = plt.subplot2grid((2,1), (0,0)) ax2 = plt.subplot2grid((2,1), (1,0)) # plt.show() ax1.set_xticks([]) # ax1.set_xlim(-0.5,23.5) # ax2.set_xticks(np.arange(0,27,2)) ax1.set_yticks(np.arange(0,1,0.02)) ax2.set_yticks(np.arange(0,1,0.006)) # ax2.set_xlim(-0.5,23.5) color="indianred" ax1.scatter(num2date(epoch2num(utc)), rate_measure/(hexagons*factor) , label="Energía > 1 EeV", marker="o", s=1.5, color=color, alpha=0.8) ax1.fill_between(num2date(epoch2num(utc)), rate_measure/(hexagons*factor) - np.sqrt(rate_measure)/(hexagons*factor), rate_measure/(hexagons*factor) + np.sqrt(rate_measure)/(hexagons*factor), color=color, alpha=0.2) color1="green" ax2.scatter(num2date(epoch2num(utc1)), rate_measure1/(hexagons1*factor) , label="Energía > 2 EeV", marker="o", s=1.5, color=color1, alpha=0.8) ax2.fill_between(num2date(epoch2num(utc1)), rate_measure1/(hexagons1*factor) - np.sqrt(rate_measure1)/(hexagons1*factor), rate_measure1/(hexagons1*factor) + np.sqrt(rate_measure1)/(hexagons1*factor), color=color1, alpha=0.2) ax1.set_ylabel("Tasa [km$^{-2}$día$^{-1}$]") ax2.set_ylabel("Tasa [km$^{-2}$día$^{-1}$]") ax2.set_xlabel("Fecha") ax1.legend(fontsize=18) ax2.legend(fontsize=18) ax1.xaxis.set_major_locator(months) # # ax.xaxis.set_major_formatter(monthsFmt) # ax1.autoscale_view() ax2.xaxis.set_major_locator(months) # ax.xaxis.set_major_formatter(monthsFmt) ax2.autoscale_view() plt.show()
def test_epoch2num(): mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') assert mdates.epoch2num(86400) == 719164.0 assert mdates.num2epoch(719165.0) == 86400 * 2 # set back to the default mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T00:00:00') assert mdates.epoch2num(86400) == 1.0 assert mdates.num2epoch(2.0) == 86400 * 2
def trades( timeSince ): # gets the innermost bid and asks and information on the most recent trade. adjustedTime = int(time.time()) - timeSince response = requests.get(URL + str(adjustedTime)) splitResponse = response.text.splitlines() prices = [] timestamps = [] amounts = [] mymax = 0 #Only keep one of each 30 lines splitResponse = splitResponse[::30] for i, line in enumerate(splitResponse): splitline = splitResponse[i].split(',') timestamp = splitline[0] price = round(float(splitline[1]), 2) amount = splitline[2] #print "amount: " + str(amount) if mymax < amount: mymax = amount timestamps.append(float(timestamp)) print "\nEvent at time: ", mdates.epoch2num(float(timestamp)) prices.append(float(price)) amounts.append(float(amount) * 5) fig = plt.figure() #ax1 = plt.subplot(2,1,1) ax1 = plt.subplot2grid((5, 4), (0, 0), rowspan=4, colspan=4) secs = mdates.epoch2num(timestamps) ax1.plot_date(secs, prices, 'k-', linewidth=.7) ax1.grid(True) plt.xlabel('Date') plt.ylabel('Bitcoin Price') #ax2 = plt.subplot(2,1,2, sharex=ax1) ax2 = plt.subplot2grid((5, 4), (4, 0), sharex=ax1, rowspan=1, colspan=4) ax2.plot(secs, amounts) ax2.grid(True) plt.ylabel('Volume') #Use a DateFormatter to set the data to the correct format. #Choose your xtick format string #date_fmt = '%d-%m-%y %H:%M:%S' date_fmt = '%d-%m-%y %H:%M' date_formatter = mdates.DateFormatter(date_fmt) ax1.xaxis.set_major_formatter(date_formatter) #Tilt x-axis text to fit fig.autofmt_xdate() plt.show()
def test_epoch2num(): with _api.suppress_matplotlib_deprecation_warning(): mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') assert mdates.epoch2num(86400) == 719164.0 assert mdates.num2epoch(719165.0) == 86400 * 2 # set back to the default mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T00:00:00') assert mdates.epoch2num(86400) == 1.0 assert mdates.num2epoch(2.0) == 86400 * 2
def trades(timeSince): # gets the innermost bid and asks and information on the most recent trade. adjustedTime = int(time.time()) - timeSince response = requests.get(URL + str(adjustedTime)) splitResponse = response.text.splitlines() prices = [] timestamps = [] amounts = [] mymax = 0 #Only keep one of each 30 lines splitResponse = splitResponse[::30] for i,line in enumerate(splitResponse): splitline = splitResponse[i].split(',') timestamp = splitline[0] price = round(float(splitline[1]),2) amount = splitline[2] #print "amount: " + str(amount) if mymax < amount: mymax = amount timestamps.append(float(timestamp)) print "\nEvent at time: ",mdates.epoch2num(float(timestamp)) prices.append(float(price)) amounts.append(float(amount)*5 ) fig = plt.figure() #ax1 = plt.subplot(2,1,1) ax1 = plt.subplot2grid((5,4), (0,0), rowspan=4, colspan=4) secs = mdates.epoch2num(timestamps) ax1.plot_date(secs, prices, 'k-', linewidth=.7) ax1.grid(True) plt.xlabel('Date') plt.ylabel('Bitcoin Price') #ax2 = plt.subplot(2,1,2, sharex=ax1) ax2 = plt.subplot2grid((5,4), (4,0), sharex=ax1, rowspan=1, colspan=4) ax2.plot(secs, amounts) ax2.grid(True) plt.ylabel('Volume') #Use a DateFormatter to set the data to the correct format. #Choose your xtick format string #date_fmt = '%d-%m-%y %H:%M:%S' date_fmt = '%d-%m-%y %H:%M' date_formatter = mdates.DateFormatter(date_fmt) ax1.xaxis.set_major_formatter(date_formatter) #Tilt x-axis text to fit fig.autofmt_xdate() plt.show()
def info(self): # General info of the data f_name = self.filename st = self.data['ST'] et = self.data['ET'] d_format = '%H:%M:%S' # start date and time formatting s_day = epoch2num(st) # num of days since epoch s_date = datetime.date.fromordinal(s_day) # returns formatted date YYYY-MM-DD s_time = time.gmtime(st) # returns struct_time of date and time values e_time = time.gmtime(et) # Range in seconds st_sec = s_time.tm_hour * 3600 + s_time.tm_min * 60 + s_time.tm_sec et_sec = e_time.tm_hour * 3600 + e_time.tm_min * 60 + e_time.tm_sec dsec = et_sec - st_sec start_str = time.strftime(d_format, s_time) end_str = time.strftime(d_format, e_time) info_d = {'Filename': f_name, 'Date': s_date, 'Start': start_str, 'End': end_str, 'dt': dsec} ''' print('Filename: ', f_name) print('Date: ', s_date, ', epoch[day]: ', s_day[0]) print('GMT:') print('Start Time : ', time.strftime(d_format, s_time), ', End Time: ', time.strftime(d_format, e_time)) ''' return info_d
def histogram_data(self, resolution = 'month', combined = True, start = None, stop = None): s_date = start if start else self.start_date() e_date = stop if stop else self.end_date() s, e = self.search_date_range(s_date, e_date) flat_messages = {} if combined: flat_messages = {'Combined':[timestamp(x[0].date()) for x in self._sms_history[s:e]]} else: for participant in self.participants + ['Me']: flat_messages[participant] = [timestamp(x[0].date()) for x in self._sms_history[s:e] if x[1][1] == participant] plt_data = [] for history in flat_messages.items(): label = history[0] mpl_data = mdates.epoch2num(history[1]) plt_data += [(label, mpl_data)] elapsed_time = e_date - s_date if resolution == 'day': num_buckets = elapsed_time.days elif resolution == 'year': num_buckets = int(elapsed_time.days / 365) + 1 else: num_buckets = int(elapsed_time.days / 30) + 1 data_sets = [] for person in plt_data: y, bin_edges = np.histogram(person[1], bins = num_buckets) bin_centers = 0.5*(bin_edges[1:]+bin_edges[:-1]) data_sets.append((person[0], y, bin_centers)) return data_sets
def graphit(newx, newy): # Enlarge font font = {'family': 'normal', 'weight': 'bold', 'size': 22} matplotlib.rc('font', **font) # Standard graph fig = plt.figure() ax = fig.add_subplot(111) secs = mdate.epoch2num(newx) ax.plot_date(secs, newy, 'r-') date_fmt = '%d-%m-%y %H:%M:%S' date_formatter = mdate.DateFormatter(date_fmt) ax.xaxis.set_major_formatter(date_formatter) fig.autofmt_xdate() # Filtered graph fig2 = plt.figure() ax2 = fig2.add_subplot(111) yhat = signal.savgol_filter(newy, 11, 3) # window size 31, polynomial order 3 ax2.plot_date(secs, yhat, 'b-') ax2.xaxis.set_major_formatter(date_formatter) fig2.autofmt_xdate() plt.show()
def jsonformat(filename): data = json.load(open(filename)) df = pd.DataFrame(columns=('Vehicle-Type', 'Speed', 'Vehicle Count', 'Time Stamp', 'Hour')) index = 0 for events, subdict in data.items(): print(events) for vehicles in itertools.chain(subdict): vtype = getFromDict(vehicles, ['properties', 'vehicle-type']) speed = getFromDict(vehicles, ['measures', 1, 'value']) vcount = getFromDict(vehicles, ['measures', 2, 'value']) #timestamp = datetime.datetime.fromtimestamp(getFromDict(vehicles,['timestamp'])/1000.0).strptime('%H') timestamp = mdate.epoch2num( getFromDict(vehicles, ['timestamp']) / 1000.0) hour = datetime.datetime.fromtimestamp( getFromDict(vehicles, ['timestamp']) / 1000.0).strftime('%H') #print (hour) index += 1 df.loc[index] = [vtype, speed, vcount, timestamp, int(hour)] #print(df.loc[index]) if (index > 3000): break return df
def plotBLMtime(t01, t02, col, BLM, label): tt_TCP, vv_TCP, tt_BLM, vv_BLM = getTimberData(t01, t02, col, BLM) plotBLMtime = plt.plot_date(epoch2num(tt_BLM + 2 * 3600), vv_BLM, '-', label=label) return plotBLMtime
def candlestick_chart(self, time_series, interval=40, show=False, filename='candlestick.png', volume_overlay=None): tz = get_localzone() adjusted_time_series = [] for item in time_series: item[0] = epoch2num(item[0]) adjusted_time_series.append(item) fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) ax.xaxis.set_major_formatter(self._date_formatter(interval, tz=tz)) days_interval = interval / 86400.0 candlestick_ochl(ax, adjusted_time_series, width=(days_interval), colorup='green', colordown='red', alpha=0.9) ax.xaxis_date(tz=tz.zone) ax.autoscale_view() yticks = ax.get_yticks() x_start = min(yticks) - ((max(yticks) - min(yticks)) * 0.60) plt.ylim([x_start,max(yticks)]) ax.grid(True) plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') if volume_overlay != None: # Add a seconds axis for the volume overlay ax2 = ax.twinx() yticks = ax.get_yticks() print('yticks', yticks) # set the position of ax2 so that it is short (y2=0.32) but otherwise the same size as ax ax2.set_position(matplotlib.transforms.Bbox([[0.125,0.2],[0.9,0.42]])) #print(days_interval * len(adjusted_time_series)) #ax2.set_position([0.125, 0.2, 0.8, 0.2]) # get data from candlesticks for a bar plot dates = [x[0] for x in adjusted_time_series] dates = np.asarray(dates) volume = [x[1] for x in volume_overlay] volume = np.asarray(volume) ax2.bar(dates,volume,color='#aaaaaa',width=(days_interval),align='center',linewidth=0.0,alpha=0.8) #scale the x-axis tight #ax2.set_xlim(min(dates),max(dates)) # the y-ticks for the bar were too dense, keep only every third one ax2yticks = ax2.get_yticks() #print('yticks', ax2yticks) #print('yticks2', ax2yticks[::3]) ax2.set_yticks(ax2yticks[::3]) ax2.yaxis.set_label_position("right") ax2.set_ylabel('Volume', size=20) # format the x-ticks with a human-readable date. #xt = ax.get_xticks() #new_xticks = [datetime.date.isoformat(num2date(d)) for d in xt] #ax.set_xticklabels(new_xticks,rotation=45, horizontalalignment='right') if show: plt.show() else: plt.savefig(const.DATA_DIR + '/' + filename, bbox_inches='tight') plt.close()
def extract(self, device, type): array = [] # use max(x, 0) to prevent native numbers. This could happen when the number overflow 32bit integer if type == "t": # t0 = 0 # for i in range(0, len(self.traffic[device]["hist"])): # t0 = t0 + self.traffic[device]["hist"][i][0] # array.append(t0) t0 = self.traffic[device]["last"][0] for i in range(len(self.traffic[device]["hist"]), 0, -1): x = mdates.epoch2num(t0) array.append(x) t0 -= self.traffic[device]["hist"][i-1][0] array.reverse() elif type == "rxbytes": for i in range(0, len(self.traffic[device]["hist"])): array.append(max(self.traffic[device]["hist"][i][1] / 1024, 0)) elif type == "txbytes": for i in range(0, len(self.traffic[device]["hist"])): array.append(max(self.traffic[device]["hist"][i][2] / 1024, 0)) elif type == "rxpkts": for i in range(0, len(self.traffic[device]["hist"])): array.append(max(self.traffic[device]["hist"][i][3], 0)) elif type == "txpkts": for i in range(0, len(self.traffic[device]["hist"])): array.append(max(self.traffic[device]["hist"][i][4], 0)) return array
def doplot(series): """ Do the actual plotting of the series Needs data with the given dtypes to be able to address the 'date', 'humidity', 'temperature' columns """ fig = pl.figure(figsize=(11.27, 8.69)) dates = [num2date(epoch2num(s[0]['date']), tz) for s in series] ax1 = fig.add_subplot(211) for i, s in enumerate(series): ax1.plot_date(dates[i], s[0]['humidity'], '-', label=s[1]) ax1.set_ylabel("Humidity (%)") ax1.legend(loc='best') fig.autofmt_xdate() ax2 = fig.add_subplot(212) signs = ['s', 'x', 'o'] for i, s in enumerate(series): ax2.plot_date(dates[i], s[0]['temperature'], signs[i % len(signs)], label=s[1]) ax2.set_ylabel("Temperature (C)") ax2.legend(loc='best') fig.autofmt_xdate() ax1.set_title("%s -> %s" %(dates[0][0], dates[0][-1]))
def convert_chandra_time(rawtimes): """ Convert input CXC time (sec) to the time base required for the matplotlib plot_date function (days since start of the Year 1 A.D - yes, really). :param times: iterable list of times, in units of CXCsec (sec since 1998.0) :rtype: plot_date times (days since Year 1 A.D.) """ # rawtimes is in units of CXC seconds, or seconds since 1998.0 # Compute the Delta T between 1998.0 (CXC's Epoch) and 1970.0 (Unix Epoch) seconds_since_1998_0 = rawtimes[0] cxctime = dt.datetime(1998, 1, 1, 0, 0, 0) unixtime = dt.datetime(1970, 1, 1, 0, 0, 0) # Calculate the first offset from 1970.0, needed by matplotlib's plotdate # The below is equivalent (within a few tens of seconds) to the command # t0 = Chandra.Time.DateTime(times[0]).unix delta_time = (cxctime - unixtime).total_seconds() + seconds_since_1998_0 plotdate_start = epoch2num(delta_time) # Now we use a relative offset from plotdate_start # the number 86,400 below is the number of seconds in a UTC day chandratime = (np.asarray(rawtimes) - rawtimes[0]) / 86400. + plotdate_start return chandratime
def main(): db_con = sqlite3.connect('databases/weather_data.db') db_cur = db_con.cursor() db_cur.execute('SELECT Temperature, Humidity FROM data') data = np.array(db_cur.fetchall()) db_cur.execute('SELECT Timestamp FROM data') timestamps = np.array(db_cur.fetchall()) timestamps += 3600 + 3600 db_con.close() mplTimestamps = mdate.epoch2num(timestamps) fig, (ax1, ax2) = plt.subplots(1, 2) ax1.plot_date(mplTimestamps, data[:, 0], '-') date_format = '%H:%M:%S' date_formatter = mdate.DateFormatter(date_format) ax1.xaxis.set_major_formatter(date_formatter) ax1.set_ylim([10.0, 30.0]) ax2.plot_date(mplTimestamps, data[:, 1], '-') ax2.xaxis.set_major_formatter(date_formatter) ax2.set_ylim([20, 60]) fig.autofmt_xdate() ax1.grid(True) ax2.grid(True) plt.show()
def plot_temp_curve(self, data_tab): x_array = [] y_array = [] for data in data_tab: x_array.append(data[0]) y_array.append(data[1]) if not x_array or not y_array: self.log.error("At least 1 tab is empty...") return 0 if len(x_array) != len(y_array): self.log.error("axis has not the same size") return 0 x_array = mat_dates.epoch2num(x_array) fig, ax = plt.subplots() ax.plot(x_array, y_array) myFmt = mat_dates.DateFormatter("%m/%d %H:%M") ax.xaxis.set_major_formatter(myFmt) # Rotate date labels automatically fig.autofmt_xdate() plt.show()
def plot_nature_run_values(self, cycle, metric_name, label=BEST_TRACK_LABEL): ''' Plot the Truth values for the given `metric_name', using the corresponding absolute times for `cycle'. The time axis values will not really correspond to the forecast times of the Nature Run, so just create an array using self.cfg.forecast_duration and self.cfg.history_interval ''' log.debug('plotting nature run data') epoch2num = lambda x: mpl_dates.num2date(mpl_dates.epoch2num(x)) natureTimesDict = self.cfg.truth_track.absolute_times_dict() endFhr = int(self.cfg.forecast_duration) interval = int(self.cfg.history_interval) fhrs = range(0, endFhr, interval) nature_data = [] #self.cfg.truth_track.tracker_entries for outputTime in range(cycle, cycle + endFhr, interval): nature_data.append(natureTimesDict[outputTime]) if self.cfg.time_axis_parameter == 'epoch_zeta': nature_time_vals = [epoch2num(cycle + fhr) for fhr in fhrs] # This is causing breakage ever since I switched to UTC elif self.cfg.time_axis_parameter == 'fhr': nature_time_vals = [t / 3600 for t in fhrs] nature_vals = [getattr(x, metric_name) for x in nature_data] #import pdb ; pdb.set_trace() self.line_plot_wrapper(nature_time_vals, nature_vals, color=self.cfg.nature_line_color, linestyle=self.cfg.nature_line_style, label=label)
def animate(i): data, time = readHistoricalData(dataFile) lastPrices = utils.readBinaryData("data/lastPrices.dat") lastValues = utils.readBinaryData("data/lastValues.dat") lastProfits = utils.readBinaryData("data/lastProfits.dat") hourAgo = EpochPST.getHourAgo() validPrices = 0 for i in range(len(time)-1,-1,-1): if(time[i] > hourAgo): validPrices += 1 else: break data = data[-validPrices:] time = time[-validPrices:] secs = mdate.epoch2num(time) axis.clear() date_fmt = '%m/%d %H:%M' date_formatter = mdate.DateFormatter(date_fmt) axis.xaxis.set_major_formatter(date_formatter) axis.yaxis.set_major_formatter('${x:1.0f}') fig.autofmt_xdate() axis.plot(secs,data) plt.legend(handles=makeLegend(lastPrices, lastValues, lastProfits)) totalValue = 0 totalProfit = 0 for value,key in lastValues.items(): totalValue = totalValue + key for value,key in lastProfits.items(): totalProfit = totalProfit + key plt.title("Total Portfolio Value: \$" + str(round(totalValue,2)) + " ($" + str(round(totalProfit,2)) + ")")
def plotTemperature(secondsToPlot, data): e = np.array(data['epoch']) last = e[-1] start = last - secondsToPlot idx = e > start minTemp = np.array(data['min'])[idx] meanTemp = np.array(data['mean'])[idx] maxTemp = np.array(data['max'])[idx] dt = mdates.epoch2num(e) plt.plot_date(dt, meanTemp, xdate=True, linestyle='-', marker='.', label='mean') plt.plot_date(dt, minTemp, xdate=True, linestyle='-', marker='.', label='max') plt.plot_date(dt, maxTemp, xdate=True, linestyle='-', marker='.', label='min') plt.grid() plt.title('Last weeks temperature') plt.xlabel('Date') plt.ylabel('Temperature [degF]') plt.legend() plt.savefig('lastweek.png', dpi=600)
def plot_summoners_data(summoners_data, queue_type, data): fig1, ax1 = plt.subplots() ax1.set_xlabel('Zeit') ax1.set_ylabel(data) ax1.set_title(queue_type) if data == "Rang": ax1.set_yticks(list(range(0, 2300, 400))) ax1.set_yticks(list(range(0, 2300, 100)), minor=True) ranks_string = [ "Eisen 4", "Bronze 4", "Silber 4", "Gold 4", "Platin 4", "Diamant 4" ] ax1.set_yticklabels(ranks_string) ax1.grid(axis='y') for summoner in summoners_data: # TODO Timezone is false x_data = [ mdates.epoch2num(time) for time in summoners_data[summoner][queue_type]['date_time'] ] ax1.plot_date(x_data, list(summoners_data[summoner][queue_type][data]), label=summoner, ls='-') ax1.legend() return fig1, ax1
def timeDataPlot(timestamp, data): dateFormat = formatMakeTimestamp(timestamp) ax1 = plt.subplot2grid((1, 1), (0, 0)) timestamp = mdates.epoch2num(timestamp) ax1.xaxis.set_major_formatter(mdates.DateFormatter(dateFormat)) ax1.plot(timestamp, data) plt.show()
def ohclvForPlots(ohlcv): timestamp = Up.getTimestamp(ohlcv) timestamp = mdates.epoch2num(timestamp) size = len(ohlcv) for i in range(0, size): ohlcv[i][0] = timestamp[i] return ohlcv
def interploateMetrics(ts, metrics, kind='quadratic'): ''' scipy.interploate.interp1d를 이용한 데이타 보간 ts, metrics: orignal data which need to interpolate (timestamp, metrics) kind: kind of interpolation return: interpolated timestamp for mpl, metrics 데이터 보간함수는 object array를 데이터(여기서는 datatime list)기반이 아니므로, timestamp list를 float list로 변환해서 보간함수르 구한다. ''' # interp1d func가 argument로 object array를 받지 않기 때문에 # datetime.datetime 유형을 float형으로 변환(Unix time: 1970.01.01 이후의 누적 초) unix_ts = [t.timestamp() for t in ts] # interpolation(보간)을 하기 위해 time interval을 조정, ex) interaval 10secs ts_new = [t for t in np.arange(min(unix_ts), max(unix_ts) + 1, 5.0)] # 보간 된 위 ts_new를 mpl datetime float format으로 변환(기준 변경 : 1970.1.1 -> 0001.1.1 UTC) # mpl datetime float format을 mpl datetime으로 변환 # mpl은 기본적으로 plotting을 하기 위해 datetime을 형식의 data를 float으로 변환한다. mpl_ts_new = [ mdt.num2date(mdt.epoch2num(t)) for t in np.arange(min(unix_ts), max(unix_ts) + 1, 5.0) ] # 기존 데이터를 이용한 interpolation function을 구한다 f = interp1d(unix_ts, metrics, kind=kind) # 위 보간함수를 이용하여 새 데이터에 대한 함수 값을 구한다. metrics_new = f(ts_new) return mpl_ts_new, metrics_new
def graph_animate(i): read_file = open('mcxlist.txt', 'r') sep_file = read_file.read().split('\n') x = [] y = [] for pair in sep_file: XY = pair.split(' ') if len(XY) > 1: x.append(float(XY[0])) y.append(float(XY[1])) ax1.clear() x = np.array(x) y = np.array(y) read_file.close() #rotates timestamps plt.setp(plt.xticks()[1], rotation=30) #shows grid ax1.grid(b=True, which='major', color='r') plt.title('CFS Betagraph V0.0.1', color='w') #plt x, and y cords ax1.plot_date(md.epoch2num(x), y,'r',tz=est,linewidth=2,xdate=True,marker='o')
def joinedHist(members): fig, ax = plt.subplots(1, 1, figsize=(12, 6), dpi=100) joined_dates = np.array( [int(member['joined']) / 1000 for member in members]) start = datetime.fromtimestamp(np.amin(joined_dates)) now = datetime.now() joined_dates = mdates.epoch2num(joined_dates) months = [datetime(year=y,month=m,day=1) \ for y in range(start.year,now.year+1) \ for m in range(1,13) \ if not ((y == now.year and m > now.month+1) or \ (y == start.year and m < start.month))] bins = mdates.date2num(months) hist, _ = np.histogram(joined_dates, bins=bins) labels = [m.strftime('%b-%y') if m.month % 6 == 1 else '' for m in months] labels = np.array(labels) plt.xticks(bins, labels) ax.bar(range(len(hist)), hist, width=0.8, align='center', tick_label=labels[:-1], zorder=3) ax.yaxis.grid(which='major', linestyle='--', zorder=0) ax.set_ylabel('Members') ax.set_title('Joined MeetUp') timestamp = datetime.now().isoformat(timespec='minutes') plt.savefig( path.join('output', '{}-joined-meetup-{}.png'.format(group_urlname, timestamp)))
def key_press(self, event): if event.key in ['z', 'p'] and event.inaxes: x0, x1 = self.ax.get_xlim() dx = x1 - x0 xc = event.xdata zoom = self.zoom if event.key == 'p' else 1.0 / self.zoom new_x1 = zoom * (x1 - xc) + xc new_x0 = new_x1 - zoom * dx tstart = max(num2epoch(new_x0), MIN_TSTART_UNIX) tstop = min(num2epoch(new_x1), MAX_TSTOP_UNIX) new_x0 = epoch2num(tstart) new_x1 = epoch2num(tstop) self.ax.set_xlim(new_x0, new_x1) self.ax.figure.canvas.draw_idle() elif event.key == 'm': for _ in range(len(self.ax.lines)): self.ax.lines.pop() self.plot_mins = not self.plot_mins print('\nPlotting mins and maxes is {}'.format( 'enabled' if self.plot_mins else 'disabled')) self.draw_plot() elif event.key == 'a': # self.fig.clf() # self.ax = self.fig.gca() self.ax.set_autoscale_on(True) self.draw_plot() self.ax.set_autoscale_on(False) self.xlim_changed(None) elif event.key == 'y': self.scaley = not self.scaley print('Autoscaling y axis is {}'.format( 'enabled' if self.scaley else 'disabled')) self.draw_plot() elif event.key == '?': print(""" Interactive MSID plot keys: a: autoscale for full data range in x and y m: toggle plotting of min/max values p: pan at cursor x y: toggle autoscaling of y-axis z: zoom at cursor x ?: print help """)
def manip_info(sessionName, quiet, line_to_print, var_to_plot): """ This function prints information about a session, and optionally plot specified variables. It can be accessed from the CLI tool ManipInfo """ if os.path.exists(sessionName + ".db"): SavedAsyncSession(sessionName).print_description() return if sessionName.endswith(".hdf5"): N = len(sessionName) sessionName = sessionName[0:(N - 5)] MI = Manip(sessionName).MI if line_to_print is not None: if line_to_print >= len(MI.log("t")): print("Specified line is out of bound.") sys.exit(1) format_str = "{:>15} | {:>20}" print("Printing saved values on line", line_to_print) print(format_str.format("Variable", "Value")) varlist = ["Time"] varlist += MI.log_variable_list() print("-" * 38) for varname in varlist: valtab = MI.log(varname) if isinstance(valtab, (float, int)): # might occur if only one line print(format_str.format(varname, MI.log(varname))) else: print( format_str.format(varname, MI.log(varname)[line_to_print])) elif not quiet: MI.describe() if var_to_plot is not None: if var_to_plot in MI.log_variable_list(): t = epoch2num(MI.log("t")) vardata = MI.log(var_to_plot) fig = plt.figure() xtick_locator = AutoDateLocator() xtick_formatter = AutoDateFormatter(xtick_locator) ax = plt.axes() ax.xaxis.set_major_locator(xtick_locator) ax.xaxis.set_major_formatter(xtick_formatter) ax.plot(t, vardata, "o-") plt.setp(ax.xaxis.get_majorticklabels(), rotation=70) fig.subplots_adjust(bottom=0.2) plt.ylabel(var_to_plot) plt.title(sessionName) plt.show() else: print("Variable", var_to_plot, "does not exist!") sys.exit(1)
def get_datetime_hist_data(dt_list, bandwidth_days=1, bins=None): mpl_data = mdates.epoch2num([dt.timestamp() for dt in dt_list]) # consider having the same bins for all the timeseries! if bins is None: bins = range(int(min(mpl_data)), int(max(mpl_data)) + 1 + bandwidth_days, bandwidth_days) hist, bin_edges = np.histogram(mpl_data, bins=bins) bincenters = 0.5 * (bin_edges[1:] + bin_edges[:-1]) return bincenters, hist
def flow_variability_plot(extracted_data, fig_index, hist_xlim): """ plot the time series and histogram of the flow variability recorded by the flow meter. The readings are extracted with Google Cloud Vision OCR INPUT - extracted_data is the data extracted from the video of the flow meter - ax1, ax2 are the subplots to be plotted with the time seires and the histogram - hist_xlim is the limit of the x-axis of the histogram in scfh OUTPUT """ fig, [ax2, ax1] = plt.subplots(1, 2, figsize=[15, 5], gridspec_kw={'width_ratios': [1, 2]}) ax1.set_ylabel('flow rate [scfh]', fontsize=13) ax2.set_ylabel('density', fontsize=13) ax2.set_xlabel('flow rate [scfh]', fontsize=13) mins = mdate.epoch2num(extracted_data.unix_time.values) ax1.plot_date(mins, extracted_data.mid_meter_flow_scfh, '-', color='#8c1515') mu, std = norm.fit( extracted_data[~np.isnan(extracted_data.mid_meter_flow_scfh)]. mid_meter_flow_scfh) ax2.hist(extracted_data.mid_meter_flow_scfh, bins=20, density=True, color='#8c1515', alpha=0.3) xmin, xmax = extracted_data.mid_meter_flow_scfh.min( ), extracted_data.mid_meter_flow_scfh.max() x = np.linspace(xmin, xmax, 100) p = norm.pdf(x, mu, std) ax2.plot(x, p, 'k', linewidth=2) ax2.annotate( '(' + fig_index + ') Max flow rate n = %d \nFit results: $\\mu$ = %d, $\\sigma$ = %d' % (extracted_data[~np.isnan(extracted_data.mid_meter_flow_scfh)]. shape[0], mu, std), xy=[0.02, 0.86], xycoords='axes fraction', fontsize=13) date_formatter = mdate.DateFormatter('%I:%M:%S %p') ax2.set_xlim(hist_xlim) ax1.set_ylim(hist_xlim) ax1.xaxis.set_major_formatter(date_formatter) fig.autofmt_xdate() plt.close() return fig
def saveGraph(f): if f != "null": matfirstbeat = mdates.epoch2num(firstbeat.keys()) plt.plot_date(matfirstbeat, firstbeat.values(), 'bs', linewidth=0.25, label='firstbeat heartrate') matstressapp = mdates.epoch2num(stress.keys()) matbasispeak = mdates.epoch2num(basispeak.keys()) plt.plot_date(matbasispeak, basispeak.values(), 'gs', linewidth=0.25, label='Basis Peak Heartrate') plt.plot_date(matstressapp, stress.values(), 'ro', linewidth=3.0, label='stress app annotation heartrate') plt.xlabel('Date') plt.ylabel('Heartrate') plt.title('Subject %s' % subjectnum) plt.ylim([-5, 200]) plt.legend() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m%d')) plt.gca().xaxis.set_major_locator(mdates.DayLocator()) plt.savefig('C:/Users/jfzabalx/Pictures/Stress_Study_Heartrate/{0}.png'.format(subjectnum)) plt.close()
def transform_date(self, inputdate): """Parameters: inputdate: Unix time - int representing number of seconds from Epoch Returns date in matplotlib date format, that is float number of days since 0001 """ return mdates.epoch2num(inputdate)
def graph(filename, chart=False): with open(filename) as file: content = file.read() # Parse data parsed = parse_data(content, verbose=False) print(parsed.keys()) # print(parsed['positions']) points = parsed['positions'] speeds = [] times = [] for i in range(1, len(points)): a = points[i - 1] b = points[i] dist_diff = abs(a[1] - b[1]) time_diff = b[0] - a[0] speed = dist_diff / time_diff speeds.append(speed) times.append(a[0]) # depth = 1_000 # averages = rolling_average(times, speeds, depth) parts = [] for i in range(4): start = i * (len(speeds) // 4) end = (i + 1) * (len(speeds) // 4) sub = speeds[start:end] avg = sum(sub) / len(sub) parts.append(avg) print(f'{filename} => {parts}') return parts if chart: # plt.plot(times, speeds) # plt.plot(times, averages) secs = mdate.epoch2num(times) fig, ax = plt.subplots() # ax.plot_date(secs, speeds) ax.plot_date(secs, averages, 'b-') # apply formatting # date_fmt = '%d-%m-%y %H:%M:%S' date_fmt = '%H:%M:%S' date_formatter = mdate.DateFormatter(date_fmt) ax.xaxis.set_major_formatter(date_formatter) fig.autofmt_xdate(rotation=45) # start, end = ax.get_xlim() # ax.xaxis_date() # plt.xticks(np.arange(0, len(secs)+1, 5)) plt.show()
def plot_medi(filepath): utc, rate_measure, rate_estimate, hexagons = np.loadtxt(filepath, unpack=True) # hexagons*=5 fig, ax = plt.subplots() ax.set_ylabel("Tasa [km$^{-2}$día$^{-1}$]") ax.set_xlabel("Fecha") # ax.set_ylim(ymin=0.2, ymax=0.4) ax.scatter(num2date(epoch2num(utc)), rate_measure/(hexagons*factor) , marker="o", s=1.5, color="indianred", alpha=0.8) ax.fill_between(num2date(epoch2num(utc)), rate_measure/(hexagons*factor) - np.sqrt(rate_measure)/(hexagons*factor), rate_measure/(hexagons*factor) + np.sqrt(rate_measure)/(hexagons*factor), color='indianred', alpha=0.2) ax.xaxis.set_major_locator(months) # ax.xaxis.set_major_formatter(monthsFmt) ax.autoscale_view() ax.grid(alpha=0.1)
def _dt_to_float_ordinal(dt): """ Convert :mod:`datetime` to the Gregorian date as UTC float days, preserving hours, minutes, seconds and microseconds. Return value is a :func:`float`. """ if isinstance(dt, (np.ndarray, Series)) and com.is_datetime64_ns_dtype(dt): base = dates.epoch2num(dt.asi8 / 1.0E9) else: base = dates.date2num(dt) return base
def divide_fit(X, y, ax): n = len(X) X1 = X[: n/2] X2 = X[n/2:] y1 = y[: n/2] y2 = y[n/2:] lr1 = linear_model.LinearRegression() lr1.fit(X1, y1) if lr1.coef_ < 0: divide_fit(X1, y1, ax) else: X_ts_track1 = mdate.epoch2num(np.array(X1) / 1000) ax.plot_date(X_ts_track1, lr1.predict(np.array(X1).reshape(-1, 1))[:], marker='o', ms=1, c='red') lr2 = linear_model.LinearRegression() lr2.fit(X2, y2) if lr2.coef_ < 0: divide_fit(X2, y2, ax) else: X_ts_track2 = mdate.epoch2num(np.array(X2)/1000) ax.plot_date(X_ts_track2, lr2.predict(np.array(X2).reshape(-1, 1))[:], marker='o', ms=1, c='red')
def hero_per_month(player_id, hero_id): hist = Player(player_id).stat_func('matches', hero_id=hero_id) hist = hist[::-1] time = hist[0]['start_time'] quantity = [] kk = 0 m = 0 q = 0 month = [] for i in hist: if i['start_time'] < time: try: if i['hero_id'] == hero_id: q += 1 kk += 1 except: pass else: time += 2592000 quantity.append(q) month.append(time) q = 0 m += 1 plt.xkcd() plt.gca().cla() plt.title('number of games played as {} per month'.format(hero_dic[hero_id])) y = quantity secs = mdates.epoch2num(month) ax = plt.gca() years = mdates.YearLocator() # every year yearsFmt = mdates.DateFormatter('%Y') ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.yaxis.set_ticks_position('left') ax.xaxis.set_ticks_position('bottom') tick = 5 if max(y) > 20 else 1 yint = range(min(y), math.ceil(max(y))+2, tick) # set only int ticks plt.yticks(yint) plt.plot(secs, y, color='blue') plt.savefig('images/graphs/hero.png') return "{} games".format(kk)
def _plot_dateplot(self, data): """ Make the date plot """ # Rotate datemarks on xaxis self.ax1.set_xticklabels([], rotation=25, horizontalalignment='right') # Left axis for dat in data['left']: # Form legend if dat['lgs'].has_key('legend'): legend = dat['lgs']['legend'] else: legend = None # Plot if len(dat['data']) > 0: self.ax1.plot_date(mdates.epoch2num(dat['data'][:,0]), dat['data'][:,1], label=legend, xdate=True, color=self.c.get_color(), tz=self.tz, fmt='-') # Right axis for dat in data['right']: # Form legend if dat['lgs'].has_key('legend'): legend = dat['lgs']['legend'] else: legend = None # Plot if len(dat['data']) > 0: self.ax2.plot_date(mdates.epoch2num(dat['data'][:,0]), dat['data'][:,1], label=legend, xdate=True, color=self.c.get_color(), tz=self.tz, fmt='-') # No data if self.measurement_count == 0: y = 0.00032 if self.o['left_logscale'] is True else 0.5 self.ax1.text(0.5, y, 'No data', horizontalalignment='center', verticalalignment='center', color='red', size=60)
def read_dates(limit, stream=sys.stdin): """ Read newline-separated unix time from stream """ dates = [] for line in stream: num = epoch2num(float(line.strip())) dates.append(num) if num < limit: break stream.close() return dates
def plot(self, output): fig, ax = self.logplot x = dates.epoch2num(self.time_axis) ax.cla() ax.plot_date(x, self.total_duration, 'b-') ax.plot_date(x, self.total_duration_average, 'r-') ax.xaxis.set_major_formatter(self.dateformatter) ax.set_ylabel('Temps de parcours (min)') ax.set_ylim(np.nanmin(self.total_duration)-2., np.nanmax(self.total_duration)+2.) fig.autofmt_xdate() fig.tight_layout() fig.savefig( output, format='png' )
def cxctime2plotdate(times): """ Convert input CXC time (sec) to the time base required for the matplotlib plot_date function (days since start of year 1). :param times: iterable list of times :rtype: plot_date times """ # Find the plotdate of first time and use a relative offset from there t0 = Chandra.Time.DateTime(times[0]).unix plotdate0 = epoch2num(t0) return (np.asarray(times) - times[0]) / 86400. + plotdate0
def draw_charts(i): a.clear() #Formatowanie wykresu secs = mdate.epoch2num(time_list) date_fmt = '%d-%m-%y %H:%M:%S' date_formatter = mdate.DateFormatter(date_fmt) a.xaxis.set_major_formatter(date_formatter) f.autofmt_xdate() #Pobranie typu i informacji o nim chart_type = choose_type(actual_chart_type) #Ustawienia etykiet a.set_ylabel(chart_type[0], color=chart_type[2]) #Rysowanie wykresow a.plot_date(secs,chart_type[1], linestyle='-', color=chart_type[2])
def plot_ts_value(values, tss, ax1, ax2): # Choose your xtick format string date_fmt = '%d-%m-%y %H:%M:%S' # Use a DateFormatter to set the data to the correct format. date_formatter = mdate.DateFormatter(date_fmt) ax1.xaxis.set_major_formatter(date_formatter) ax2.xaxis.set_major_formatter(date_formatter) # Sets the tick labels diagonal so they fit easier. fig.autofmt_xdate() ts_track = np.array(tss) / 1000 ts_track = mdate.epoch2num(ts_track) ax1.plot_date(ts_track, values, marker='x', ms=1, c='green') n = len(tss) gap = 1000 k = n/gap for i in xrange(k+1): X = np.array(tss[i*gap: min((i+1)*gap, n)]).reshape(-1, 1) y = np.array(values[i*gap: (i+1)*gap]).reshape(-1, 1) lr = linear_model.LinearRegression() lr.fit(X, y) if lr.coef_ < 0: divide_fit(X, y, ax2) else: X_ts_track = mdate.epoch2num(np.array(X)/1000) ax2.plot_date(X_ts_track, lr.predict(np.array(X).reshape(-1, 1))[:], marker='o', ms=1, c='red') ax1.set_ylim(ymin=0) ax2.set_ylim(ymin=0) ax1.set_xlabel('Epoch time') ax2.set_xlabel('Epoch time') ax1.set_ylabel('Rate limit track') ax2.set_ylabel('Rate limit track')
def key_press(self, event): if event.key in ['z', 'p'] and event.inaxes: x0, x1 = self.ax.get_xlim() dx = x1 - x0 xc = event.xdata zoom = self.zoom if event.key == 'p' else 1.0 / self.zoom new_x1 = zoom * (x1 - xc) + xc new_x0 = new_x1 - zoom * dx tstart = max(num2epoch(new_x0), MIN_TSTART_UNIX) tstop = min(num2epoch(new_x1), MAX_TSTOP_UNIX) new_x0 = epoch2num(tstart) new_x1 = epoch2num(tstop) self.ax.set_xlim(new_x0, new_x1) self.ax.figure.canvas.draw_idle() elif event.key == 'm': for _ in range(len(self.ax.lines)): self.ax.lines.pop() self.plot_mins = not self.plot_mins print '\nPlotting mins and maxes is {}'.format( 'enabled' if self.plot_mins else 'disabled') self.draw_plot() elif event.key == 'a': # self.fig.clf() # self.ax = self.fig.gca() self.ax.set_autoscale_on(True) self.draw_plot() self.ax.set_autoscale_on(False) self.xlim_changed(None) elif event.key == 'y': self.scaley = not self.scaley print 'Autoscaling y axis is {}'.format( 'enabled' if self.scaley else 'disabled') self.draw_plot() elif event.key == '?': print """
def plot_common(data, tz): dates = [mdates.epoch2num(int(c[0])) for c in data] hours = mdates.HourLocator(interval=4, tz=tz) dateFmt = mdates.DateFormatter('%H:%M', tz=tz) fig,ax = plt.subplots() dpi = fig.get_dpi() fig.set_size_inches(600/dpi, 200/dpi) ax.xaxis.set_major_locator(hours) ax.xaxis.set_major_formatter(dateFmt) ax.grid(which='major', linestyle='solid', color='grey') return dates, fig, ax
def get_root(self, args): power_list = getLastXPoints(int(args["past_hours"][0]), int(args["num_points"][0])) print power_list x_axis_epoch = [] y_axis = [] if power_list != []: for point in power_list[0]["points"]: x_axis_epoch.append(point[0]) y_axis.append(point[2]) # Convert to the correct format for matplotlib. # mdate.epoch2num converts epoch timestamps to the right format for matplotlib secs = mdate.epoch2num(x_axis_epoch) fig, ax = plt.subplots() # Plot the date using plot_date rather than plot ax.plot_date(secs, y_axis) # Choose your xtick format string date_fmt = '%H:%M:%S' # Use a DateFormatter to set the data to the correct format. date_formatter = mdate.DateFormatter(date_fmt) ax.xaxis.set_major_formatter(date_formatter) # Sets the tick labels diagonal so they fit easier. fig.autofmt_xdate() plt.ylim(0, 80) plt.ylabel("Power Usage (W)") plt.xlabel("Time (UTC)") plt.title("Time vs Power Usage") fig.set_size_inches(12,8) root_html = "<html><body><div style=\"float:left\">" root_html += mpld3.fig_to_html(fig).encode("ascii") if len(y_axis) > 0: root_html += "</div><div style=\"float:right\"></br><bold>STATISTICS:</bold> </br>" root_html += "MIN Power: " + str(round(nps.min(y_axis), 6)) + " Watts </br>" root_html += "MAX Power: " + str(round(nps.max(y_axis), 6)) + " Watts </br>" root_html += "AVG Power: " + str(round(nps.average(y_axis), 6)) + " Watts </br>" root_html += "Energy Usage: " + str(round((nps.average(y_axis)*((nps.max(x_axis_epoch)-nps.min(x_axis_epoch))/float(60*60)))/1000, 6)) + " Kilowatt Hours </br>" root_html += "</div></body></html>" plt.close() return root_html
def ucsc_flt_lev_vars(fname): """Read in data from NetCDF file containing P3 flight level data created by NOAA AOC. Pull out the needed variables for flight track info. INPUT:: fname = Filename [string] OUTPUT:: Time = Aircraft time array [Datetime object] Rhoair = Air density [kg/m^3] VertVel = Vertical Wind [m/s] Altitude = Aircraft altitude [m] USAGE:: Time,Rhoair,VertVel = ucsc_flt_lev_vars(fname) NOTES:: The variables are masked of bad values """ # HISTORY:: # 7 Mar 2014 - Nick Guy NOAA/NSSL/WRDD, NRC #--------------------------------------------------- # Set a sea level density value sea_level_dens = 1.2250 #[kg/m^3] # Read the NetCDF ncFile = n4.Dataset(fname, 'r') # Retrieve vertical velocity # VertVel = ncFile.variables['WSZ_DPJ'][:] VertVel = ncFile.variables['UWZ.1'][:] np.ma.masked_invalid(VertVel) # Retrieve variables to calculate Air density Temp = ncFile.variables['TA.1'][:] + 273.15 # Convert from C to K np.ma.masked_invalid(Temp) Alt = ncFile.variables['AltGPS.3'][:] np.ma.masked_invalid(Alt) RhoAir = sea_level_dens * np.exp(-0.04 * Alt / Temp) del Temp # Pull out the start time StartTime = ncFile.StartTime # Create a time array TimeSec = np.linspace(StartTime, StartTime + len(RhoAir), len(RhoAir)) Time = mdate.epoch2num(TimeSec) return Time, RhoAir, VertVel, Alt
def draw_charts(): #Czyszczenie wykresow a_altitude.clear() a_speed.clear() #Ustawienia zwiazane z wyswietlaniem czasu na osi OX secs = mdate.epoch2num(time_list) date_fmt = '%d-%m-%y %H:%M:%S' date_formatter = mdate.DateFormatter(date_fmt) a_altitude.xaxis.set_major_formatter(date_formatter) a_speed.xaxis.set_major_formatter(date_formatter) f.autofmt_xdate() #Ustawienia etykiet a_altitude.set_ylabel('Altitude [ft]', color='RED') a_speed.set_ylabel('KTS', color='GREEN') #Rysowanie wykresow a_altitude.plot_date(secs,altitude_list, linestyle='-', color='RED') a_speed.plot_date(secs, kts_list, linestyle='-', color='GREEN')
def run(self): tLast = 0 while self.runFlag: time.sleep(self.period) tNow = time.time() if tNow >= tLast + self.period: data = self.source() # Convert timestamps to datetimes. times = mdates.num2date(mdates.epoch2num(list(data[0]))) # Convert data to lists - deques do not support slicing. series = [list(d) for d in data[1:]] # Create the plot. fig, ax = plt.subplots(dpi=200) for s in series: pts = min(len(times), len(s)) ax.plot(times[0:pts], s[0:pts]) ax.xaxis.set_minor_locator(mdates.MinuteLocator()) ax.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M:%S')) ax.xaxis.set_major_locator(mdates.HourLocator()) ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d %H:%M:%S')) ax.yaxis.set_major_formatter(FormatStrFormatter("%.1f")) # Format minor ticks. majlocs = ax.xaxis.get_majorticklocs() for loc, label in zip(ax.xaxis.get_minorticklocs(), ax.xaxis.get_minorticklabels()): # Rotate minor ticks label.set_rotation(90) # Hide minor ticks and major tick locations. if loc in majlocs: label.set_visible(False) # Rotate major ticks. for label in ax.xaxis.get_majorticklabels(): label.set_rotation(90) # Make room for the tick labels. plt.subplots_adjust(bottom=.3) with self.lock: if not self.plot: self.plot = io.BytesIO() self.plot.seek(0) fig.canvas.print_figure(self.plot, format='png') self.plot.flush() # Must explicitly close the plot. plt.close() tLast = tNow
def update_k_line(self, tochlva): print("update_k_line", tochlva) self.amount = [i.pop() for i in tochlva] self.volume = [i.pop() for i in tochlva] self.x = [] for i in tochlva: i[0] = mdates.epoch2num(i[0]) #convert timestamp to matplotlib.dates num format self.x.append(i[0]) self.x0 = min(self.x) self.x1 = max(self.x) if self.k_lines == None and self.k_patches == None: self.k_lines,self.k_patches = candlestick_ochl(self.ax1, tochlva)#, width=0.4, colorup='#77d879', colordown='#db3f3f') else: self.k_lines,self.k_patches = candlestick_ochl(self.ax1, tochlva) self.ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) self.ax1.xaxis.set_major_locator(mdates.DayLocator()) plt.xticks(rotation=45)
def _getgraph(self): metricLists = dict() for entry in self.input: #Each entry contains datapoints for one metric, for one host if(entry.metric.name not in metricLists): metricLists[entry.metric.name] = dict() if(entry.host.name not in metricLists[entry.metric.name]): metricLists[entry.metric.name][entry.host.name] = (list(), list()) for datapoint in entry.datapoints: try: metricLists[entry.metric.name][entry.host.name][0].append(dates.epoch2num(int(datapoint.time.strip(':')))) metricLists[entry.metric.name][entry.host.name][1].append(float(datapoint.value)) except Exception: logging.info("Ignored datapoint due to exception: " + str(sys.exc_info())) continue # self._showgraph(metricLists) self.metricLists = metricLists return metricLists