xx = [datetime.datetime.fromtimestamp(x / 1000).strftime("%M:%S") for x in l] print(xx) myFmt = mdates.DateFormatter('%H:%M:%S') plt.gca().xaxis.set_major_formatter(myFmt) ax1Price = plt.subplot2grid((6, 1), (0, 0), rowspan=5, colspan=1) ax2Volume = plt.subplot2grid((6, 1), (5, 0), rowspan=1, colspan=1, sharex=ax1Price) ax2Volume.set_ylim(0, 0.3 * df['VOLUME'].max()) ax1Price.plot(df.index, df['PRICE']) ax2Volume.bar(df.index, df['VOLUME']) # setting the 2 levels X axis labels xfmtMajor = mdates.DateFormatter('%M:%S') ax1Price.xaxis.set_major_locator(mdates.SecondLocator(interval=60)) ax1Price.xaxis.set_major_formatter(xfmtMajor) xfmtMinor = mdates.DateFormatter('%S') ax1Price.xaxis.set_minor_locator(mdates.SecondLocator(interval=30)) ax1Price.xaxis.set_minor_formatter(xfmtMinor) # defining the vertical interval between the major and minor X axis label rows ax1Price.get_xaxis().set_tick_params(which='major', pad=20) #fig.autofmt_xdate() #plt.savefig('myfig') plt.show()
ax.plot_date(timea, valuea, 'g-', label="First Sensor (uT)") ax.plot_date(timeb, valueb, 'b-', label="Second Sensor (uT)") #ax.plot_date(timed, valued,'m-', label="Sensor 5") #ax.plot_date(timee, valuee,'y-', label="Sensor 6") ax.set_xlabel('time (hour:minutes:seconds)') ax.set_ylabel('Magnetic Magnitude (uT)', color='g') legend = ax.legend(loc='upper left', shadow=True) frame = legend.get_frame() frame.set_facecolor('0.90') fig.autofmt_xdate() xfmt = mdates.DateFormatter('%H:%M:%S') ax.xaxis.set_major_formatter(xfmt) ax.xaxis.set_major_locator(mdates.SecondLocator(interval=int(locator))) plt.show() fig, ax = plt.subplots() axes = [ax, ax.twinx()] fig.subplots_adjust(right=0.75) axes[0].plot_date(timea, valuea, 'g-') axes[0].set_xlabel('time (minutes:seconds)') axes[0].set_ylabel('First Sensor (uT)', color='g') axes[0].tick_params('y', colors='g') t1, t2 = axes[0].get_xlim() #axes[0].set_xlim(736598.589500, 736598.591681) axes[1].plot_date(timeb, valueb, 'b-') axes[1].set_ylabel('Second Sensor (uT)', color='b')
def matplotlib_plot(ev, phaseslist, allsta, arrtimes, alltraces, model, options): print "plot with matplotlib ..." mytitle = "EVID %d - M%3.1f %s on %s (Lat: %.2f; Lon: %.2f; Z: %dkm)" % ( ev.evid, ev.mag, ev.region, str( ev.OTutc)[0:21], ev.lat, ev.lon, ev.depth) fig2 = plt.figure(figsize=[12, 8]) if (options.section): from matplotlib.transforms import blended_transform_factory alltraces.plot(fig=fig2, type='section', ev_coord=(ev.lat, ev.lon), dist_degree=True, time_down=True, linewidth=.35, grid_linewidth=.25, offsetmin=0, recordlength=float(options.sig_length)) axes = fig2.get_axes() ax = axes[0] transform = blended_transform_factory(ax.transData, ax.transAxes) for tr in alltraces: #print tr.stats.distance/1e6, tr.stats.station ax.text(tr.stats.distance / 1e6, 1.0, tr.stats.station, rotation=305, va="bottom", ha="center", transform=transform) fig2.suptitle(mytitle, fontsize=11) fig2.tight_layout(pad=0.5, rect=(0, 0, 1, 0.90)) png = "%s/%d.section.png" % (DATADIR, ev.evid) plt.savefig(png) plt.show() exit() else: alltraces.plot(fig=fig2, automerge=False) axes = fig2.get_axes() XLIM = axes[0].get_xlim() DX = (XLIM[1] - XLIM[0]) * 1440 NEWXLIM = ((XLIM[0], XLIM[0] + float(options.sig_length) / 60 / 1440)) print i = 0 for trace in alltraces: print "== Process trace %s" % trace tr = trace.stats Xtime_min = trace.stats.starttime Xtime_max = trace.stats.endtime sampling_rate = trace.stats.sampling_rate timestep = (1 / sampling_rate) * 1000 npts = int(trace.stats['npts']) print(" Nb points= %d ; sampling rate= %d Hz ; time step= %f ms" % (npts, sampling_rate, timestep)) trace.trim( Xtime_min, Xtime_min + datetime.timedelta(milliseconds=float(options.sig_length) * 1000)) axes[i].set_xlim(NEWXLIM[0], NEWXLIM[1]) axes[i].xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')) axes[i].xaxis.set_major_locator(dates.SecondLocator(interval=30)) axes[i].xaxis.set_minor_locator(dates.SecondLocator(interval=10)) labelx = axes[i].get_xticklabels() plt.setp(labelx, rotation=30, fontsize=9) Ymax = min(max(trace.data) * 1.01, int(options.ampmax)) # 5000 max ystep = max((int(Ymax / 1000) + 1) * 1000 / 2, 100) Ymaxunit = len(str(int(Ymax))) Ymaxsize = str(int(Ymax))[0] ystep = int(Ymaxsize) * pow(10, (int(Ymaxunit) - 1)) axes[i].set_ylim(-Ymax, Ymax) loc = ticker.MultipleLocator( base=ystep) # this locator puts ticks at regular intervals axes[i].yaxis.set_major_locator(loc) labely = axes[i].get_yticklabels() plt.setp(labely, fontsize=9) arrivals = model.get_travel_times( source_depth_in_km=ev.depth, distance_in_degree=allsta[tr.station].epidist_deg, phase_list=phaseslist) arrtimes[tr.station] = [] for arr in arrivals: arrtimes[tr.station].append( (arr.name, TIMESTAMP_TO_DATETIME(ev.oritime + arr.time))) phases_done = [] for pick in arrtimes[tr.station]: if (pick[0] in phases_done): continue phases_done.append(pick[0]) phase_pick = pick[1] x = [phase_pick, phase_pick] y = [-Ymax, 0] axes[i].plot(x, y) offsetx = datetime.timedelta( milliseconds=(NEWXLIM[1] - NEWXLIM[0]) * 1000 * 0.01) axes[i].text(phase_pick + offsetx, -Ymax, pick[0], style='normal', bbox={ 'facecolor': 'lightblue', 'alpha': 0.8, 'pad': 4 }, fontsize=8) streamcode = "%s_%s:%s:%s" % (tr.network, tr.station, tr.location, tr.channel) axes[i].set_title( "%s (%.1f degrees ; %d km ; Azim %d) - Filter [%.1f-%.1f Hz]" % (streamcode, allsta[tr.station].epidist_deg, int(allsta[tr.station].epidist_km), allsta[tr.station].azimuth, float(options.freqmin), float(options.freqmax)), fontsize=9) i = i + 1 fig2.suptitle(mytitle, fontsize=11) fig2.tight_layout(pad=0.5, rect=(0, 0, 1, 0.95)) png = "%s/%d.png" % (DATADIR, ev.evid) plt.savefig(png) plt.show()
def run_with_string_diagram(sys, start_time, end_time): '''To draw the string diagram based on the schedule dictionary for all the trains. ''' colors = ['red', 'green', 'blue', 'black', 'orange', 'cyan', 'magenta'] color_num = len(colors) x, y = [], [] for i in range(len(sys.trains)): x.append([]) y.append([]) for j in range(len(sys.trains[i].time_pos_list)): x[i].append( datetime.fromtimestamp(sys.trains[i].time_pos_list[j][0])) y[i].append(sys.trains[i].time_pos_list[j][1]) # x[i].append(sys.trains[i].time_pos_list[j][0]) # y[i].append(sys.trains[i].time_pos_list[j][1]) assert len(x) == len(y) for i in range(len(x)): assert len(x[i]) == len(y[i]) train_idx = list(range(len(x))) t_color = [colors[x.index(i) % color_num] for i in x] min_t, max_t = min([i[0] for i in x if i]), max([i[-1] for i in x if i]) # plt.ion() plt.title('String Diagram') hours = mdates.HourLocator() minutes = mdates.MinuteLocator() seconds = mdates.SecondLocator() dateFmt = mdates.DateFormatter("%H:%M") plt.gca().xaxis.set_major_locator(hours) plt.gca().xaxis.set_minor_locator(minutes) plt.gca().xaxis.set_major_formatter(dateFmt) plt.xticks(rotation=90) plt.grid(True, linestyle="-.", color="r", linewidth="0.1") plt.legend() plt.xlabel('Time') plt.ylabel('Mile Post/miles') start_time = int(start_time.timestamp()) end_time = int(end_time.timestamp()) plt.axis([(datetime.fromtimestamp(start_time - 500)), (datetime.fromtimestamp(end_time + 500)), -5, 55]) # =============================================================================== # time_length = end_time - start_time # step_size = 10 # for start in range(1,time_length + 1, step_size): # plt.axis([(datetime.fromtimestamp(start_time - 500)), \ # (datetime.fromtimestamp(end_time + 500)), -5 , 55]) # for n in range(len(x)-1): # new_x_y = [[mdates.date2num(datetime.fromtimestamp(i)), j] for i, j in zip(x[n], y[n]) if i < start_time + start and i > start_time + start - 1 - step_size] # new_x = [] # new_y = [] # for i , j in new_x_y: # new_x.append(i) # new_y.append(j) # if(len(new_x) == 0): # continue # plt.plot(new_x, new_y, color=t_color[n]) # # print('==============') # # print('Length of new_x: {}'.format(len(new_x))) # # print('Length of new_y: {}'.format(len(new_y))) # plt.pause(0.00001) # =============================================================================== for n in range(len(x)): #assert len(x[n]) == len(y[n]) == t_color[n] plt.plot([mdates.date2num(i) for i in x[n]], y[n], color=t_color[n]) plt.gca().axhspan(15, 20, color='yellow', alpha=0.5) plt.gca().axhspan(30, 35, color='yellow', alpha=0.5) # plt.gca().axvspan((datetime.fromtimestamp(start_time + 90 * 60)),(datetime.fromtimestamp(start_time + 150 * 60)),color='black',alpha=0.5) plt.figure(figsize=(18, 16), dpi=80, facecolor='w', edgecolor='k') plt.rcParams['figure.dpi'] = 200 import pylab pylab.rcParams['figure.figsize'] = (15.0, 8.0) plt.show()
def string_diagram(sys): '''To draw the string diagram based on the schedule dictionary for all the trains. ''' plt.rcParams['figure.dpi'] = 200 plt.ion() start_time, end_time = sys.init_time, sys.term_time colors = ['red', 'green', 'blue', 'black', 'orange', 'cyan', 'magenta'] color_num = len(colors) t_color = [colors[i % color_num] for i in range(len(sys.trains))] x, y = [], [] plt.clf() for i in range(len(sys.trains)): x.append([ mdates.date2num(datetime.fromtimestamp(j)) for (j, _) in sys.trains[i].time_pos_list ]) y.append([j for (_, j) in sys.trains[i].time_pos_list]) plt.plot([mdates.date2num(datetime.fromtimestamp(j)) for (j,_) in sys.trains[i].time_pos_list], \ [j for (_,j) in sys.trains[i].time_pos_list], color=t_color[i]) train_idx = list(range(len(sys.trains))) min_t, max_t = min([i[0] for i in x if i]), max([i[-1] for i in x if i]) plt.title('String Diagram') hours = mdates.HourLocator() minutes = mdates.MinuteLocator() seconds = mdates.SecondLocator() dateFmt = mdates.DateFormatter("%H:%M") plt.gca().xaxis.set_major_locator(hours) plt.gca().xaxis.set_minor_locator(minutes) plt.gca().xaxis.set_major_formatter(dateFmt) plt.xticks(rotation=90) plt.grid(True, linestyle="-.", color="r", linewidth="0.1") plt.legend() plt.xlabel('Time') plt.ylabel('Mile Post/miles') plt.axis([(datetime.fromtimestamp(start_time - 500)), \ (datetime.fromtimestamp(end_time + 500)), -5 , 55]) # =============================================================================== # time_length = int(end_time - start_time) # step_size = 10 # for start in range(1, int(time_length + 1), int(step_size)): # plt.axis([(datetime.fromtimestamp(start_time - 500)), \ # (datetime.fromtimestamp(end_time + 500)), -5 , 55]) # for n in range(len(x)-1): # new_x_y = [[mdates.date2num(datetime.fromtimestamp(i)), j] for i, j in zip(x[n], y[n]) if i < start_time + start and i > start_time + start - 1 - step_size] # new_x = [] # new_y = [] # for i , j in new_x_y: # new_x.append(i) # new_y.append(j) # if(len(new_x) == 0): # continue # plt.plot(new_x, new_y, color=t_color[n]) # # print('==============') # # print('Length of new_x: {}'.format(len(new_x))) # # print('Length of new_y: {}'.format(len(new_y))) # plt.pause(0.00001) # =============================================================================== plt.gca().axhspan(15, 20, color='yellow', alpha=0.5) plt.gca().axhspan(30, 35, color='yellow', alpha=0.6) plt.gca().axhspan(25, 40, color='orange', alpha=0.3) # plt.gca().axvspan((datetime.fromtimestamp(start_time + 90 * 60)),(datetime.fromtimestamp(start_time + 150 * 60)),color='black',alpha=0.5) plt.pause(1) plt.ioff() plt.show()
# sma = np.convolve(values, weights, 'valid') # return sma #for i, key in enumerate(x.keys()): # plt.plot(x[key][14:], movingaverage(y[key], 15), c=colors[i]) #plt.plot() ax = plt.gca() plt.xticks(rotation=45, ha='right') plt.title( "Batch Time (solid line plot) and Epoch Time (dotten lines are epochs 1,2..n) as the no. of dockers increase (every 30 secs)" ) fmt = mdates.DateFormatter('%H:%M:%S') ax.xaxis.set_major_formatter(fmt) ax.xaxis.set_major_locator(mdates.SecondLocator(interval=30)) # Vertical lines for epochs for i, keyEpochTime in enumerate(xEpochTime.keys()): plt.vlines(xEpochTime[keyEpochTime], ymin=0, ymax=max(list(y.values())[0]) + 0.1, color=cmap.colors[i], linestyle='dashed') #Line plot for batch time for i, key in enumerate(x.keys()): plt.plot(x[key], y[key], c=cmap.colors[i], label=key) plt.legend(loc="upper left") plt.tight_layout()
def datetick(dir, **kwargs): ''' datetick('x') or datetick('y') formats the major and minor tick labels of the current figure. datetick('x', axes=ax) or datetick('y', axes=ax) formats the given axes `ax`. Example: -------- import datetime as dt import numpy as np import matplotlib.pyplot as plt from hapiclient.plot.datetick import datetick d1 = dt.datetime(1900, 1, 2) d2 = dt.datetime.fromordinal(10 + dt.datetime.toordinal(d1)) x = np.array([d1, d2], dtype=object) y = [0.0,1.0] plt.clf() plt.plot(x, y) datetick('x') ''' # Based on spacepy/plot/utils.py on 07/10/2017, but many additions. # TODO: Account for leap years instead of using 367, 366, etc. # TODO: Use numsize() to determine if figure width and height # will cause overlap when default number major tick labels is used. # TODO: If time[0].day > 28, need to make first tick at time[0].day = 28 # as needed. # TODO: If first data point has fractional seconds, the plot won't have # a major x-label right below it. This is due to the fact that # MicrosecondLocator() does not take a keyword argument of # "bymicroseconds". # TODO: Adjust lower and upper limits as in 366*8 span def millis(x, pos): x = matplotlib.dates.num2date(x) label = x.strftime('.%f') label = label[0:3] #label = label.rstrip(".") return label DOPTS = {} DOPTS.update({'debug': False}) DOPTS.update({'set_cb': True}) DOPTS.update({'axes': None}) # Override defaults for key, value in kwargs.items(): if key in DOPTS: DOPTS[key] = value else: print('Warning: Keyword option "%s" is not valid.' % key) if 'axes' in kwargs: axes = kwargs['axes'] fig = axes.figure else: axes = plt.gca() fig = plt.gcf() #import pdb #pdb.set_trace() debug = DOPTS['debug'] def on_xlims_change(ax): datetick('x', axes=ax, set_cb=False) def on_ylims_change(ax): datetick('y', axes=ax, set_cb=False) def draw(fig): fig.canvas.draw() # Render new ticks and tick labels bbox = axes.dataLim if dir == 'x': datamin = bbox.x0 datamax = bbox.x1 lim = axes.get_xlim() else: datamin = bbox.y0 datamax = bbox.y1 lim = axes.get_ylim() try: mpld.num2date(lim[0]) except: raise ValueError( 'Lower axis limit of %f is not a valid Matplotlib datenum' % lim[0]) try: mpld.num2date(lim[1]) except: raise ValueError( 'Upper axis limit of %f is not a valid Matplotlib datenum' % lim[1]) try: mpld.num2date(datamin) except: raise ValueError( 'Minimum data value of %f is not a valid Matplotlib datenum' % datamin) try: mpld.num2date(datamax) except: raise ValueError( 'Maximum data value of %f is not a valid Matplotlib datenum' % datamax) tmin = np.max((lim[0], datamin)) tmax = np.min((lim[1], datamax)) time = mpld.num2date((tmin, tmax)) if debug == True: print('Data min time: %f' % datamin) print('Data max time: %f' % datamax) deltaT = time[-1] - time[0] nDays = deltaT.days nHours = deltaT.days * 24.0 + deltaT.seconds / 3600.0 nSecs = deltaT.total_seconds() if debug == True: print("Total seconds: %s" % deltaT.total_seconds()) # fmt is format of the tick labels # fmt2 contains additional information that is used for the first tick label # or when there is a majore change. For example, if # fmt = %M:%S and fmt2 = %H, the labels will have only minute and hour and # the first tick will have a label of %M:%S\n%H. If there is a change # in hour somewhere on the axis, that label will include the new hour. # Note that interval=... is specified even when it would seem to be # redundant. It is needed to workaround the bug discussed at # https://stackoverflow.com/questions/31072589/matplotlib-date-ticker-exceeds-locator-maxticks-error-for-no-apparent-reason if deltaT.total_seconds() < 0.1: # < 0.1 second # At this level of zoom, would need original datetime data # which has not been converted by date2num and then re-plot # using integer number of milliseconds. E.g., call # plotd(dtobj,y) # where # t = dtobj converted to milliseconds since first array value # plotd() calls # plot(t,y) # and then makes first label indicate %Y-%m-%dT%H:%M:%S warnings.warn( "Warning: Cannot create accurate time labels with this time resolution." ) # This does not locate microseconds. Mtick = mpld.MicrosecondLocator(interval=10000) mtick = mpld.MicrosecondLocator(interval=2000) from matplotlib.ticker import FuncFormatter fmt = FuncFormatter(millis) fmt2 = '%H:%M:%S\n%Y-%m-%d' if deltaT.total_seconds() < 0.5: # < 0.5 seconds # Locators don't locate at this resolution. # Need to do this manually. See comment above. warnings.warn( "Warning: Cannot create accurate time labels with this time resolution." ) Mtick = mpld.MicrosecondLocator(interval=50000) mtick = mpld.MicrosecondLocator(interval=10000) from matplotlib.ticker import FuncFormatter fmt = FuncFormatter(millis) fmt2 = '%H:%M:%S\n%Y-%m-%d' if deltaT.total_seconds() < 1: # < 1 second # https://matplotlib.org/api/dates_api.html#matplotlib.dates.MicrosecondLocator # MircosecondLocator() does not have a "bymicrosecond" option. If # First point is not at zero microseconds, it won't be labeled. Mtick = mpld.MicrosecondLocator(interval=100000) mtick = mpld.MicrosecondLocator(interval=20000) from matplotlib.ticker import FuncFormatter fmt = FuncFormatter(millis) #fmt = mpld.DateFormatter('%M:%S.%f') fmt2 = '%H:%M:%S\n%Y-%m-%d' elif deltaT.total_seconds() < 5: # < 5 seconds Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 1))) mtick = mpld.MicrosecondLocator(interval=200000) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 10: # < 10 seconds Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 1))) mtick = mpld.MicrosecondLocator(interval=500000) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 20: # < 20 seconds Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 2))) mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 1))) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 30: # < 30 seconds Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 5))) mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 1))) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 60: # < 1 minute Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 10))) mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 2))) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 60 * 2: # < 2 minutes Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 20))) mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 5))) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 60 * 3: # < 3 minutes Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 20))) mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 5))) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 60 * 5: # < 5 minutes Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 30))) mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 10))) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 60 * 10: # < 10 minutes Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 1))) mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 15))) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 60 * 20: # < 20 minutes Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 2))) mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 30))) fmt = mpld.DateFormatter('%M:%S') fmt2 = '%Y-%m-%dT%H' elif deltaT.total_seconds() < 60 * 30: # < 30 minutes Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 5))) mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 1))) fmt = mpld.DateFormatter('%H:%M') fmt2 = '%Y-%m-%d' elif deltaT.total_seconds() < 60 * 60: # < 60 minutes Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 10))) mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 2))) fmt = mpld.DateFormatter('%H:%M') fmt2 = '%Y-%m-%d' elif nHours < 2: Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 15))) mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 5))) fmt = mpld.DateFormatter('%H:%M') fmt2 = '%Y-%m-%d' elif nHours < 4: Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 20))) mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 5))) fmt = mpld.DateFormatter('%H:%M') fmt2 = '%Y-%m-%d' elif nHours < 6: Mtick = mpld.HourLocator(byhour=list(range(0, 24, 1))) mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 10))) fmt = mpld.DateFormatter('%H:%M') fmt2 = '%Y-%m-%d' elif nHours < 12: Mtick = mpld.HourLocator(byhour=list(range(0, 24, 2))) mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 30))) fmt = mpld.DateFormatter('%H:%M') fmt2 = '%Y-%m-%d' elif nHours < 24: # < 1 day Mtick = mpld.HourLocator(byhour=list(range(0, 24, 3))) mtick = mpld.HourLocator(byhour=list(range(0, 24, 1))) fmt = mpld.DateFormatter('%H') fmt2 = '%Y-%m-%d' elif nHours < 48: # < 2 days Mtick = mpld.HourLocator(byhour=list(range(0, 24, 4))) mtick = mpld.HourLocator(byhour=list(range(0, 24, 2))) fmt = mpld.DateFormatter('%H') fmt2 = '%Y-%m-%d' elif nHours < 72: # < 3 days Mtick = mpld.HourLocator(byhour=list(range(0, 24, 6))) mtick = mpld.HourLocator(byhour=list(range(0, 24, 3))) fmt = mpld.DateFormatter('%H') fmt2 = '%Y-%m-%d' elif nHours < 96: # < 4 days Mtick = mpld.HourLocator(byhour=list(range(0, 24, 12))) mtick = mpld.HourLocator(byhour=list(range(0, 24, 3))) fmt = mpld.DateFormatter('%H') fmt2 = '%Y-%m-%d' elif deltaT.days < 8: Mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1))) mtick = mpld.HourLocator(byhour=list(range(0, 24, 4))) fmt = mpld.DateFormatter('%d') fmt2 = '%Y-%m' elif deltaT.days < 16: Mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1))) mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1))) fmt = mpld.DateFormatter('%d') fmt2 = '%Y-%m' elif deltaT.days < 32: Mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 4))) mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1))) fmt = mpld.DateFormatter('%d') fmt2 = '%Y-%m' elif deltaT.days < 60: Mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 7))) mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1))) fmt = mpld.DateFormatter('%d') fmt2 = '%Y-%m' elif deltaT.days < 183: Mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 1))) mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 7))) fmt = mpld.DateFormatter('%m') fmt2 = '%Y' elif deltaT.days < 367: Mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 1))) mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 1))) fmt = mpld.DateFormatter('%m') fmt2 = '%Y' elif deltaT.days < 366 * 2: Mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 2))) mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 1))) fmt = mpld.DateFormatter('%m') fmt2 = '%Y' elif deltaT.days < 366 * 8: Mtick = mpld.YearLocator(1) mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 4))) fmt = mpld.DateFormatter('%Y') fmt2 = '' elif deltaT.days < 366 * 15: to = axes.lines[0].get_xdata()[0] tf = axes.lines[0].get_xdata()[-1] print(to) print(tf) # Ideally would set byyear=list(range(to.year, tf.year,2)) but # byyear is not a kwarg. Would need to something like # https://stackoverflow.com/questions/48428729/matplotlib-dates-yearlocator-with-odd-intervals Mtick = mpld.YearLocator(1) mtick = mpld.YearLocator(1) fmt = mpld.DateFormatter('%Y') fmt2 = '' if False: xl = axes.get_xlim() a = mpld.num2date(xl[0]) print(a) import pdb pdb.set_trace() a = mpld.date2num( a.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)) b = mpld.num2date(xl[1]) b = mpld.date2num( b.replace(year=(b.year + 1), month=1, day=1, hour=0, minute=0, second=0, microsecond=0)) axes.set_xlim([a, b]) elif deltaT.days < 366 * 40: Mtick = mpld.YearLocator(5) mtick = mpld.YearLocator(1) fmt = mpld.DateFormatter('%Y') fmt2 = '' elif deltaT.days < 366 * 100: Mtick = mpld.YearLocator(10) mtick = mpld.YearLocator(2) fmt = mpld.DateFormatter('%Y') fmt2 = '' elif deltaT.days < 366 * 200: Mtick = mpld.YearLocator(20) mtick = mpld.YearLocator(5) fmt = mpld.DateFormatter('%Y') fmt2 = '' else: Mtick = mpld.YearLocator(50) mtick = mpld.YearLocator(10) fmt = mpld.DateFormatter('%Y') fmt2 = '' xt = axes.get_xticks() xl = axes.get_xlim() if debug: print("Default xlim[0]: %s" % mpld.num2date(xl[0])) print("Default xlim[1]: %s" % mpld.num2date(xl[1])) print("Default xticks[0]: %s" % mpld.num2date(xt[0])) print("Default xticks[-1]: %s" % mpld.num2date(xt[-1])) if debug: print("Start: %s" % mpld.num2date(xl[0])) print("Stop: %s" % mpld.num2date(xl[1])) for i in range(0, len(xt)): print("Tick: %s" % mpld.num2date(xt[i])) draw(fig) # Needed? if dir == 'x': axes.xaxis.set_major_locator(Mtick) axes.xaxis.set_minor_locator(mtick) axes.xaxis.set_major_formatter(fmt) draw(fig) # Render new labels so updated for next line labels = [item.get_text() for item in axes.get_xticklabels()] ticks = axes.get_xticks() time = mpld.num2date(ticks) else: axes.yaxis.set_major_locator(Mtick) axes.yaxis.set_minor_locator(mtick) axes.yaxis.set_major_formatter(fmt) draw(fig) # Render new labels so updated for next line labels = [item.get_text() for item in axes.get_yticklabels()] ticks = axes.get_yticks() time = mpld.num2date(ticks) if debug: xl = axes.get_xlim() print(mpld.num2date(xl[0])) print(mpld.num2date(ticks[0])) print(mpld.num2date(xl[1])) print(mpld.num2date(ticks[-1])) if ticks[0] < xl[0]: print('Left-most tick label will be clipped.') if ticks[-1] > xl[1]: print('Right-most tick label will be clipped.') for i in range(0, len(ticks)): print("Tick: %s" % mpld.num2date(ticks[i])) if fmt2 != '': first = 0 if ticks[0] < xl[0]: # Work-around for bug in Matplotlib where left-most tick is less than # lower x-limit. first = 1 # Always apply fmt2 to first tick label labels[first] = '%s\n%s' % (labels[first], datetime.strftime(time[first], fmt2)) for i in range(first + 1, len(time)): # First label will always have fmt applied. # Modify labels after first under certain conditions. modify = False if time[i].year > time[i - 1].year: modify = True if nDays < 60 and time[i].month > time[i - 1].month: modify = True if nDays < 4 and time[i].day > time[i - 1].day: modify = True if nSecs < 60 * 30 and time[i].hour > time[i - 1].hour: modify = True if nSecs < 1 and time[i].minute > time[i - 1].minute: modify = True if nSecs < 1 and time[i].second > time[i - 1].second: modify = True if not modify: continue if i == first + 1 and dir == 'x': # If first two major tick labels have fmt2 applied, the will # likely run together. This keeps fmt2 label for second major # tick. #labels[i] = '%s\n%s' % (labels[i], datetime.strftime(mpld.num2date(ticks[i]), fmt2)) pass else: labels[i] = '%s\n%s' % (labels[i], datetime.strftime( mpld.num2date(ticks[i]), fmt2)) if dir == 'x': axes.set_xticklabels(labels) if dir == 'y': axes.set_yticklabels(labels) # Trigger update of ticks when limits change due to user interaction. if DOPTS['set_cb']: if dir == 'x': axes.callbacks.connect('xlim_changed', on_xlims_change) else: axes.callbacks.connect('ylim_changed', on_ylims_change)
def plot_spk(df_flt, df_speak, seps=None, show_trace=False, show_std=False, beg=0, dur=50, rotation=0, interval=5, markersize=1.5, figsize=(30,15), title='', alpha=1, spkplot_gap=1): """ plot the spk informaiotn of either genuine spk, all spk, or real spk. """ idxes = df_speak.index.unique() idxes = get_meet_sec(df_flt).index sbeg = str(idxes[beg*20]) send = str(idxes[beg*20+dur*20]) df_speak = df_speak.loc[sbeg: send] n_sub = len(df_flt.columns) n_row = n_sub + 1 ## Cannot set [sbeg: send] due to pandas df_flt_part = df_flt.loc[:send] fig, axes = plt.subplots(n_row, 1, figsize=figsize, sharex=True) axs = df_flt_part.plot(figsize=figsize, subplots=True, linewidth=1, marker='o', markersize=markersize, alpha=alpha, title=title, ax=axes[:n_sub]) ### add std if show_std: df_sec = get_meet_sec(df_flt_part) df_std = df_sec.groupby(df_sec.index).std() dt_std = {} colors = [] dt_uc = {} dt_ps = {} for comb in zip(axs, df_flt.columns): ax, u = comb l = ax.lines[0] dt_uc[u] = l.get_color() dt_ps[u] = [] if show_std: dt_std[u] = [] subjects = sorted(dt_uc.keys(), reverse=True) if show_std: for k in df_sec.index.unique(): k1 = k + datetime.timedelta(seconds=1) for u in df_sec.columns: # add std stdu = df_std.ix[k, u] dt_std[u].append([k, k1]) dt_std[u].append([stdu, stdu]) for k in df_speak.index: k1 = k + datetime.timedelta(seconds=1) us = df_speak.loc[df_speak.index == k].speaker.tolist() for u in us: y = -1 * spkplot_gap * ( 1 + subjects.index(u) ) dt_ps[u].append([k, k1]) dt_ps[u].append([y, y]) nax = axes[n_sub] for i,u in enumerate(df_flt.columns): c = dt_uc[u] params = dt_ps[u] axs[i].plot(*params, linewidth=5, color=c) if seps is not None: axs[i].axhline(seps[i], linestyle= '--', color='black', alpha=0.8) axs[i].set_ylim([-10,60]) axs[i].set_ylabel('Volume') axs[i].grid(axis='x', which='major', alpha=0.5, linestyle=':') # add std if show_std: params_std = dt_std[u] axs[i].plot(*params_std, linewidth=3, color='black', linestyle='--') if show_trace and len(params) != 0: nax.axhline(params[1][0], linestyle=':' , color=c ) nax.plot(*params, linewidth=spkplot_gap*20, color=c); nax.set_ylim([0, -1*spkplot_gap*(n_sub+1) ]) nax.set_yticklabels('') nax.xaxis.set_major_locator(mdates.SecondLocator(interval=interval)) dateformatter = ':%S' if dur <= 60 else '%M:%S' nax.xaxis.set_major_formatter(mdates.DateFormatter(dateformatter)) nax.grid(axis='x', which='major', alpha=0.5, linestyle='--') nax.set_xlabel('Time') nax.set_ylabel('Speaker') ## This is just a work-around. Something should be wrong with df.plot (pd version 0.22.) nax.set_xlim([sbeg, send]) plt.xticks(rotation=rotation) plt.tight_layout() return sbeg, send
def plot_data(self, start=None, stop=None, downsample_factor=1): """Plots hip and wrist data whichever/both is available. arguments: -start: timestamp for start of region. Format = "YYYY-MM-DD HH:MM:SS" OR integer for minutes into collection -stop: timestamp for end of region. Format = "YYYY-MM-DD HH:MM:SS" OR integer for minutes into collection -downsample: ratio by which data are downsampled. E.g. downsample=3 will downsample from 75 to 25 Hz If start and stop are not specified, data will be cropped to one of the following: -If no previous graphs have been generated, it will plot the entire data file -If a previous crop has occurred, it will 'remember' that region and plot it again. To clear the 'memory' of previously-plotted regions, enter "x.start_stamp=None" and "x.stop_stop=None" in console """ print( "\n-----------------------------------------------------------------------------------------------------" ) # Gets appropriate timestamps start_stamp, stop_stamp, data_type = self.get_timestamps(start, stop) self.start_stamp = start_stamp self.stop_stamp = stop_stamp # Crops dataframes to selected region ------------------------------------------------------------------------- if self.hip_fname is not None: # Sets stop to end of collection if stop timestamp exceeds timestamp range try: if stop_stamp > self.df_hip.iloc[-1]["Timestamp"]: stop_stamp = self.df_hip.iloc[-1]["Timestamp"] except TypeError: if datetime.strptime(stop_stamp, "%Y-%m-%d %H:%M:%S" ) > self.df_hip.iloc[-1]["Timestamp"]: stop_stamp = self.df_hip.iloc[-1]["Timestamp"] df_hip = self.df_hip.loc[(self.df_hip["Timestamp"] > start_stamp) & (self.df_hip["Timestamp"] < stop_stamp)] if data_type == "absolute": df_hip["Timestamp"] = np.arange( 0, (stop_stamp - start_stamp).seconds, 1 / self.hip_samplerate)[0:df_hip.shape[0]] if downsample_factor != 1: df_hip = df_hip.iloc[::downsample_factor, :] # Window length in minutes window_len = (stop_stamp - start_stamp).total_seconds() / 60 print("Plotting {} minute section from {} to {}.".format( round(window_len, 2), datetime.strftime(start_stamp, "%Y-%m-%d %H:%M:%S"), datetime.strftime(stop_stamp, "%Y-%m-%d %H:%M:%S"))) # Downsampling information ------------------------------------------------------------------------------------ if downsample_factor != 1: if self.hip_fname is not None: print("\nDownsampling {}Hz data by a factor of {}. " "New data is {}Hz.".format( self.hip_samplerate, downsample_factor, round(self.hip_samplerate / downsample_factor, 1))) # Formatting x-axis ticks ------------------------------------------------------------------------------------ if window_len >= .25: xfmt = mdates.DateFormatter("%a %b %d, %H:%M:%S") bottom_plot_crop_value = .17 # Shows milliseconds if plotting less than 15-second window if window_len < .25: xfmt = mdates.DateFormatter("%a %b %d, %H:%M:%S.%f") bottom_plot_crop_value = .23 # Generates ~15 ticks (1/15th of window length apart) locator = mdates.MinuteLocator(byminute=np.arange( 0, 59, int(np.ceil(window_len / 15))), interval=1) # Two-second ticks if window length between 5 and 30 seconds if 1 / 12 < window_len <= .5: locator = mdates.SecondLocator(interval=2) # Plots ~15 ticks if window less than 5 seconds if window_len <= 1 / 12: locator = mdates.MicrosecondLocator( interval=int(1000000 * (window_len * 60 / 15))) # Plots depending on what data is available ------------------------------------------------------------------- if self.hip_fname is not None: def plot_hip(): fig, ax1 = plt.subplots(1, figsize=(self.fig_width, self.fig_height)) plt.subplots_adjust(bottom=bottom_plot_crop_value) ax1.set_title("{} ({} Hz)".format( self.hip_fname.split("/")[-1], int(self.hip_samplerate / downsample_factor))) ax1.plot(df_hip["Timestamp"], df_hip["X"], color='red', label="Hip_X") ax1.plot(df_hip["Timestamp"], df_hip["Y"], color='black', label="Hip_Y") ax1.plot(df_hip["Timestamp"], df_hip["Z"], color='dodgerblue', label="Hip_Z") ax1.legend(loc='lower left') ax1.set_ylabel("G") # Timestamp axis formatting if data_type == "timestamp": ax1.xaxis.set_major_formatter(xfmt) ax1.xaxis.set_major_locator(locator) plt.xticks(rotation=45, fontsize=8) if data_type == "absolute": ax1.set_xlabel("Seconds into collection") plot_hip() f_name = self.check_file_overwrite("Hip_{}Hz_{} to {}".format( int(self.hip_samplerate / downsample_factor), datetime.strftime(start_stamp, "%Y-%m-%d %H_%M_%S"), datetime.strftime(stop_stamp, "%Y-%m-%d %H_%M_%S"))) plt.savefig(f_name + ".png") print("Plot saved as png ({}.png)".format(f_name))
def zoom_chart(df_master, df_master_zoom, plot_d, column_d, disk_type, disk_name): if disk_name == "": TITLE = column_d["Text"] + " " + plot_d["TITLEDATES"] else: TITLE = disk_type+" ("+disk_name+") " + \ column_d["Text"]+" "+plot_d["TITLEDATES"] x = df_master[column_d["Name"]] xz = df_master_zoom[column_d["Name"]] plt.style.use('seaborn-whitegrid') palette = plt.get_cmap(plot_d["Colormap Name"]) color = palette(1) # Two plots on the same figure fig, (ax1, ax2) = plt.subplots(2, 1) plt.gcf().set_size_inches(plot_d["WIDTH"], plot_d["HEIGHT"]) plt.gcf().set_dpi(plot_d["DPI"]) ax1.grid(which='major', axis='both', linestyle='--') ax1.set_title(TITLE, fontsize=14) line1 = ax1.plot(df_master[column_d["Name"]], color=color, alpha=0.7) if plot_d["MEDIAN"]: ax1.plot(x, smooth(df_master[column_d["Name"]], plot_d["movingAverage"]), label='Moving Average', color=palette(2), alpha=0.7, lw=1) ax1.set_ylabel(column_d["Text"], fontsize=10, color=color) ax1.tick_params(labelsize=10) ax1.set_ylim(bottom=0) # Always zero start if column_d["Name"] == "Total CPU_vm": ax1.set_ylim(top=100) if df_master[column_d["Name"]].max() < 10: ax1.yaxis.set_major_formatter( mpl.ticker.StrMethodFormatter('{x:,.2f}')) else: ax1.yaxis.set_major_formatter( mpl.ticker.StrMethodFormatter('{x:,.0f}')) TotalMinutes = (df_master.index[-1] - df_master.index[0]).total_seconds() / 60 if TotalMinutes <= 15: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S")) ax1.xaxis.set_major_locator( mdates.SecondLocator(interval=int((TotalMinutes * 60) / 10))) elif TotalMinutes <= 180: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax1.xaxis.set_major_locator( mdates.MinuteLocator(interval=int(TotalMinutes / 10))) elif TotalMinutes <= 1500: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax1.xaxis.set_major_locator(mdates.HourLocator()) elif TotalMinutes <= 3000: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%d-%H:%M")) else: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%a %m/%d - %H:%M")) plt.setp(ax1.get_xticklabels(), rotation=45, ha="right") TITLE = column_d["Text"] + " Zoom In " color = palette(2) ax2.set_title(TITLE, fontsize=14) line2 = ax2.plot(df_master_zoom[column_d["Name"]], color=color, alpha=0.5) if plot_d["MEDIAN"]: ax2.plot(xz, smooth(df_master_zoom[column_d["Name"]], plot_d["movingAverage"]), label='Moving Average', color=palette(1), alpha=0.7, lw=2) ax2.set_ylabel(column_d["Text"], fontsize=10, color=color) ax2.tick_params(labelsize=10) ax2.set_ylim(bottom=0) # Always zero start if df_master_zoom[column_d["Name"]].max() < 10: ax2.yaxis.set_major_formatter( mpl.ticker.StrMethodFormatter('{x:,.2f}')) else: ax2.yaxis.set_major_formatter( mpl.ticker.StrMethodFormatter('{x:,.0f}')) TotalMinutes = (df_master_zoom.index[-1] - df_master_zoom.index[0]).total_seconds() / 60 if TotalMinutes <= 15: ax2.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S")) ax2.xaxis.set_major_locator( mdates.SecondLocator(interval=int((TotalMinutes * 60) / 10))) elif TotalMinutes <= 180: ax2.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax2.xaxis.set_major_locator( mdates.MinuteLocator(interval=int(TotalMinutes / 10))) elif TotalMinutes <= 1500: ax2.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax2.xaxis.set_major_locator(mdates.HourLocator()) elif TotalMinutes <= 3000: ax2.xaxis.set_major_formatter(mdates.DateFormatter("%d-%H:%M")) else: ax2.xaxis.set_major_formatter(mdates.DateFormatter("%a %m/%d - %H:%M")) ax2.grid(which='major', axis='both', linestyle='--') plt.setp(ax2.get_xticklabels(), rotation=45, ha="right") plt.tight_layout() FinalFileName = plot_d["outputFile_png"] + "_" + ( plot_d["RunDate"][0].strftime('%Y-%m-%d') + " " + disk_type + " " + column_d["Text"] + " " + plot_d["ZOOM_TO"] + ".png").replace(" ", "_") plt.savefig(FinalFileName, format='png') plt.close(fig)
def free_chart(df_master, plot_d, columns_to_show, TITLE, y_label_l, y_label_r, y_max_l, y_max_r, zoom): # What are the attribtes of this chart Right_axis_used = False for column_d in columns_to_show: if column_d["axis"] == "right": Right_axis_used = True break TotalMinutes = (df_master.index[-1] - df_master.index[0]).total_seconds() / 60 axis_greater_than_10_left = False axis_greater_than_10_right = False # Start the plot plt.style.use('seaborn-whitegrid') palette = plt.get_cmap(plot_d["Colormap Name"]) fig, ax1 = plt.subplots() plt.gcf().set_size_inches(plot_d["WIDTH"], plot_d["HEIGHT"]) plt.gcf().set_dpi(plot_d["DPI"]) ax1.grid(which='major', axis='both', linestyle='--') ax1.set_title(TITLE, fontsize=14) # This where the left hand plot happens colour_count = 1 YAxisMaxL = 0 YAxisMaxR = 0 for column_d in columns_to_show: if column_d["axis"] == "left": if plot_d["limit_yaxis"] and y_max_l == 0: # To remove outliers from chart limit x axis to 3 sigma YAxis3Sigma = df_master[column_d["Name"]].mean( ) + 3 * df_master[column_d["Name"]].std() if YAxis3Sigma > YAxisMaxL: YAxisMaxL = YAxis3Sigma if YAxisMaxL > 10: axis_greater_than_10_left = True else: if df_master[column_d["Name"]].max() > 10: axis_greater_than_10_left = True ax1.plot(df_master[column_d["Name"]], label=column_d["Text"], color=palette(colour_count), alpha=0.5, linestyle=column_d["Style"], linewidth=column_d["Linewidth"], markersize=column_d["Markersize"], marker=column_d["Markerstyle"]) colour_count = colour_count + 1 if plot_d["limit_yaxis"]: ax1.set_title("3 Sigma " + TITLE, fontsize=14) else: ax1.set_title(TITLE, fontsize=14) ax1.set_ylabel(y_label_l, fontsize=10) ax1.tick_params(labelsize=10) ax1.set_ylim(bottom=0) # Always zero start if plot_d["limit_yaxis"] and y_max_l == 0: y_max_l = YAxisMaxL if y_max_l > 0: ax1.set_ylim(top=y_max_l) if axis_greater_than_10_left: ax1.yaxis.set_major_formatter( mpl.ticker.StrMethodFormatter('{x:,.0f}')) else: ax1.yaxis.set_major_formatter( mpl.ticker.StrMethodFormatter('{x:,.2f}')) if TotalMinutes <= 15: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S")) ax1.xaxis.set_major_locator( mdates.SecondLocator(interval=int((TotalMinutes * 60) / 10))) elif TotalMinutes <= 180: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax1.xaxis.set_major_locator( mdates.MinuteLocator(interval=int(TotalMinutes / 10))) elif TotalMinutes <= 1500: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax1.xaxis.set_major_locator(mdates.HourLocator()) elif TotalMinutes <= 3000: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%d-%H:%M")) else: ax1.xaxis.set_major_formatter(mdates.DateFormatter("%a %m/%d - %H:%M")) ax1.legend(loc="upper left") if Right_axis_used: ax2 = ax1.twinx() for column_d in columns_to_show: if column_d["axis"] == "right": if plot_d["limit_yaxis"] and y_max_r == 0: # To remove outliers from chart limit x axis to 3 sigma YAxis3Sigma = df_master[column_d["Name"]].mean( ) + 3 * df_master[column_d["Name"]].std() if YAxis3Sigma > YAxisMaxR: YAxisMaxR = YAxis3Sigma if YAxisMaxR > 10: axis_greater_than_10_right = True else: if df_master[column_d["Name"]].max() > 10: axis_greater_than_10_right = True ax2.plot(df_master[column_d["Name"]], label=column_d["Text"], color=palette(colour_count), alpha=0.5, linestyle=column_d["Style"], linewidth=column_d["Linewidth"], markersize=column_d["Markersize"], marker=column_d["Markerstyle"]) colour_count = colour_count + 1 ax2.set_ylabel(y_label_r, fontsize=10) ax2.tick_params(labelsize=10) ax2.set_ylim(bottom=0) # Always zero start if plot_d["limit_yaxis"] and y_max_r == 0: y_max_r = YAxisMaxR if y_max_r > 0: ax2.set_ylim(top=y_max_r) if axis_greater_than_10_right: ax2.yaxis.set_major_formatter( mpl.ticker.StrMethodFormatter('{x:,.0f}')) else: ax2.yaxis.set_major_formatter( mpl.ticker.StrMethodFormatter('{x:,.2f}')) ax2.grid(None) if TotalMinutes <= 15: ax2.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S")) ax2.xaxis.set_major_locator( mdates.SecondLocator(interval=int((TotalMinutes * 60) / 10))) elif TotalMinutes <= 180: ax2.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax2.xaxis.set_major_locator( mdates.MinuteLocator(interval=int(TotalMinutes / 10))) elif TotalMinutes <= 1500: ax2.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax2.xaxis.set_major_locator(mdates.HourLocator()) elif TotalMinutes <= 3000: ax2.xaxis.set_major_formatter(mdates.DateFormatter("%d-%H:%M")) else: ax2.xaxis.set_major_formatter( mdates.DateFormatter("%a %m/%d - %H:%M")) ax2.legend(loc="upper right") plt.setp(ax1.get_xticklabels(), rotation=45, ha="right") plt.tight_layout() FinalFileName = plot_d["outputFile_png"] + "_" + (TITLE + ".png").replace( ": ", "_").replace(",", "_").replace(" ", "_").replace("__", "_") plt.savefig(FinalFileName, format='png') plt.close(fig)
def matplotlib_plot(ev, phaseslist, allsta, arrtimes, alltraces, model, options, DATADIR): print "plot with matplotlib ..." mytitle = "EVID %d - M%3.1f %s on %s (Lat: %.2f; Lon: %.2f; Z: %dkm)" % ( ev.evid, ev.mag, ev.region, str( ev.OTutc)[0:21], ev.lat, ev.lon, ev.depth) fig2 = plt.figure(figsize=[11, 5]) #fig, (ax1,ax2,ax3) = plt.subplots(3) axes = fig2.get_axes() print i = 0 from obspy import read_inventory, Stream for trace in alltraces: print "== Process trace %s" % trace tr = trace.stats try: myinv = read_inventory( os.path.expanduser('./responses/%s.xml' % tr.station)) trace.attach_response(myinv) tr_copy = trace.copy() mystream = Stream(traces=[tr_copy]) mystream_acc = mystream.copy() mystream_vel = mystream.copy() mystream_disp = mystream.copy() #mystream_acc.attach_response(myinv) mystream_acc.remove_response(output='ACC') #mystream_vel.attach_response(myinv) mystream_vel.remove_response(output='VEL') #mystream_disp.attach_response(myinv) mystream_disp.remove_response(output='DISP') print "<<<<<<<" for mytrace in mystream_acc: print('Station %s PGA: %.4f m/s/s (%.3f mg)' % (mytrace.stats.station, max(abs(mytrace.data)), max(abs(mytrace.data)) / 9.81 * 1000)) for mytrace in mystream_vel: print( 'Station %s PGV: %.3f micrometers/s' % (mytrace.stats.station, max(abs(mytrace.data)) * 1000000)) for mytrace in mystream_disp: print( 'Station %s PGD: %.3f micrometers' % (mytrace.stats.station, max(abs(mytrace.data)) * 1000000)) print "<<<<<<<" except: print("WARNING: no response file found for station %s" % tr.station) print("\tDownload response file via associated webservice") print( "\tExample with Raspberryshake webservice: curl -k 'https://fdsnws.raspberryshakedata.com/fdsnws/station/1/query?network=AM&station=RDF31&level=resp&format=sc3ml' -o RDF31.xml" ) pass if (options.output == 'vel'): print 'Plot velocity' trace = mystream_vel[0] elif (options.output == 'disp'): print 'Plot displacement' trace = mystream_disp[0] else: print 'Plot raw data' trace.plot(fig=fig2, automerge=False) print 'Plot spectrogram' clip = [0.0, 0.4] ###pre_filt=[0.01,0.05,25,30] trace.spectrogram(log=False, mult=8.0, wlen=5.0, per_lap=0.9, clip=clip) XLIM = axes[0].get_xlim() DX = (XLIM[1] - XLIM[0]) * 1440 NEWXLIM = ((XLIM[0], XLIM[0] + float(options.sig_length) / 60 / 1440)) Xtime_min = trace.stats.starttime Xtime_max = trace.stats.endtime sampling_rate = trace.stats.sampling_rate timestep = (1 / sampling_rate) * 1000 npts = int(trace.stats['npts']) print(" Nb points= %d ; sampling rate= %d Hz ; time step= %f ms" % (npts, sampling_rate, timestep)) trace.trim( Xtime_min, Xtime_min + timedelta(milliseconds=float(options.sig_length) * 1000)) axes[i].set_xlim(NEWXLIM[0], NEWXLIM[1]) axes[i].xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')) axes[i].xaxis.set_major_locator(dates.SecondLocator(interval=30)) axes[i].xaxis.set_minor_locator(dates.SecondLocator(interval=10)) labelx = axes[i].get_xticklabels() plt.setp(labelx, rotation=30, fontsize=9) Ymax = min(max(trace.data) * 1.01, int(options.ampmax)) # 5000 max print "Ymax=", Ymax ##ystep=max ((int(Ymax/1000)+1)*1000/2,100) ##print "Ystep=",ystep ##Ymaxunit=len(str(int(Ymax))) ##Ymaxsize=str(int(Ymax))[0] #ystep=int(Ymaxsize)*pow(10,(int(Ymaxunit)-1)) ##print "Ystep=",ystep #axes[i].set_ylim(-Ymax,Ymax) #loc = ticker.MultipleLocator(base=ystep) # this locator puts ticks at regular intervals #axes[i].yaxis.set_major_locator(loc) labely = axes[i].get_yticklabels() plt.setp(labely, fontsize=9) #Ymax1=min (max(alltraces[0].data)*1.01,int(options.ampmax)) # 5000 max #Ymax2=max(alltraces[1].data)*1.01 #Ymax3=min (max(alltraces[2].data)*1.01,int(options.ampmax)) # 5000 max #print Ymax1, Ymax2, Ymax3 #Ymax=Ymax2 #ystep=max ((int(Ymax/1000)+1)*1000/2,100) #Ymaxunit=len(str(int(Ymax))) #Ymaxsize=str(int(Ymax))[0] #ystep=int(Ymaxsize)*pow(10,(int(Ymaxunit)-1)) #axes[i].set_ylim(-Ymax,Ymax) #loc = ticker.MultipleLocator(base=ystep) # this locator puts ticks at regular intervals #axes[i].yaxis.set_major_locator(loc) #labely = axes[i].get_yticklabels() #plt.setp(labely, fontsize=9) arrivals = model.get_travel_times( source_depth_in_km=ev.depth, distance_in_degree=allsta[tr.station].epidist_deg, phase_list=phaseslist) arrtimes[tr.station] = [] for arr in arrivals: arrtimes[tr.station].append( (arr.name, datetime.fromtimestamp(ev.oritime + arr.time + 0.05))) phases_done = [] for pick in arrtimes[tr.station]: if (pick[0] in phases_done): continue phases_done.append(pick[0]) phase_pick = pick[1] x = [phase_pick, phase_pick] y = [-Ymax, 0] axes[i].plot(x, y) offsetx = timedelta(milliseconds=(NEWXLIM[1] - NEWXLIM[0]) * 1000 * 0.01) axes[i].text(phase_pick + offsetx, -Ymax, pick[0], style='normal', bbox={ 'facecolor': 'lightblue', 'alpha': 0.8, 'pad': 4 }, fontsize=8) streamcode = "%s_%s:%s:%s" % (tr.network, tr.station, tr.location, tr.channel) axes[i].set_title( "%s (%.1f degrees ; %d km ; Azim %d) - Filter [%.1f-%.1f Hz]" % (streamcode, allsta[tr.station].epidist_deg, int(allsta[tr.station].epidist_km), allsta[tr.station].azimuth, float(options.freqmin), float(options.freqmax)), fontsize=9) i = i + 1 fig2.suptitle(mytitle, fontsize=11) fig2.tight_layout(pad=0.5, rect=(0, 0, 1, 0.95)) png = "%s/%d.png" % (DATADIR, ev.evid) plt.savefig(png) plt.show()
def genericplot(df, column, outfile, config, device_name): logger = logging.getLogger(__name__) timeframe = config["timeframe"] outfile = outfile.replace(":", ".") logging.info(f"creating {outfile} for column {column}") dim = (16, 6) markersize = 1 style = "-" marker = "" colormapName = "Set1" plt.style.use("seaborn-whitegrid") palette = plt.get_cmap(colormapName) colour = palette(1) # Is this a numeric column? try: column_type = str(df[column].dtype) # logger.info(f"Column type: {column_type}") except AttributeError: column_type = "unknown" if column_type == "float64" or column_type == "int64": pass else: # TryEuropean df[column] = [x.replace(",", ".") for x in df[column]] df[column] = df[column].astype(float) try: dim = parse_tuple("(" + config["plotting"]["dim"] + ")") except KeyError: pass try: markersize = float(config["plotting"]["markersize"]) except KeyError: pass try: style = config["plotting"]["style"] except KeyError: pass if style == "": marker = "o" # Defaults or override with config file fig, ax = plt.subplots(figsize=dim, dpi=80, facecolor="w", edgecolor="dimgrey") if timeframe is not None: ax.plot( df[column][timeframe.split(",")[0] : timeframe.split(",")[1]], alpha=0.7, color=colour, linestyle=style, markersize=markersize, marker=marker, ) else: ax.plot( df[column], alpha=0.7, color=colour, linestyle=style, markersize=markersize, marker=marker, ) plt.grid(which="both", axis="both", linestyle="--") # vmstat make chart top "100" if column == "us" or column == "sy" or column == "wa" or column == "Total CPU": ax.set_ylim(top=100) if "%" in column: ax.set_ylim(top=100) # y axis ax.get_yaxis().set_major_formatter( plt.FuncFormatter(lambda x, loc: "{:,}".format(float(x))) ) ax.set_ylim(bottom=0) # Always zero start # print('max {:,.4f}'.format(df[column].max())) if df[column].max() > 10 or "%" in column: ax.yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter("{x:,.0f}")) elif df[column].max() < 0.002: ax.yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter("{x:,.4f}")) else: ax.yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter("{x:,.3f}")) # if df[column].max() > 999: # ax.yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter("{x:,.0f}")) # else: # ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=None)) # ax.get_yaxis().get_major_formatter().set_scientific(False) # Try to be smarter with the x axis. more to come if timeframe is not None and timeframe != "": if df[column][timeframe.split(",")[0] : timeframe.split(",")[1]].max() <= 10: ax.yaxis.set_major_formatter( matplotlib.ticker.StrMethodFormatter("{x:,.3f}") ) StartTime = datetime.strptime(timeframe.split(",")[0], "%Y-%m-%d %H:%M:%S") EndTime = datetime.strptime(timeframe.split(",")[-1], "%Y-%m-%d %H:%M:%S") TotalMinutes = (EndTime - StartTime).total_seconds() / 60 logging.debug("TF Minutes: " + str(TotalMinutes)) else: StartTime = df.index[0] EndTime = df.index[-1] # if the data wraps around (usually a few minutes where we have created the datetime artificially) if StartTime > EndTime: logger.debug(f"Wrapping dates {str(StartTime)} {str(EndTime)}") df = df.sort_index() StartTime = df.index[0] EndTime = df.index[-1] logger.debug(f"Sorted dates {str(StartTime)} {str(EndTime)}") # Compare to previous value # logger.info(f"{df.iloc[:,[0]].lt(df.iloc[:,[0]].shift())}") TotalMinutes = (df.index[-1] - df.index[0]).total_seconds() / 60 # logging.info("All Minutes: " + str(TotalMinutes)) if TotalMinutes <= 15: ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S")) ax.xaxis.set_major_locator( mdates.SecondLocator(interval=int((TotalMinutes * 60) / 10)) ) elif TotalMinutes <= 180: ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax.xaxis.set_major_locator( mdates.MinuteLocator(interval=int(TotalMinutes / 10)) ) elif TotalMinutes <= 1500: ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax.xaxis.set_major_locator(mdates.HourLocator()) elif TotalMinutes <= 3000: ax.xaxis.set_major_formatter(mdates.DateFormatter("%d-%H:%M")) else: ax.xaxis.set_major_formatter(mdates.DateFormatter("%a %m/%d - %H:%M")) StartTimeStr = datetime.strftime(StartTime, "%a %Y-%m-%d %H:%M:%S") EndTimeStr = datetime.strftime(EndTime, "%a %Y-%m-%d %H:%M:%S") if device_name == "": plt.title( column + " between " + StartTimeStr + " and " + EndTimeStr, fontsize=12 ) else: plt.title( device_name + " : " + column + " between " + StartTimeStr + " and " + EndTimeStr, fontsize=12, ) # plt.xlabel("Time", fontsize=10) plt.tick_params(labelsize=10) plt.setp(ax.get_xticklabels(), rotation=45, ha="right") plt.tight_layout() plt.savefig(outfile, bbox_inches="tight") plt.close()
# Plot the fit current_date = hilt_filtered.index[0].floor('d') time_seconds = (hilt_filtered.index - current_date).total_seconds() popt = row.loc[['A', 't0', 'fwhm', 'y-int', 'slope']] popt[1] = (popt[1] - current_date).total_seconds() popt[2] = popt[2] / 2.355 # Convert the Gaussian FWHM to std y = SAMPEX_Microburst_Widths.gaus_lin_function(time_seconds, *popt) ax_i.plot(hilt_filtered.index, (count_rate_conversion / yaxis_scale_factor) * y, c='r', ls='--') # Format the time axis as seconds. ax_i.xaxis.set_minor_locator(mdates.MicrosecondLocator(interval=100_000)) ax_i.xaxis.set_major_locator(mdates.SecondLocator()) # Get the axis ticks first prev_ticks = mdates.num2date(ax_i.get_xticks()) prev_ticks = [tick_i.replace(tzinfo=None) for tick_i in prev_ticks] sec_ticks = [ int((tick_i - start_time_sec_floor).total_seconds()) for tick_i in prev_ticks ] ax_i.set_xticklabels(sec_ticks) # Set the yticks to be at integer values. ax_i.yaxis.set_major_locator(ticker.MaxNLocator(integer=True)) annotate_str = ( f'({label_i})\n' f'FWHM = {round(row.fwhm*1000)} [ms]\n' r'$\bar{{R}}^2$ = {}'.format(round(row.adj_r2, 2))
ax.plot((start, stop), (0, 0), 'k', alpha=.5) # Iterate through releases annotating each one for ii, (iname, idate) in enumerate(zip(names, dates)): level = levels[ii % 6] vert = 'top' if level < 0 else 'bottom' ax.scatter(idate, 0, s=100, facecolor='w', edgecolor='k', zorder=9999) # Plot a line up to the text ax.plot((idate, idate), (0, level), c='r', alpha=.7) # Give the text a faint background and align it properly ax.text(idate, level, iname, horizontalalignment='right', verticalalignment=vert, fontsize=14, backgroundcolor=(1., 1., 1., .3)) ax.set(title="Matplotlib release dates") # Set the xticks formatting # format xaxis with 3 month intervals ax.get_xaxis().set_major_locator(mdates.SecondLocator(interval=3)) ax.get_xaxis().set_major_formatter(mdates.DateFormatter("%M %S")) fig.autofmt_xdate() # Remove components for a cleaner look plt.setp( (ax.get_yticklabels() + ax.get_yticklines() + list(ax.spines.values())), visible=False) plt.show()
def subplot_all(self, cols, plot=False, grain=False): plot_title, save_title = self._get_titles() hours = mdates.HourLocator() # every year mins = mdates.MinuteLocator() # every month secs = mdates.SecondLocator() # every month if len(cols) == 2: # row and column sharing ax1 = plt.subplot(211) self._set_subplots_title_and_plot( ax1, 'epoch', self._select_plot_columns(cols[0]), cols[0]) self._set_subplots_time(ax=ax1, hours=hours, mins=mins, secs=secs, grain=grain) ax1.set_ylabel(self._set_unit(cols[0])) # plt.setp(ax1.get_xticklabels(), rotation=30, horizontalalignment='right') ax1.xaxis.set_tick_params(which="major", pad=15) ax1.grid(True) ax1.legend(loc="upper left", bbox_to_anchor=[1, 1], shadow=True, fancybox=True) ax2 = plt.subplot(212) self._set_subplots_title_and_plot( ax2, 'epoch', self._select_plot_columns(cols[1]), cols[1]) self._set_subplots_time(ax=ax2, hours=hours, mins=mins, secs=secs, grain=grain) ax2.set_ylabel(self._set_unit(cols[1])) ax2.set_xlabel('time') # plt.setp(ax2.get_xticklabels(), rotation=30, horizontalalignment='right') ax2.xaxis.set_tick_params(which="major", pad=15) ax2.grid(True) ax2.legend(loc="upper left", bbox_to_anchor=[1, 1], shadow=True, fancybox=True) else: ax1 = plt.subplot(311) self._set_subplots_title_and_plot( ax1, 'epoch', self._select_plot_columns(cols[0]), cols[0]) ax1.grid(True) self._set_subplots_time(ax=ax1, hours=hours, mins=mins, secs=secs, grain=grain) ax1.set_ylabel(self._set_unit(cols[0])) ax1.xaxis.set_tick_params(which="major", pad=15) ax1.legend(loc="upper left", bbox_to_anchor=[1, 1], shadow=True, fancybox=True) ax2 = plt.subplot(312) self._set_subplots_title_and_plot( ax2, 'epoch', self._select_plot_columns(cols[1]), cols[1]) ax2.grid(True) self._set_subplots_time(ax=ax2, hours=hours, mins=mins, secs=secs, grain=grain) ax2.set_ylabel(self._set_unit(cols[1])) ax2.xaxis.set_tick_params(which="major", pad=15) ax2.legend(loc="upper left", bbox_to_anchor=[1, 1], shadow=True, fancybox=True) ax3 = plt.subplot(313) self._set_subplots_title_and_plot( ax3, 'epoch', self._select_plot_columns(cols[2]), cols[2]) ax3.grid(True) self._set_subplots_time(ax=ax3, hours=hours, mins=mins, secs=secs, grain=grain) ax3.set_ylabel(self._set_unit(cols[2])) ax3.xaxis.set_tick_params(which="major", pad=15) ax3.set_xlabel('time') ax3.legend(loc="upper left", bbox_to_anchor=[1, 1], shadow=True, fancybox=True) plt.gcf().autofmt_xdate() if plot: plt.show() else: self.save(save_title + "subplots") plt.close()
def plot_spectro(file, t0plot, t1plot, downsample=1): #--------------------------------------# # Note downsampling allows a bigger time range to be read into RAM. # But it increases the computation time significantly. On an average # machine it may be best to keep full sampling (default), but limit # your plot to 15 minute blocks. runtime0 = datetime.now() f = h5py.File(file, 'r') t_lines = np.shape(f['SUB_ARRAY_POINTING_000/BEAM_000/STOKES_0'])[0] f_lines = np.shape(f['SUB_ARRAY_POINTING_000/BEAM_000/STOKES_0'])[1] target = f.attrs['TARGETS'][0] print('Target: %s' % (target)) #---------------------------------# # Sort out the time arrays # obs_start = f.attrs['OBSERVATION_START_UTC'].decode("utf-8") obs_stop = f.attrs['OBSERVATION_END_UTC'].decode("utf-8") obs_start = datetime.strptime(obs_start[0:-4], "%Y-%m-%dT%H:%M:%S.%f") obs_stop = datetime.strptime(obs_stop[0:-4], "%Y-%m-%dT%H:%M:%S.%f") total_duration = f.attrs['TOTAL_INTEGRATION_TIME'] #in seconds tres = total_duration / t_lines t0sec = t0plot - obs_start t1sec = t1plot - obs_start t0index = int(t0sec.seconds / tres) t1index = int(t1sec.seconds / tres) print('Observation start time: %s' % (obs_start)) print('Observation stop time: %s' % (obs_stop)) print('Total observation duration %s seconds.' % (total_duration)) print('Time resolution: %s seconds.' % (tres * downsample)) tim_mpl = [ dates.date2num(obs_start + timedelta(seconds=tres * (i + t0index))) for i in range(t1index - t0index) ] tim_mpl = tim_mpl[::downsample] #----------------------------------# # Sort out the frequency arrays # start_freq = f.attrs['OBSERVATION_FREQUENCY_MIN'] #in MHz end_freq = f.attrs['OBSERVATION_FREQUENCY_MAX'] fres = (end_freq - start_freq) / f_lines #in MHz freq = np.linspace(start_freq, end_freq, f_lines) print('Frequency resolution: %s MHz.' % (fres * downsample)) #-------------------------------------# # Downsample and background subtract # gigabytes = 4 * f_lines * ( t1index - t0index) / downsample / 1e9 # Data is 32 bit (4 byte). print('Reading in %s GB of data.' % (gigabytes)) #import pdb #pdb.set_trace() data = f['SUB_ARRAY_POINTING_000/BEAM_000/STOKES_0'][ t0index:t1index:downsample, ::downsample] data = np.transpose(data) data = backsub(data) #----------------------------------# # High pass filter # #data_lp = ndimage.gaussian_filter(data, sigma=(10, 10)) #data = data - data_lp xyshape = np.shape(data) print('Array size: %s Mpixels.' % (xyshape[0] * xyshape[1] / 1e6)) #----------------------------------# # Plot the spectrogram # plt.figure(0, figsize=(12, 6)) imshow(data[::-1, ::], aspect='auto', extent=(tim_mpl[0], tim_mpl[-1], start_freq, end_freq), cmap=plt.get_cmap('plasma'), vmin=np.percentile(data, 30.0), vmax=np.percentile(data, 97.9)) yymmdd = f.attrs['OBSERVATION_END_UTC'][0:10] xlabel('Time (UT)') ylabel('Frequency (MHz)') title('LOFAR %s Beam 000 %s' % (yymmdd, target)) np.save('dynamic_spec_hba.npy', data) extent = [tim_mpl[0], tim_mpl[-1], end_freq, start_freq] np.save('extent_hba.npy', extent) ax = plt.gca() ax.set_yscale('log') ax.xaxis_date() ax.xaxis.set_major_locator(dates.MinuteLocator(interval=5)) ax.xaxis.set_minor_locator(dates.SecondLocator(interval=60)) ax.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')) runtime1 = datetime.now() runtime = runtime1 - runtime0 print('Execution time: %s seconds' % (runtime.seconds)) plt.show()
def create_plot( *, title: str, x_attr: str, y_attr: str, stats: list, additional_plots: typing.Optional[typing.Sequence[dict]] = None, legend: typing.Optional[typing.Sequence[str]] = None) -> io.BytesIO: # init_settings_for_plt() is called before if len(stats) <= 2: marker = 'o' else: marker = None x = tuple(getattr(item, x_attr) for item in stats) y = tuple(round(getattr(item, y_attr), 1) for item in stats) fig, ax = plt.subplots(figsize=( 12, 8, )) ax.plot(x, y, marker=marker) if additional_plots is not None: for additional_plot in additional_plots: x_ = tuple( getattr(item, additional_plot['x_attr']) for item in additional_plot['stats']) y_ = tuple( round(getattr(item, additional_plot['y_attr']), 1) for item in additional_plot['stats']) ax.plot(x_, y_, marker=marker) if legend is not None: ax.legend(legend) if isinstance(x[0], ( datetime.date, datetime.datetime, )): if len(x) > 1: diff = abs(x[0] - x[-1]) postfix = f'({x[0].strftime("%H:%M:%S %d.%m.%y")} - {x[-1].strftime("%H:%M:%S %d.%m.%y")})' else: diff = datetime.timedelta(seconds=1) postfix = '' if diff < datetime.timedelta(seconds=10): ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S')) ax.xaxis.set_major_locator(mdates.SecondLocator(interval=1)) plt.xlabel(f'Time {postfix}') elif diff < datetime.timedelta(minutes=20): ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=1)) plt.xlabel(f'Time {postfix}') elif diff <= datetime.timedelta(hours=24): ax.xaxis.set_major_formatter(DateFormatter('%H')) ax.xaxis.set_major_locator(mdates.HourLocator(interval=1)) plt.xlabel(f'Hours {postfix}') elif diff < datetime.timedelta(days=30): ax.xaxis.set_major_formatter(DateFormatter('%d')) ax.xaxis.set_major_locator(mdates.DayLocator(interval=1)) plt.xlabel(f'Days {postfix}') elif diff < datetime.timedelta(days=30 * 15): ax.xaxis.set_major_formatter(DateFormatter('%m')) ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1)) plt.xlabel(f'Months {postfix}') else: ax.xaxis.set_major_formatter(DateFormatter('%y')) ax.xaxis.set_major_locator(mdates.YearLocator()) plt.xlabel(f'Years {postfix}') else: ax.xaxis.set_major_locator(AutoLocator()) if all( isinstance(i, int) or isinstance(i, float) and i.is_integer() for i in y): ax.yaxis.set_major_locator(MaxNLocator(integer=True)) ax.set_title(title) with tempfile.TemporaryDirectory() as temp_dir_name: image_name = os.path.join(temp_dir_name, f'{title}.png') fig.savefig(image_name) fig.clear() plt.close(fig) with open(image_name, 'rb') as image: return io.BytesIO(image.read())
def wav_spectrogram(waveforms, fmi1, fma1, fmi2, fma2, clip, yscale='linear'): files = glob(waveforms) matplotlib.rcParams.update({'font.size': 16}) for f in range(len(files)): x = read(files[f]) t0 = x[0].stats.starttime t1 = x[0].stats.endtime zx = x.select(component='Z') zx = zx.slice(starttime=t0, endtime=t1) for i in range(len(zx)): b = zx[i] a = b.copy() station = a.stats.station ac = a.copy() ac.detrend(type='demean') ac.taper(max_percentage=0.005, type='hann', side='both') #max_lenght=None ac.filter('bandpass', freqmin=fmi1, freqmax=fma1, corners=2, zerophase='True') af = a.copy() af.detrend(type='demean') af.taper(max_percentage=0.005, type='hann', side='both') #max_lenght=None af.filter('bandpass', freqmin=fmi2, freqmax=fma2, corners=2, zerophase='True') t0 = a.stats.starttime t1 = a.stats.endtime stat = a.stats.station #plot original filtered and spectrogram fig2 = plt.figure(figsize=(10, 8)) #[20,18]15.15 date = t0.strftime(format='%Y-%m-%d %H:%M:%S') ax1 = fig2.add_axes([0.1, 0.79, 0.8, 0.12]) #[left bottom width height] ax4 = fig2.add_axes([0.1, 0.67, 0.8, 0.12], sharex=ax1) #[left bottom width height] ax2 = fig2.add_axes([0.1, 0.04, 0.8, 0.52]) #, sharex=ax1) ax3 = fig2.add_axes([0.92, 0.04, 0.03, 0.52]) #ax1.set_title(str(stat) + '\n' + date + '\n', fontsize=16, fontweight='bold') #insert dates start = date2num(t0.datetime) end = date2num(t1.datetime) #make time vector t = np.linspace(start, end, a.stats.npts) #plot waveform original (top subfigure) ax1.plot_date(t, ac.data, 'g', label=station + ': ' + str(fmi1) + '-' + str(fma1) + 'Hz') ax1.xaxis.set_major_locator(dates.MinuteLocator(interval=1)) ax1.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')) ax1.xaxis.set_minor_locator(dates.SecondLocator(interval=2)) ax1.set_ylabel("Amplitude", fontsize=18) plt.setp(ax1.get_xticklabels(), visible=0) #hide tick text but shows tick marks ax1.legend(loc="upper right", prop={ 'size': 16, 'weight': 'bold' }, framealpha=1.0, bbox_to_anchor=(1.0, 1.15), frameon=False) #max_Y=max(abs(ac.data)) #ax1.set_yticks(np.linspace(-max_Y, max_Y, 3)) #plot filtered wav (2nd figure) ax4.plot_date(t, af.data, 'r', label=station + ': ' + str(fmi2) + '-' + str(fma2) + 'Hz') ax1.locator_params(nbins=3) ax4.locator_params(nbins=3) ax4.set_xlabel("time [HH:MM:SS]", fontsize=18) ax4.yaxis.set_ticks_position('right') ax4.yaxis.set_label_position('right') ax4.set_ylabel("Amplitude", fontsize=18) ax4.legend(loc="upper right", prop={ 'size': 16, 'weight': 'bold' }, framealpha=1.0, bbox_to_anchor=(1.0, 1.15), frameon=False) #increase size ax4.set_xticks(np.linspace(start, end, 6)[:-1]) #max_Y=max(abs(af.data)) #ax4.set_yticks(np.linspace(-max_Y, max_Y, 3)) ###### plot spectrogram (bottom subfigure) name = str(t0)[0:10] + '_' + stat fig2 = a.spectrogram(log=True, show=False, axes=ax2, clip=[0, clip], cmap=plt.cm.jet) mappable = ax2.collections[ 0] #for logaritmic scale on spectrogram function #mappable = ax2.images[0] #for linear scale on spectrogram function plt.colorbar(mappable=mappable, cax=ax3) ax2.set_ylabel("Frequency [Hz]", fontsize=18) ax2.set_yscale(yscale) #linear #log ax2.tick_params(axis='x', color='white') ax2.tick_params(axis='y', which='minor', color='white') ax2.set_xlabel('time after ' + str(date) + ' [s]', fontsize=18) ax2.set_title("All Spectrogram", fontsize=18) #subplots_adjust(top=10,bottom=5) plt.savefig(str(t0)[0:16] + '_' + str(stat) + '_spectrogram_' + yscale + '.png', bbox_inches='tight') plt.clf() plt.close('all')
def plotSpectrum(self, option=3, xtick=2, blevel=0, endpts=[False, False], cmap=cm.jet, cbar=True, cbar_ori='vertical', fontsize=14): #plot the fig fig, ax = plt.subplots(figsize=(7, 7)) if option == 1: y, x = self.imagehdu.data.shape cax = ax.imshow(self.imagehdu.data, extent=[0, x, 0, y], aspect='auto', cmap=cmap, vmin=blevel) if cbar == True: ticks = list( np.linspace( blevel, self.datamax, 10).astype('int')) #calculate 10 ticks positins if cbar_ori == 'horizontal': cbar = fig.colorbar(cax, ticks=ticks, orientation='horizontal') else: cbar = fig.colorbar(cax, ticks=ticks) cbar.set_label('Intensity', rotation=90) plt.xlabel('Row Count') plt.ylabel('Column Count') if option == 2: xstart = int(self.imageheader['CRVAL1']) xstep = float(self.imageheader['CDELT1']) xlength = int(self.imageheader['NAXIS1']) xend = xstart + xstep * xlength freqs = self.bintablehdu.data['frequency'][ 0] # these are true frequencies cax = ax.imshow(self.imagehdu.data, extent=[xstart, xend, freqs[-1], freqs[0]], aspect='auto', cmap=cmap, vmin=blevel) if cbar == True: ticks = list( np.linspace( blevel, self.datamax, 10).astype('int')) #calculate 10 ticks positins if cbar_ori == 'horizontal': cbar = fig.colorbar(cax, ticks=ticks, orientation='horizontal') else: cbar = fig.colorbar(cax, ticks=ticks) cbar.set_label('Intensity', rotation=90) if endpts[1]: #set y minorticks for first and last frequency y_lims = [freqs[-1], freqs[0]] #print(y_lims) plt.yticks(list(plt.yticks()[0]) + y_lims) plt.xlabel('Time (sec of day)') plt.ylabel('Frequency [MHz]') if option == 3: # get the start time and end time of observation start_date = utils.toDate(self.imageheader['DATE-OBS']) starttime = utils.toTime(self.imageheader['TIME-OBS']) starttime = dt.datetime.combine( start_date, starttime) # make a datetime object endtime = utils.toTime(self.imageheader['TIME-END']) endtime = dt.datetime.combine(start_date, endtime) # get the frequencies freqs = self.bintablehdu.data['frequency'][ 0] # these are true frequencies # set the limits for plotting x_lims = [starttime, endtime] x_lims = mdates.date2num(x_lims) # dates to numeric values y_lims = [freqs[-1], freqs[0]] cax = ax.imshow( self.imagehdu.data, extent=[x_lims[0], x_lims[1], y_lims[0], y_lims[1]], aspect='auto', cmap=cmap, vmin=blevel) if cbar == True: ticks = list( np.linspace( blevel, self.datamax, 10).astype('int')) #calculate 10 ticks positins if cbar_ori == 'horizontal': cbar = fig.colorbar(cax, ticks=ticks, orientation='horizontal') else: cbar = fig.colorbar(cax, ticks=ticks) cbar.set_label('Intensity', rotation=90) ax.xaxis_date() # x axis has a date data ax.xaxis.set_major_locator( mdates.MinuteLocator(byminute=range(60), interval=xtick, tz=None)) ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S')) #set ytick labels to be intigers only, in case we have a small frequency range, pyplot tends to show fraction ax.get_yaxis().get_major_formatter().set_useOffset(False) if endpts[0]: #get the start time and end time ans set ticks at those position son x-axis using minor_locator total_sec = utils.tosec(endtime - starttime) ax.xaxis.set_minor_locator( mdates.SecondLocator(bysecond=None, interval=total_sec, tz=None)) ax.xaxis.set_minor_formatter(DateFormatter('%H:%M:%S')) fig.autofmt_xdate() if endpts[1]: #set y minorticks for first and last frequency plt.yticks(list(plt.yticks()[0]) + y_lims) #ax.xaxis.set_minor_formatter(DateFormatter('%H:%M:%S')) plt.xlabel('Universal Time') plt.ylabel('Frequency [MHz]') #title = fitsfile + "\n" + imageheader['DATE-OBS'] + "::" + imageheader['TIME-OBS'] title = self.imageheader['CONTENT'] plt.title(title) #plt.show() return plt
def run_with_string_diagram(sys, sys_dos, start_time, end_time): '''To draw the string diagram based on the schedule dictionary for all the trains. ''' colors = ['red', 'green', 'blue', 'black', 'orange', 'cyan', 'magenta'] color_num = len(colors) x, y = process_data(sys) x_dos, y_dos = process_data(sys_dos) t_color = [colors[x.index(i) % color_num] for i in x] # plt.ion() plt.title('Stringline Diagram', size=35) hours = mdates.HourLocator() minutes = mdates.MinuteLocator() seconds = mdates.SecondLocator() dateFmt = mdates.DateFormatter("%H:%M") plt.gca().xaxis.set_major_locator(hours) plt.gca().xaxis.set_minor_locator(minutes) plt.gca().xaxis.set_major_formatter(dateFmt) plt.xticks(rotation=90) plt.grid(color="black") plt.legend() plt.xlabel('Time', size=30) plt.ylabel('Mile Post/miles', size=30) plt.tick_params(labelsize=23) start_time = int(start_time.timestamp()) end_time = int(end_time.timestamp()) sys_length = sys.block_intervals[-1][1] multi_track_blk_intervals = [] for i, blk in enumerate(sys.blocks): if blk.track_number > 1: multi_track_blk_intervals.append(sys.block_intervals[i]) dos_period = sys_dos.dos_period dos_interval = sys_dos.block_intervals[sys_dos.dos_pos] dos_period_ratio = [(dos_period[0] - start_time) / (end_time - start_time), (dos_period[1] - start_time) / (end_time - start_time)] plt.axis([(datetime.fromtimestamp(start_time)), (datetime.fromtimestamp(end_time)), 0, sys_length]) plt.gca().axhspan(15, 20, color='yellow', alpha=0.5) plt.gca().axhspan(30, 35, color='yellow', alpha=0.5) #plt.gca().axvspan((datetime.fromtimestamp(start_time + 90 * 60)),(datetime.fromtimestamp(start_time + 150 * 60)),color='black',alpha=0.5) labels, label_colors = ['Siding Location'], ['yellow'] # 用label和color列表生成mpatches.Patch对象,它将作为句柄来生成legend patches = [ mpatches.Patch(color=label_colors[i], label="{:s}".format(labels[i])) for i in range(len(label_colors)) ] ax = plt.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width, box.height * 0.8]) # 下面一行中bbox_to_anchor指定了legend的位置 ax.legend(handles=patches, bbox_to_anchor=(0.85, 0.94), ncol=1) # 生成legend for n in range(len(x) - 1): # #assert len(x[n]) == len(y[n]) == t_color[n] plt.plot([mdates.date2num(i) for i in x[n]], y[n], color=t_color[n], alpha=0.5) # for n in range(len(x_dos) - 1): # plt.plot([mdates.date2num(i) for i in x_dos[n]], y_dos[n], color=t_color[n]) plt.gca().axhspan(dos_interval[0], dos_interval[1], dos_period_ratio[0], dos_period_ratio[1], color='blue', alpha=0.5) for mtbi in multi_track_blk_intervals: plt.gca().axhspan(mtbi[0], mtbi[1], color='yellow', alpha=0.5) plt.show()
def plot_tog(): fig = plt.figure(figsize=(7, 10)) gss = gridspec.GridSpec(nrows=3, ncols=1, height_ratios=[1, 2,2]) ax1 = plt.subplot(gss[0]) ax2 = plt.subplot(gss[1]) ax3 = plt.subplot(gss[2], sharex=ax2) ax1.plot(gl, color='r', label='GOES 1-8$\mathrm{\AA}$') ax1.plot(gs, color='b', label='GOES 1-8$\mathrm{\AA}$') ax1.set_yscale('log') ax1.set_xlim(flare_ts, flare_te) ax1.xaxis.set_minor_locator(dates.SecondLocator(interval=10)) ax1.xaxis.set_major_locator(dates.MinuteLocator(interval=1)) ax1.legend(loc='upper right') ax1.tick_params(labelbottom=False, which='both', direction='in') ax1.set_ylabel('Flux (Wm$^{-2}$)') ax1.axvline(pul_ts, color='k') ax1.axvline(pul_te, color='k') ax2.plot(norp17, label='NoRP 17GHz', color='darkred', drawstyle='steps-mid') ax2.plot(norp9, label='NoRP 9GHz', color='darkblue', drawstyle='steps-mid') ax2.legend(loc='upper right') ax2.set_ylabel('Flux (SFU)') ax2.set_ylim(0, 200) x1 = dates.date2num(datetime.datetime.strptime(pul_ts, '%Y-%m-%d %H:%M:%S')) # start time x2 = dates.date2num(datetime.datetime.strptime(pul_te, '%Y-%m-%d %H:%M:%S')) # end time xyA = (x1, -0.01) xyB = (0.0, 1) # x and y in axes coordinates coordsA = ax1.get_xaxis_transform() # x in data coords, y in axes coords coordsB = "axes fraction" con_start = ConnectionPatch(xyA=xyA, xyB=xyB, coordsA=coordsA, coordsB=coordsB, axesA=ax1, axesB=ax2, arrowstyle="-") xyC = (x2, -0.01) xyD = (1, 1) # x and y in axes coordinates coordsC = ax1.get_xaxis_transform() # x in data coords, y in axes coords coordsD = "axes fraction" con_end = ConnectionPatch(xyA=xyC, xyB=xyD, coordsA=coordsC, coordsB=coordsD, axesA=ax1, axesB=ax2, arrowstyle="-") ax2.add_artist(con_start) ax2.add_artist(con_end) #ax3 = ax2.twinx() ax3.plot(rhessi_2535, drawstyle='steps-mid', label='RHESSI 25-35keV', color='grey', lw=0.8) ax3.plot(rhessi_35100, drawstyle='steps-mid', label='RHESSI 35-100keV', color='k', lw=0.8) ax3.legend(loc = 'upper right') ax3.set_ylabel('Counts det$^{-1}$ s$^{-1}$') ax3.set_xlabel('Time (UT) 2014-06-11') ax3.set_xlim(pul_ts, pul_te) ax2.xaxis.set_minor_locator(dates.SecondLocator(interval=10)) ax2.xaxis.set_major_locator(dates.MinuteLocator(interval=1)) ax2.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')) ax2.tick_params(which='both', direction='in', labelbottom=False) #ax3.xaxis.set_tick_params(rotation=45, which='both', direction='in') ax3.xaxis.set_tick_params(which='both', direction='in') plt.tight_layout() plt.subplots_adjust(hspace=0.1) plt.savefig('overview_test.png', dpi=200) plt.close()
def _build_execution_progress_plot( df: pd.DataFrame, plot_parameters: Dict) -> Tuple[str, Figure]: title = plot_parameters["title"] datetime_range = plot_parameters.get("datetime_range") fig, ax = plt.subplots(figsize=(8, 4), dpi=200, constrained_layout=True) collected_date = df["collected_date"].iloc[0] collected_datetime = datetime.strptime(collected_date, DATETIME_FORMAT) for index, row in df.iterrows(): workflow_number = row["workflow_number"] workflow_status = row["status"] created_date = datetime.strptime(row["created"], DATETIME_FORMAT) asked_to_start_date_exists = not pd.isna(row["asked_to_start_date"]) started_exists = not pd.isna(row["started"]) ended_exists = not pd.isna(row["ended"]) # add created point, should always exist ax.plot( created_date, workflow_number, ".", markerfacecolor=STATUS_TO_COLOR[WorkflowStatus.created], markersize=3, color=STATUS_TO_COLOR[WorkflowStatus.created], label="1-created", ) if asked_to_start_date_exists: asked_to_start_date = datetime.strptime(row["asked_to_start_date"], DATETIME_FORMAT) # add asked to start point ax.plot( asked_to_start_date, workflow_number, ".", markerfacecolor="navy", markersize=3, color="navy", label="2-asked to start", ) if started_exists: started_date = datetime.strptime(row["started"], DATETIME_FORMAT) # add started point ax.plot( started_date, workflow_number, ".", markerfacecolor="sienna", markersize=3, color="sienna", label="4-started", ) if ended_exists: ended_date = datetime.strptime(row["ended"], DATETIME_FORMAT) # add ended point to indicate whatever workflow finished or failed ax.plot( ended_date, workflow_number, ".", markerfacecolor=STATUS_TO_COLOR[workflow_status], markersize=4 if workflow_status == WorkflowStatus.failed else 3, # zorder, acts similar to z-index zorder=10 if workflow_status == WorkflowStatus.failed else 5, color=STATUS_TO_COLOR[workflow_status], label=f"6-{workflow_status}", ) else: ended_date = collected_datetime # draw running line ax.hlines( workflow_number, xmin=started_date, xmax=ended_date, colors=[STATUS_TO_COLOR[WorkflowStatus.running]], label="5-running", ) else: started_date = collected_datetime # add pending line ax.hlines( workflow_number, xmin=asked_to_start_date, xmax=started_date, colors=["orange"], label="3-pending", ) # force integers on y axis ax.yaxis.set_major_locator(MaxNLocator(integer=True)) interval = plot_parameters.get("time_interval") if interval: ax.xaxis.set_major_locator(mdates.SecondLocator(interval=interval)) ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S")) # rotate dates on x axis plt.setp(ax.get_xticklabels(), rotation=45, ha="right") ax.set(title=title) ax.set(ylabel="workflow run") ax.set_ylim(ymin=df["workflow_number"].min() - 1) if datetime_range: left = datetime.strptime(datetime_range[0], DATETIME_FORMAT) right = datetime.strptime(datetime_range[1], DATETIME_FORMAT) ax.set_xlim(left=left, right=right) ax.grid(color="black", linestyle="--", alpha=0.15) def _build_legend(axes): # remove labels duplicates # origin - https://stackoverflow.com/a/56253636 handles, labels = axes.get_legend_handles_labels() unique = [(h, l) for i, (h, l) in enumerate(zip(handles, labels)) if l not in labels[:i]] # sort labels in order of leading number in value if label (e.g 1-created) unique.sort(key=lambda x: x[1]) # remove leading number from label unique = [(h, l.split("-")[1]) for h, l in unique] legends = ax.legend(*zip(*unique), loc="upper left") # increase size of points on the legend to be more visible for handler in legends.legendHandles: if hasattr(handler, "_legmarker"): handler._legmarker.set_markersize(6) _build_legend(ax) return "execution_progress", fig
def plot_launches(filename, type): f = open(filename, 'r') dates = [] times = [] launches = [] distances = [] for line in f: data = line.rstrip("\n").split(" ") dates.append(data[0]) launches.append(float(data[1])) times.append(float(data[2])) distance = float(data[3]) - MARS_RADIUS if distance < 0: distance = 0 distances.append(distance) f.close() min_index = np.argmin(distances) print("Minimum distance to Mars achieved:") print("\tLaunch Day:", (INITIAL_DATE + datetime.timedelta(seconds=launches[min_index]) ).strftime('%Y-%m-%d %H:%M:%S'), "(" + str(launches[min_index]) + "[s])") print("\tArrival day:", (INITIAL_DATE + datetime.timedelta(seconds=times[min_index]) ).strftime('%Y-%m-%d %H:%M:%S') + "[s]") print("\tTime of Flight:", str(times[min_index]) + "[s]") print("\tDistance to Mars:", str(distances[min_index]) + "[km]") if type == TYPE_MINUTE or type == TYPE_SECOND or type == TYPE_HOUR: date_index = 0 x_values = [] for i in range(len(launches)): sub_date = INITIAL_DATE + datetime.timedelta(seconds=launches[i]) x_values.append(sub_date) y_values = distances elif type == TYPE_DAY: x_values = [ datetime.datetime.strptime(d, "%Y-%m-%d").date() for d in dates ] y_values = distances elif type == TYPE_WEEK: x_values = [ datetime.datetime.strptime(d, "%Y-%m-%d").date() for d in dates ] y_values = distances ax = plt.gca() formatter = mdates.DateFormatter("%d-%m-%Y") ax.xaxis.set_major_formatter(formatter) locator = mdates.DayLocator() ax.xaxis.set_major_locator(locator) plt.scatter(x_values, y_values, color="red") # Removes the scientific notation on top # https://stackoverflow.com/questions/28371674/prevent-scientific-notation-in-matplotlib-pyplot ax.ticklabel_format(style='plain', axis='y') # Format the date into months & days # Change the tick interval if type == TYPE_SECOND: interval = 15 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) plt.gca().xaxis.set_major_locator( mdates.SecondLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue [" + dates[0] + "]") elif type == TYPE_MINUTE: interval = 10 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) plt.gca().xaxis.set_major_locator( mdates.MinuteLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue [" + dates[0] + "]") elif type == TYPE_HOUR: interval = 4 plt.gca().xaxis.set_major_formatter( mdates.DateFormatter('%m-%d %H:%M')) plt.gca().xaxis.set_major_locator( mdates.HourLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue [Año " + dates[0].split("-")[0] + "]") elif type == TYPE_DAY: interval = 2 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m')) plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue [Año " + dates[0].split("-")[0] + "]") elif type == TYPE_WEEK: # https://stackoverflow.com/questions/46555819/months-as-axis-ticks interval = 2 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%Y')) plt.gca().xaxis.set_major_locator( mdates.MonthLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue") # Puts x-axis labels on an angle plt.gca().xaxis.set_tick_params(rotation=30) plt.show()
c.execute("SELECT * FROM winddate") alltime = c.fetchall() conn.close() myTime = [] mySpeed = [] # parse result from db for i in allspeed: mySpeed.append(i[1]) for j in alltime: myTime.append(parser.parse(j[1])) #years = mdates.YearLocator() # every year minutes = mdates.MinuteLocator() # every minute #months = mdates.MonthLocator() # every month seconds = mdates.SecondLocator() # every second #years_fmt = mdates.DateFormatter('%Y') minutes_fmt = mdates.DateFormatter('%M:%S') # which you would then use as: data1, data2, data3, data4 = np.random.randn(4, 100) fig, ax = plt.subplots(1, 1) # format the ticks ax.xaxis.set_major_locator(minutes) ax.xaxis.set_major_formatter(minutes_fmt) ax.xaxis.set_minor_locator(seconds) my_plotter(ax, myTime, mySpeed, {'marker': 'x'}) plt.title('Wind of Change')
def _deduce_locators_formatters(max_ticks, data): from datetime import timedelta as tdelta import matplotlib.dates as mdates from matplotlib.ticker import FuncFormatter data_interval_seconds = (data[-1] - data[0]) / tdelta(seconds=1) interval_seconds = data_interval_seconds / max_ticks if interval_seconds < tdelta(minutes=0.5).total_seconds(): # print("xticks: seconds") unit_multiple = _next_largest(interval_seconds, INTERVALS['SECONDLY']) timedelta = tdelta(seconds=unit_multiple) return (mdates.SecondLocator(bysecond=range(0, 60, unit_multiple)), FuncFormatter( _get_dynamic_formatter(timedelta, '%M%:S', '%-Hh', '%-d %b'))) elif interval_seconds < tdelta(hours=0.5).total_seconds(): # print("xticks: minutes") unit_multiple = _next_largest( interval_seconds / tdelta(minutes=1).total_seconds(), INTERVALS['MINUTELY']) timedelta = tdelta(minutes=unit_multiple) return (mdates.MinuteLocator(byminute=range(0, 60, unit_multiple)), FuncFormatter( _get_dynamic_formatter(timedelta, '%H%:M', '%-d %b', '%Y'))) elif interval_seconds < tdelta(days=0.5).total_seconds(): # print("xticks: hours") unit_multiple = _next_largest( interval_seconds / tdelta(hours=1).total_seconds(), INTERVALS['HOURLY']) timedelta = tdelta(hours=unit_multiple) return (mdates.HourLocator(byhour=range(0, 24, unit_multiple)), FuncFormatter( _get_dynamic_formatter(timedelta, '%-Hh', '%-d %b', '%Y'))) elif interval_seconds < tdelta(days=3).total_seconds(): # print("xticks: days") unit_multiple = _next_largest( interval_seconds / tdelta(days=1).total_seconds(), INTERVALS['DAILY']) timedelta = tdelta(days=unit_multiple) return (mdates.WeekdayLocator(byweekday=range(0, 7, unit_multiple)), FuncFormatter( _get_dynamic_formatter(timedelta, '%-d', '%b', '%Y'))) elif interval_seconds < tdelta(days=14).total_seconds(): # print("xticks: weeks") unit_multiple = _next_largest( interval_seconds / tdelta(weeks=1).total_seconds(), INTERVALS['WEEKLY']) timedelta = tdelta(days=unit_multiple * 7) return (mdates.WeekdayLocator(byweekday=0, interval=unit_multiple), FuncFormatter( _get_dynamic_formatter(timedelta, '%-d', '%b', '%Y'))) elif interval_seconds < tdelta(weeks=26).total_seconds(): # print("xticks: months") unit_multiple = _next_largest( interval_seconds / tdelta(weeks=4).total_seconds(), INTERVALS['MONTHLY']) timedelta = tdelta(weeks=unit_multiple * 4) return (mdates.MonthLocator(bymonth=range(1, 13, unit_multiple)), FuncFormatter(_get_dynamic_formatter(timedelta, '%b', '%Y'))) else: # print("xticks: years") unit_multiple = _next_largest( interval_seconds / tdelta(weeks=52).total_seconds(), INTERVALS['YEARLY']) return (mdates.YearLocator(base=unit_multiple), mdates.DateFormatter('%Y'))
def draw_pd(infile, outfilename): timeConverter = lambda x: datetime.strptime(x.decode('ascii'), '%Y-%m-%d %H:%M:%S.%f') contactData = np.genfromtxt(infile, delimiter='\t', dtype=None, names=['time', 'address', 'avg_rssi', 'median_rssi', 'count'], converters={'time': timeConverter}) def address_to_color(addresses): colors = ['#FF9D00', '#8000FF', '#32A08C', '#F03237', '#008CC8', 'b', 'g', 'r', 'c', 'm', 'y', 'b', 'w'] result = [] for address in addresses: result.append(colors[address]) return result def convert_addresses(addresses): result = [] for address in addresses: result.append(address-16) return result def get_distinct_sorted_addresses(addresses): result = [] for address in addresses: if address not in result: result.append(address) result.sort() return result def get_legend_labels(legend_index_list): result = [] for a in legend_index_list: result.append("Actor %d" % (a+1)) return result def get_legend_handles(addresses): result = [] colors = address_to_color(addresses) for c in colors: result.append(plt.Circle((0,0), color=c, alpha=0.5)) return result address_list=convert_addresses(contactData['address']) legend_index_list = get_distinct_sorted_addresses(address_list) labels=get_legend_labels(legend_index_list) handles=get_legend_handles(legend_index_list) colors=address_to_color(address_list) fig, ax = plt.subplots() ax.scatter(contactData['time'], contactData['median_rssi'], s=contactData['count'] * 15, c=colors, label=colors,alpha=0.5) max_time = np.amax(contactData['time'][:]) min_time = np.amin(contactData['time'][:]) x_min = min_time - timedelta(0,60 + min_time.second, min_time.microsecond - 500000) x_max = max_time + timedelta(0, 60 - max_time.second, 1000000 - max_time.microsecond ) ax.set_xlim(x_min, x_max) ax.set_ylim(-70, -100) #ax.set_title("Actor 2") ax.set_xlabel("time (min:sec)") ax.set_ylabel("RSSI (dBm)") majLocator = dates.SecondLocator(interval=60) minLocator = dates.SecondLocator(interval=30) formatter = dates.DateFormatter('%M:%S') ax.xaxis.set_major_locator(majLocator) ax.xaxis.set_minor_locator(minLocator) ax.xaxis.set_major_formatter(formatter) ax.autoscale_view() ax.grid(True, which='both') plt.legend(handles,labels, loc="lower right") #plt.show() plt.savefig(outfilename, format="png")
y, data.T, np.arange(0, dndlogdpMax + 1, 500), cmap='jet', extend='max') #so2plot = ax2.plot(x2,y2) #Formatting dates to a sane format #We have a MAJOR (Times) and a MINOR (dates) #myFmt corresponds to MINOR, myFmt2 to MAJOR myFmt = mdates.DateFormatter("%m/%d/%Y") #Format date myFmt2 = mdates.DateFormatter("%H:%M:%S") #Format time myFmt3 = mdates.DateFormatter("%m/%d") #SO2 Format date ax.xaxis.set_major_formatter(myFmt2) ax.xaxis.set_major_locator( mdates.SecondLocator(interval=int(((max(x) - min(x)).total_seconds()) / 5))) #6 marks on x axis ax.xaxis.set_minor_formatter(myFmt) ax.xaxis.set_minor_locator(mdates.DayLocator(interval=1)) ax.xaxis.set_tick_params(which='minor', pad=15) #Keeps major/minor axis from overlapping #Y axis needs to be logarithmic ax.set_yscale('log') #Colorbar Setup - putting neater ticks on, label cb = fig.colorbar(bananaPlot, ax=ax, ticks=list(range(0, int(dndlogdpMax + 1), int(colorticks))), extendrect=True) cb.set_label('dN/dlogDp')