def plot_terminal(data, title, xlabel, yrange): """ Plot data to the terminal using plotext """ import plotext as plt x = data.index.tolist() y = data[title].tolist() plt.scatter(x, y) plt.title(title) plt.xlabel(xlabel) plt.ylim(*yrange) plt.plot_size(100, 30) plt.show()
def plot_measures(measures, offset=0): start_timestamp = datetime.now() - timedelta(days=7) now = datetime.now() width, height = plt.terminal_size() if offset: height -= offset if MAXIMUM_HEIGHT and MAXIMUM_HEIGHT < height: height = MAXIMUM_HEIGHT if MAXIMUM_WIDTH and MAXIMUM_WIDTH < width: width = MAXIMUM_WIDTH values_y = [m.value for m in measures] values_x = [m.timestamp for m in measures] plt.clear_plot() plt.plot(values_x, values_y) plt.nocolor() xticks = [int(start_timestamp.strftime("%s"))] xlabels = [start_timestamp.strftime(DATETIME_FORMAT)] day = (start_timestamp + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0) while day < now: xticks.append(int(day.strftime("%s"))) xlabels.append(day.strftime(DATETIME_FORMAT)) day += timedelta(days=1) plt.xlim(int(start_timestamp.strftime("%s")), int(now.strftime("%s"))) plt.xticks(xticks, xlabels) plt.figsize(width, height) plt.show()
def diff2plot(diff, kind=None, title='Unnamed plot'): """Function to plot graph to the terminal. Can be scatter or bar plot (Initial test functionality) Works only with plotext 4.2, not above""" # try: # import plotext as plt # except ModuleNotFoundError: # # Error handling # pass plx.clear_plot() diff = diff.round(2) if kind == 'bar': # expects a series with index being string names mask0 = (diff.values != 0) & ~_np.isnan(diff.values) if mask0.any(): plx.bar(diff.index.values[mask0].tolist(), diff.values[mask0].tolist(), orientation="h", width=0.3) plx.vertical_line(coordinate=0) plx.plotsize(100, diff.shape[0] * 2 + 3) else: return None else: mask0 = ((diff.values != 0) & ~_np.isnan(diff.values)).any(axis=0) if mask0.any(): cols = diff.columns[mask0] x1 = plx.datetime.datetime_to_string( diff.index.astype('timedelta64[ns]') * 1000000000 + _J2000_ORIGIN) for i in range(cols.shape[0]): plx.scatter_date(x1, diff[cols[i]].to_list(), color=i, marker="hd", label=diff.columns[i]) plx.plotsize(100, 30 + 3) else: return None plx.title(title) plx.limit_size(limit_xsize=False, limit_ysize=False) plx.show()
def main(what, calc_id: int = -1, others: int = [], webapi=False, local=False): """ Generic plotter for local and remote calculations. """ if what.startswith('POLYGON'): plt = plot_wkt(what) plt.show() return if what == 'examples': help_msg = ['Examples of possible plots:'] for k, v in globals().items(): if k.startswith('make_figure_'): help_msg.append(v.__doc__) raise SystemExit(''.join(help_msg)) if '?' not in what: raise SystemExit('Missing ? in %r' % what) prefix, rest = what.split('?', 1) if prefix in 'hcurves hmaps' and 'imt=' not in rest: raise SystemExit('Missing imt= in %r' % what) elif prefix == 'uhs' and 'imt=' in rest: raise SystemExit('Invalid IMT in %r' % what) elif prefix in 'hcurves uhs disagg' and 'site_id=' not in rest: what += '&site_id=0' if prefix == 'disagg' and 'poe_id=' not in rest: what += '&poe_id=0' if local: xs = [WebExtractor(calc_id, 'http://localhost:8800', '')] for other_id in others: xs.append(WebExtractor(other_id), 'http://localhost:8800', '') elif webapi: xs = [WebExtractor(calc_id)] for other_id in others: xs.append(WebExtractor(other_id)) else: xs = [Extractor(calc_id)] for other_id in others: xs.append(Extractor(other_id)) make_figure = globals()['make_figure_' + prefix] plt = make_figure(xs, what) plt.show()
def main(filea, fileb): if filea is None: # take the second most recent profiling_files = parse_and_sort_profiling_files() filea = profiling_files[-2] if fileb is None: # take the most recent profiling_files = parse_and_sort_profiling_files() fileb = profiling_files[-1] profile_a = pd.read_pickle(filea) profile_b = pd.read_pickle(fileb) merged = pd.merge( profile_a, profile_b, how='left', on=['document_length','document_depth', 'working_depth', 'item_length'] ) plt.clp() bins = 30 plt.hist(merged['tottime_x'], bins, label="profile a") plt.hist(merged['tottime_y'], bins, label="profile b") plt.title("tottime distribution") plt.xlabel("time bins") plt.ylabel("frequency") plt.canvas_color("none") plt.axes_color("none") plt.ticks_color("cloud") plt.figsize(50, 15) plt.show() merged['diff'] = merged['tottime_x'] - merged['tottime_y'] print("") print(f"## avg improvement: {merged['diff'].mean()} seconds ##") print("")
#For formating plt.clear_plot() plt.clear_terminal() plt.scatter(y, point_color=color, fill=True, label=ticker + "- Open:" + str(opening) + " Current:" + str(current) + "1d High:" + str(current_high)) # Labled as Open: , Current: , High: #Most recent hours of trades will be highlighted blue plt.scatter(list(range(540, 600)), y[-60:], fill=True, label="Current Hour", point_color="blue") #More formating plt.canvas_color('none') plt.axes_color("none") plt.grid(False, False) plt.axes(False, False) # Adds padding to right side plt.xlim([0, 625]) plt.ticks(0, 10) # Render graph every 1m (API only updates at 1m intervals) plt.show() sleep(60)
def metrics(self): # available metrics metrics = ('node_cpu_usage', 'node_memory_usage', 'node_disk_usage', 'pod_memory_usage', 'pod_cpu_usage') # column in prom2df dataframe for given metric group metric_group_column = { 'node': 'node', 'pod': 'pod_name', } metric_name = (self.args.get('<metric_name>') or '').replace('-', '_') since = self.args.get('--since') or None start = self.args.get('--start') or None end = self.args.get('--end') or None step = self.args.get('--step') or None should_plot = self.args.get('--plot') # check if we have a valid metric if metric_name in metrics: # get default range, if any if since: if any(since.endswith(v) for v in 'hms'): # a relative time spec unit = dict(h='hours', m='minutes', s='seconds').get(since[-1]) delta = int(since[0:-1]) start = datetime.utcnow() - timedelta(**{unit: delta}) else: # absolute time start = pd.to_datetime(since, utc=True) if start: start = pd.to_datetime(start, utc=True) if end: end = pd.to_datetime(end, utc=True) if start and not end: end = datetime.utcnow() if end and not start: start = (end - timedelta(minutes=10)).isoformat() if (start or end) and not step: step = '5m' # query om = self.om auth = self._restapi_auth() data = self._get_metric(metric_name, auth, start=start, end=end, step=step) try: df = prom2df(data['objects'], metric_name) except: print("No data could be found. Check the time range.") return if should_plot: import plotext as plx # as returned by plx.get_colors colors = 'red', 'green', 'yellow', 'organge', 'blue', 'violet', 'cyan' metric_group = metric_group_column[metric_name.split('_', 1)[0]] for i, (g, gdf) in enumerate(df.groupby(metric_group)): x = range(0, len(gdf)) y = gdf['value'].values plx.plot(x, y, line_color=colors[i]) plx.show() else: print(tabulate(df, headers='keys')) else: print("Available metrics:", metrics)
''' small auxiliary script that uses the lammps log parser to quickly plot data to terminal usage: plot_thermo.py <thermo_key> ''' import lammps_log_parser as lmp import plotext as pt import sys yvar = sys.argv[1] data = lmp.parse_log_to_pandas_df('*log') pt.plot(data['Step'], data[yvar]) pt.xlabel('simulation steps') pt.ylabel(yvar) pt.nocolor() pt.figsize(160, 40) pt.show()
def animate(self): """ Animates the rocket's movement according to the data in the logs. """ # Ideally we'd use something other than matplotlib. It really wasn't designed for # this sort of application. import matplotlib.pyplot as plt import matplotlib.patches as patches from matplotlib.animation import FuncAnimation import numpy as np import quaternion as q cm_positions = self.logs['cm_position'].get_data() cm_orientations = self.logs['cm_orientation'].get_data() frame_rel_positions = self.logs['frame_position'].get_data() frame_rel_orientations = self.logs['frame_orientation'].get_data() mount_rel_positions = self.logs['mount_position'].get_data() mount_rel_orientations = self.logs['mount_orientation'].get_data() thrust = self.logs['thrust'].get_data() fig, ax = plt.subplots(2, 2) ax[0, 0].set_xlim(-21, 21) ax[0, 0].set_ylim(-2, 20) ax[0, 1].set_xlim(-21, 21) ax[0, 1].set_ylim(-2, 20) ax[1, 0].set_xlim(-21, 21) ax[1, 0].set_ylim(-21, 21) # Rockets definition rocket_xz = patches.Rectangle((0, 0.5), 0.2, 1) rocket_yz = patches.Rectangle((0, 0.5), 0.2, 1) rocket_xy = patches.Rectangle((0, 0), 0.2, 0.2) # Thrust definition thrust_xz = patches.Rectangle((0, 0), 0.1, -1, facecolor='r') thrust_yz = patches.Rectangle((0, 0), 0.1, -1, facecolor='r') # Floor floor_xz = patches.Rectangle((-50, 0), 100, -2, facecolor='g') floor_yz = patches.Rectangle((-50, 0), 100, -2, facecolor='g') def init(): ax[0, 0].add_patch(rocket_xz) ax[0, 0].add_patch(thrust_xz) ax[0, 0].add_patch(floor_xz) ax[0, 1].add_patch(rocket_yz) ax[0, 1].add_patch(thrust_yz) ax[0, 1].add_patch(floor_yz) ax[1, 0].add_patch(rocket_xy) return rocket_xz, thrust_xz, floor_xz, rocket_yz, thrust_yz, floor_yz, rocket_xy def update(frame_count): cm_position = cm_positions[frame_count] cm_orientation = cm_orientations[frame_count] frame_position = cm_position + q.rotate_vectors( cm_orientation, frame_rel_positions[frame_count] + np.array([-0.1, -0.1, -0.5])) frame_direction = q.rotate_vectors( frame_rel_orientations[frame_count] * cm_orientation, np.array([0, 0, 1])) mount_position = cm_position + q.rotate_vectors( cm_orientation, mount_rel_positions[frame_count]) mount_direction = q.rotate_vectors( mount_rel_orientations[frame_count] * cm_orientation, np.array([0, 0, 1])) frame_angle_xz = np.rad2deg( math.atan2(frame_direction[0], frame_direction[2])) frame_angle_yz = np.rad2deg( math.atan2(frame_direction[1], frame_direction[2])) # Rockets update rocket_xz.set_xy(frame_position[[0, 2]]) rocket_xz.angle = frame_angle_xz rocket_yz.set_xy(frame_position[[1, 2]]) rocket_yz.angle = frame_angle_yz rocket_xy.set_xy(frame_position[[0, 1]]) thrust_angle_xz = np.rad2deg( math.atan2(mount_direction[0], mount_direction[2])) thrust_angle_yz = np.rad2deg( math.atan2(mount_direction[1], mount_direction[2])) thrust_xz.set_xy(mount_position[[0, 2]]) thrust_xz.angle = thrust_angle_xz thrust_yz.set_xy(mount_position[[1, 2]]) thrust_yz.angle = thrust_angle_yz return rocket_xz, thrust_xz, floor_xz, rocket_yz, thrust_yz, floor_yz, rocket_xy anim = FuncAnimation(fig, update, init_func=init, frames=len(cm_positions), interval=20, blit=True) plt.show()
def show_statistics(rule_file=None, rule_set=None, indx=None): interface = None if rule_set == "-e": interface = "external" elif rule_set == "-i": interface = "internal" logs = utils.load_logs('logs.json') print("") print("-" * 13, "OVERALL FIREWALL STATISTICS", "-" * 13) print("TOTAL PACkETS RECEIVED BY THE FIREWALL: ", logs["total_packets"]) print("TOTAL PACkETS DROPPED BY THE FIREWALL: ", logs["total_dropped"]) if rule_file != None: if interface != None: print("") print("-" * 12, interface.upper(), "INTERFACE STATISTICS", "-" * 12) print("TOTAL PACKETS RECEIVED ON", interface.upper(), "INTERFACE: ", logs[rule_file][interface]["total_packets"]) print("TOTAL PACKETS DROPPED ON", interface.upper(), "INTERFACE: ", logs[rule_file][interface]["total_dropped"]) if indx != None: if indx in logs[rule_file][interface]: print("\nTOTAL PACKETS DROPPED DUE TO RULE", indx, "ON", interface.upper(), "INTERFACE :", logs[rule_file][interface][indx]) else: print("Invalid rule index!") else: print("") print("-" * 12, "EXTERNAL INTERFACE STATISTICS", "-" * 12) print("TOTAL PACKETS RECEIVED ON EXTERNAL INTERFACE: ", logs[rule_file]["external"]["total_packets"]) print("TOTAL PACKETS DROPPED ON EXTERNAL INTERFACE: ", logs[rule_file]["external"]["total_dropped"]) print("") print("-" * 12, "INTERNAL INTERFACE STATISTICS", "-" * 12) print("TOTAL PACKETS RECEIVED ON INTERNAL INTERFACE: ", logs[rule_file]["internal"]["total_packets"]) print("TOTAL PACKETS DROPPED ON INTERNAL INTERFACE: ", logs[rule_file]["internal"]["total_dropped"]) else: print("") print("-" * 16, "ICMP FLOOD STATISTICS", "-" * 16) print("FLOODS ENCOUNTERED SO FAR: ", logs["icmp_floods"]) print("PACKETS DROPPED DURING ICMP BLOCK:", logs["icmp_dropped"]) print("") print("-" * 22, "PPS INFO", "-" * 22) print("MAXIMUM PPS SO FAR: ", logs["max_pps"]) print("") title = "-" * 18 + "NETWORK TRAFFIC" + "-" * 18 net_traffic = logs["traffic"] max_sec = max(np.array(list(net_traffic.keys())).astype(int)) packets_num = np.zeros(max_sec + 1) for i in net_traffic: packets_num[int(i)] = net_traffic[i] packets_num = packets_num.astype(int) plt.scatter(np.arange(max_sec + 1), packets_num, fillx=True) plt.figsize(80, 20) plt.title(title) plt.xlabel("Time") plt.ylabel("Packets") plt.show()