def demo_fixed_size_axes(): fig1 = plt.figure(1, (6, 6)) # The first items are for padding and the second items are for the axes. # sizes are in inch. h = [Size.Fixed(1.0), Size.Fixed(4.5)] v = [Size.Fixed(0.7), Size.Fixed(5.)] divider = Divider(fig1, (0.0, 0.0, 1., 1.), h, v, aspect=False) # the width and height of the rectangle is ignored. ax = LocatableAxes(fig1, divider.get_position()) ax.set_axes_locator(divider.new_locator(nx=1, ny=1)) fig1.add_axes(ax) ax.plot([1,2,3])
def subplot_array(self, hsize, vsize=(1.0, ), figsize=(10, 10)): """ Use the axes_divider module to make a single row of plots hsize : list of floats horizontal spacing: alternates Scaled for plot, Fixed for between plots vsize : list of floats vertical spacing ref: http://matplotlib.org/mpl_toolkits/axes_grid/users/axes_divider.html """ nx = (len(hsize) + 1) / 2 ny = (len(vsize) + 1) / 2 fig, axx = plt.subplots( ny, nx, squeeze=False, figsize=figsize) # just to make the axes, will move them sizer = lambda x, i: axes_size.Scaled( x) if i % 2 == 0 else axes_size.Fixed(x) horiz = [sizer(h, i) for i, h in enumerate(hsize)] vert = [sizer(v, i) for i, v in enumerate(vsize)] divider = Divider(fig, (0.1, 0.1, 0.8, 0.8), horiz, vert, aspect=False) for i, ax in enumerate(axx.flatten()): iy = i // nx ix = i % nx ax.set_axes_locator(divider.new_locator(nx=2 * ix, ny=2 * iy)) return fig, axx
import mpl_toolkits.axes_grid.axes_size as Size from mpl_toolkits.axes_grid import Divider import matplotlib.pyplot as plt fig1 = plt.figure(1, (5.5, 4.)) # the rect parameter will be ignore as we will set axes_locator rect = (0.1, 0.1, 0.8, 0.8) ax = [fig1.add_axes(rect, label="%d" % i) for i in range(4)] horiz = [Size.Scaled(1.5), Size.Fixed(.5), Size.Scaled(1.), Size.Scaled(.5)] vert = [Size.Scaled(1.), Size.Fixed(.5), Size.Scaled(1.5)] # divide the axes rectangle into grid whose size is specified by horiz * vert divider = Divider(fig1, rect, horiz, vert, aspect=False) ax[0].set_axes_locator(divider.new_locator(nx=0, ny=0)) ax[1].set_axes_locator(divider.new_locator(nx=0, ny=2)) ax[2].set_axes_locator(divider.new_locator(nx=2, ny=2)) ax[3].set_axes_locator(divider.new_locator(nx=2, nx1=4, ny=0)) for ax1 in ax: plt.setp(ax1.get_xticklabels() + ax1.get_yticklabels(), visible=False) plt.draw() plt.show()
def redraw(self): if len(self.input) == 0: self.warning("Empty input") return fast_redraw = self.name in self.pp.get_figlabels() fig = self.pp.figure(self.name) axes = fig.add_subplot(111) if not fast_redraw: self.draw_grid(axes) else: while len(axes.texts): axes.texts[0].remove() # Draw the inner hexagons with text all_patches = [list() for _ in range(len(self.fitness_by_label))] hits_max = numpy.max(self.input) if hits_max == 0: hits_max = 1 reversed_result = {} for label, neurons in enumerate(self.result): for neuron in neurons: reversed_result[neuron] = label # Add hexagons one by one for y in range(self.height): for x in range(self.width): neuron = y * self.width + x fitness = self.fitness_by_neuron[neuron] number = self.input[y * self.width + x] if numpy.sqrt(number / hits_max) <= \ KohonenHits.SIZE_TEXT_THRESHOLD: fitness = 0 try: label = reversed_result[neuron] except KeyError: continue patches = all_patches[label] self._add_hexagon(axes, patches, x, y, fitness) if fast_redraw: axes.collections = [axes.collections[-1]] else: legend_patches = [] cmap = getattr(self.cm, self.cmap) for index, patches in enumerate(all_patches): if len(patches) == 0: continue facecolor = cmap(index * cmap.N // len(all_patches)) col = self.matplotlib.collections.PatchCollection( patches, edgecolors='none', facecolors=facecolor) axes.add_collection(col) if not fast_redraw: legend_patches.append( self.matplotlib.patches.Patch( color=facecolor, label="%d - %.2f" % (index, self.fitness_by_label[index]))) # Make the grid to be drawn last axes.collections = axes.collections[1:] + [axes.collections[0]] if not fast_redraw: legend = axes.legend(handles=legend_patches, loc='upper left', bbox_to_anchor=(1.05, 1), borderaxespad=0.0, title="Fitness: %.2f" % self.fitness) fig.tight_layout() # The legend becomes truncated, but it should not # Measure the legend width to fix buggy matplotlib layout # Without drawing, window extent equals to 1 if legend is not None: legend.draw(fig.canvas.get_renderer()) bbox = legend.get_window_extent() bbox = bbox.transformed(fig.dpi_scale_trans.inverted()) from mpl_toolkits.axes_grid import Divider from mpl_toolkits.axes_grid.axes_size import Fixed, Scaled divider = Divider( fig, (0.05, 0.05, 0.9, 0.9), (Scaled(1.0), Fixed(bbox.width), Scaled(0.05)), (Scaled(1), Fixed(0))) axes.set_axes_locator(divider.new_locator(0, 0)) else: # Modify legend title and labels legend = axes.get_legend() if legend is not None: legend.set_title("Fitness: %.2f" % self.fitness) for index, text in enumerate(legend.get_texts()): text.set_text("%d - %.2f" % (index, self.fitness_by_label[index])) self.show_figure(fig) fig.canvas.draw() return fig
def static_plot_grid_hist_selections(lst_det_left, lst_det_right, selection, fname_det): """ :param lst_det_left: :param lst_det_right: :param selection: :param fname_det: """ # Plot starts here: cms = matplotlib.cm color_map_left = cms.Blues color_map_right = cms.RdPu if fname_det: f1 = plt.figure(1, (23, 13), dpi=1200) else: f1 = plt.figure(1, (15, 8)) vel_range = range(-90, 70) f1.clf() # the rect parameter is ignored as we set the axes_locator rect = (0.05, 0.07, 0.9, 0.87) f1ax = [f1.add_axes(rect, label="%d" % i) for i in range(7)] horiz = [ Size.Scaled(5.), Size.Fixed(1.0), Size.Scaled(1.5), Size.Fixed(1.0), Size.Scaled(2.) ] vert = [ Size.Scaled(1.), Size.Fixed(0.5), Size.Scaled(1.), Size.Fixed(0.5), Size.Scaled(1.), Size.Fixed(0.5), Size.Scaled(1.) ] # divide the axes rectangle into grid whose size is specified by horiz * vert divider = Divider(f1, rect, horiz, vert, aspect=False) f1ax[0].set_axes_locator(divider.new_locator(nx=0, ny=0, ny1=7)) f1ax[1].set_axes_locator(divider.new_locator(nx=2, ny=0)) f1ax[2].set_axes_locator(divider.new_locator(nx=2, ny=2)) f1ax[3].set_axes_locator(divider.new_locator(nx=2, ny=4)) f1ax[4].set_axes_locator(divider.new_locator(nx=2, ny=6)) f1ax[5].set_axes_locator(divider.new_locator(nx=4, ny=0, ny1=3)) f1ax[6].set_axes_locator(divider.new_locator(nx=4, ny=4, ny1=7)) f1ax[0].axis([-40, 100, -80, 80]) f1ax[5].axis([-140, 140, 0, 100]) f1ax[6].axis([-140, 140, -100, 80]) f1ax[0].grid(True) f1ax[1].grid(True) f1ax[2].grid(True) f1ax[3].grid(True) f1ax[4].grid(True) f1ax[5].grid(True) f1ax[6].grid(True) number_of_dets_left = 0 number_of_dets_left_processed = 0 number_of_dets_right = 0 number_of_dets_right_processed = 0 # Left radar plot if lst_det_left: LR_data = lst_det_left.get_array_detections_selected( selection=selection) if LR_data["mcc"].any(): LR_data_exists = True f1ax[2].hist(LR_data["rvelocity"], vel_range, color=color_map_left(0.4), normed=1) number_of_dets_left = np.size(LR_data["mcc"]) if selection["beam_tp"].count(0): selection_tp = copy.deepcopy(selection) selection_tp["beam_tp"] = [0] LR0_data = lst_det_left.get_array_detections_selected( selection=selection_tp) f1ax[1].hist(LR0_data["rvelocity"], vel_range, color=color_map_left(0.2), normed=1, label='beam 0') f1ax[0].plot(LR0_data["x"], LR0_data["y"], color=color_map_left(0.2), marker='o', ls='None', label='Left RDR, beam 0') f1ax[6].plot(-180 * LR0_data["razimuth"] / np.pi, LR0_data["rvelocity"], color=color_map_left(0.2), marker='o', ls='None', label='Left RDR, beam 0') f1ax[5].plot(-180 * LR0_data["razimuth"] / np.pi, LR0_data["range"], color=color_map_left(0.2), marker='o', ls='None', label='Left RDR, beam 0') number_of_dets_left_processed += np.size(LR0_data["mcc"]) if selection["beam_tp"].count(1): selection_tp = copy.deepcopy(selection) selection_tp["beam_tp"] = [1] LR1_data = lst_det_left.get_array_detections_selected( selection=selection_tp) f1ax[1].hist(LR1_data["rvelocity"], vel_range, color=color_map_left(0.4), normed=1, label='beam 1') f1ax[0].plot(LR1_data["x"], LR1_data["y"], color=color_map_left(0.4), marker='o', ls='None', label='Left RDR, beam 1') f1ax[6].plot(-180 * LR1_data["razimuth"] / np.pi, LR1_data["rvelocity"], color=color_map_left(0.4), marker='o', ls='None', label='Left RDR, beam 1') f1ax[5].plot(-180 * LR1_data["razimuth"] / np.pi, LR1_data["range"], color=color_map_left(0.4), marker='o', ls='None', label='Left RDR, beam 1') number_of_dets_left_processed += np.size(LR1_data["mcc"]) if selection["beam_tp"].count(2): selection_tp = copy.deepcopy(selection) selection_tp["beam_tp"] = [2] LR2_data = lst_det_left.get_array_detections_selected( selection=selection_tp) f1ax[1].hist(LR2_data["velocity"], vel_range, color=color_map_left(0.6), normed=1, label='beam 2') f1ax[0].plot(LR2_data["x"], LR2_data["y"], color=color_map_left(0.6), marker='o', ls='None', label='Left RDR, beam 2') f1ax[6].plot(-180 * LR2_data["azimuth"] / np.pi, LR2_data["velocity"], color=color_map_left(0.6), marker='o', ls='None', label='Left RDR, beam 2') f1ax[5].plot(-180 * LR2_data["azimuth"] / np.pi, LR2_data["range"], color=color_map_left(0.6), marker='o', ls='None', label='Left RDR, beam 2') number_of_dets_left_processed += np.size(LR2_data["mcc"]) if selection["beam_tp"].count(3): selection_tp = copy.deepcopy(selection) selection_tp["beam_tp"] = [3] LR3_data = lst_det_left.get_array_detections_selected( selection=selection_tp) f1ax[1].hist(LR3_data["rvelocity"], vel_range, color=color_map_left(0.8), normed=1, label='beam 3') f1ax[0].plot(LR3_data["x"], LR3_data["y"], color=color_map_left(0.8), marker='o', ls='None', label='Left RDR, beam 3') f1ax[6].plot(-180 * LR3_data["razimuth"] / np.pi, LR3_data["rvelocity"], color=color_map_left(0.8), marker='o', ls='None', label='Left RDR, beam 3') f1ax[5].plot(-180 * LR3_data["razimuth"] / np.pi, LR3_data["range"], color=color_map_left(0.8), marker='o', ls='None', label='Left RDR, beam 3') number_of_dets_left_processed += np.size(LR3_data["mcc"]) plt.draw() else: LR_data_exists = False # Right radar plot if lst_det_right: RR_data = lst_det_right.get_array_detections_selected( mcc=selection['mcc_tp']) if RR_data["mcc"].any(): RR_data_exists = True f1ax[4].hist(RR_data["velocity"], vel_range, color=color_map_left(0.4), normed=1) number_of_dets_right = np.size(RR_data["mcc"]) if selection["beam_tp"].count(0): selection_tp = copy.deepcopy(selection) selection_tp["beam_tp"] = [0] RR0_data = lst_det_right.get_array_detections_selected( selection=selection_tp) f1ax[3].hist(RR0_data["velocity"], vel_range, color=color_map_right(0.2), normed=1, label='beam 0') f1ax[0].plot(RR0_data["x"], RR0_data["y"], color=color_map_right(0.2), marker='o', ls='None', label='Right RDR, beam 0') f1ax[6].plot(-180 * RR0_data["azimuth"] / np.pi, RR0_data["velocity"], color=color_map_right(0.2), marker='o', ls='None', label='Right RDR, beam 0') f1ax[5].plot(-180 * RR0_data["azimuth"] / np.pi, RR0_data["range"], color=color_map_right(0.2), marker='o', ls='None', label='Right RDR, beam 0') number_of_dets_right_processed += np.size(RR0_data["mcc"]) if selection["beam_tp"].count(1): selection_tp = copy.deepcopy(selection) selection_tp["beam_tp"] = [1] RR1_data = lst_det_right.get_array_detections_selected( selection=selection_tp) f1ax[3].hist(RR1_data["velocity"], vel_range, color=color_map_right(0.4), normed=1, label='beam 1') f1ax[0].plot(RR1_data["x"], RR1_data["y"], color=color_map_right(0.4), marker='o', ls='None', label='Right RDR, beam 1') f1ax[6].plot(-180 * RR1_data["azimuth"] / np.pi, RR1_data["velocity"], color=color_map_right(0.4), marker='o', ls='None', label='Right RDR, beam 1') f1ax[5].plot(-180 * RR1_data["azimuth"] / np.pi, RR1_data["range"], color=color_map_right(0.4), marker='o', ls='None', label='Right RDR, beam 1') number_of_dets_right_processed += np.size(RR1_data["mcc"]) if selection["beam_tp"].count(2): selection_tp = copy.deepcopy(selection) selection_tp["beam_tp"] = [2] RR2_data = lst_det_right.get_array_detections_selected( selection=selection_tp) f1ax[3].hist(RR2_data["velocity"], vel_range, color=color_map_right(0.6), normed=1, label='beam 2') f1ax[0].plot(RR2_data["x"], RR2_data["y"], color=color_map_right(0.6), marker='o', ls='None', label='Right RDR, beam 2') f1ax[6].plot(-180 * RR2_data["azimuth"] / np.pi, RR2_data["velocity"], color=color_map_right(0.6), marker='o', ls='None', label='Right RDR, beam 2') f1ax[5].plot(-180 * RR2_data["azimuth"] / np.pi, RR2_data["range"], color=color_map_right(0.6), marker='o', ls='None', label='Right RDR, beam 2') number_of_dets_right_processed += np.size(RR2_data["mcc"]) if selection["beam_tp"].count(3): selection_tp = copy.deepcopy(selection) selection_tp["beam_tp"] = [3] RR3_data = lst_det_right.get_array_detections_selected( selection=selection_tp) f1ax[3].hist(RR3_data["velocity"], vel_range, color=color_map_right(0.8), normed=1, label='beam 3') f1ax[0].plot(RR3_data["x"], RR3_data["y"], color=color_map_right(0.8), marker='o', ls='None', label='Right RDR, beam 3') f1ax[6].plot(-180 * RR3_data["azimuth"] / np.pi, RR3_data["velocity"], color=color_map_right(0.8), marker='o', ls='None', label='Right RDR, beam 3') f1ax[5].plot(-180 * RR3_data["azimuth"] / np.pi, RR3_data["range"], color=color_map_right(0.8), marker='o', ls='None', label='Right RDR, beam 3') number_of_dets_right_processed += np.size(RR3_data["mcc"]) plt.draw() else: RR_data_exists = False if LR_data_exists and RR_data_exists: lgd2 = f1ax[0].legend(loc='upper right', bbox_to_anchor=(1.0, 1.0)) f1ax[0].set_xlabel('x [meters]') f1ax[0].set_ylabel('y [meters]') f1ax[1].set_ylabel('Left RDR, separate') f1ax[1].set_xlabel('Velocity $v(mcc)$ [m/s]') f1ax[2].set_ylabel('Left RDR, all') f1ax[3].set_ylabel('Right RDR, separate') f1ax[4].set_ylabel('Right RDR, all') f1ax[5].set_xlabel('Azimuth [deg]') f1ax[5].set_ylabel('Range [meters]') f1ax[6].set_ylabel('Velocity [km h$^{-1}$]') tit = "MCC: %08d Num of Det: L=%d R=%d In Selected Beams: L=%d R=%d" % ( selection["mcc_tp"][0], number_of_dets_left, number_of_dets_right, number_of_dets_left_processed, number_of_dets_right_processed) f1.suptitle(tit, fontsize=14, fontweight='bold') if fname_det: f1.savefig(fname_det, format='eps', dpi=1200) else: plt.show() else: print("Nothing to plot for MCC:", selection["mcc_tp"])