def test_offsetbox_loc_codes(): # Check that valid string location codes all work with an AnchoredOffsetbox codes = { 'upper right': 1, 'upper left': 2, 'lower left': 3, 'lower right': 4, 'right': 5, 'center left': 6, 'center right': 7, 'lower center': 8, 'upper center': 9, 'center': 10, } fig, ax = plt.subplots() da = DrawingArea(100, 100) for code in codes: anchored_box = AnchoredOffsetbox(loc=code, child=da) ax.add_artist(anchored_box) fig.canvas.draw()
def make_line(color, style, alpha, width=20, y_offset=10, height=20, linewidth=3): color = color if color != None else "k" # Default value if None style = style if style != None else "-" viz = DrawingArea(30, 10, 0, -5) x = np.arange(0.0, width, width / 7.0) y = np.repeat(y_offset, x.size) key = mlines.Line2D(x, y, linestyle=style, linewidth=linewidth, alpha=alpha, c=color) viz.add_artist(key) return viz
def createBall(self, colour): radius = 2 da = DrawingArea(radius, radius, 10, 10) circle = patches.Circle((0.0, 0.0), radius=radius, edgecolor='k', facecolor=colour, fill=True, ls='solid', clip_on=False) da.add_artist(circle) ab = AnnotationBbox(da, xy=(0, 0), xycoords=("data", "data"), boxcoords=("data", "data"), box_alignment=(5.0, 5.0), frameon=False) return ab
def pie(plot, p, values, colors=None, size=16, norm=True, xoff=0, yoff=0, halign=0.5, valign=0.5, xycoords='data', boxcoords=('offset points')): """ Draw a pie chart Args: plot (Tree): A Tree plot instance p (Node): A Node object values (list): A list of floats. colors (list): A list of strings to pull colors from. Optional. size (float): Diameter of the pie chart norm (bool): Whether or not to normalize the values so they add up to 360 xoff, yoff (float): X and Y offset. Optional, defaults to 0 halign, valign (float): Horizontal and vertical alignment within box. Optional, defaults to 0.5 """ x, y = _xy(plot, p) da = DrawingArea(size, size); r = size*0.5; center = (r,r) x0 = 0 S = 360.0 if norm: S = 360.0/sum(values) if not colors: c = _colors.tango() colors = [ c.next() for v in values ] for i, v in enumerate(values): theta = v*S if v: da.add_artist(Wedge(center, r, x0, x0+theta, fc=colors[i], ec='none')) x0 += theta box = AnnotationBbox(da, (x,y), pad=0, frameon=False, xybox=(xoff, yoff), xycoords=xycoords, box_alignment=(halign,valign), boxcoords=boxcoords) plot.add_artist(box) plot.figure.canvas.draw_idle() return box
def hbar(plot, p, values, colors=None, height=16, xoff=0, yoff=0, halign=1, valign=0.5, xycoords='data', boxcoords=('offset points')): x, y = _xy(plot, p) h = height; w = sum(values) * height#; yoff=h*0.5 da = DrawingArea(w, h) x0 = -sum(values) if not colors: c = _colors.tango() colors = [ c.next() for v in values ] for i, v in enumerate(values): if v: da.add_artist(Rectangle((x0,0), v*h, h, fc=colors[i], ec='none')) x0 += v*h box = AnnotationBbox(da, (x,y), pad=0, frameon=False, xybox=(xoff, yoff), xycoords=xycoords, box_alignment=(halign,valign), boxcoords=boxcoords) plot.add_artist(box) plot.figure.canvas.draw_idle()
def draw_matching(match, nbMen, nbWomen): width, height = 25, 50 da = DrawingArea(width, height) coordM = [( width/4, height*(nbMen-i)/(nbMen+1)) for i in range(nbMen)] coordW = [(3*width/4, height*(nbWomen-i)/(nbWomen+1)) for i in range(nbWomen)] for idWoman in range(nbWomen): if match[idWoman] == -1: xdata = [coordW[idWoman][0]] ydata = [coordW[idWoman][1]] da.add_artist(Line2D(xdata, ydata, marker=".")) for idMan in range(nbMen): if idMan not in match: xdata = [coordM[idMan][0]] ydata = [coordM[idMan][1]] da.add_artist(Line2D(xdata, ydata, marker=".")) for idWoman,idMan in enumerate(match): if idMan != -1: xdata = [coordM[idMan][0], coordW[idWoman][0]] ydata = [coordM[idMan][1], coordW[idWoman][1]] da.add_artist(Line2D(xdata, ydata, marker=".")) return da
def tipsquares(plot, p, colors='r', size=15, pad=2, edgepad=10): """ RR: Bug with this function. If you attempt to call it with a list as an argument for p, it will not only not work (expected) but it will also make it so that you can't interact with the tree figure (gives errors when you try to add symbols, select nodes, etc.) -CZ Add square after tip label, anchored to the side of the plot Args: plot (Tree): A Tree plot instance. p (Node): A Node object (Should be a leaf node). colors (str): color of drawn square. Optional, defaults to 'r' (red) size (float): Size of square. Optional, defaults to 15 pad: RR: I am unsure what this does. Does not seem to have visible effect when I change it. -CZ edgepad (float): Padding from square to edge of plot. Optional, defaults to 10. """ x, y = _xy(plot, p) # p is a single node or point in data coordinates n = len(colors) da = DrawingArea(size * n + pad * (n - 1), size, 0, 0) sx = 0 for c in colors: sq = Rectangle((sx, 0), size, size, color=c) da.add_artist(sq) sx += size + pad box = AnnotationBbox(da, (x, y), xybox=(-edgepad, y), frameon=False, pad=0.0, xycoords='data', box_alignment=(1, 0.5), boxcoords=('axes points', 'data')) plot.add_artist(box) plot.figure.canvas.draw_idle()
def test_offsetbox_clip_children(): # - create a plot # - put an AnchoredOffsetbox with a child DrawingArea # at the center of the axes # - give the DrawingArea a gray background # - put a black line across the bounds of the DrawingArea # - see that the black line is clipped to the edges of # the DrawingArea. fig, ax = plt.subplots() size = 100 da = DrawingArea(size, size, clip=True) bg = mpatches.Rectangle((0, 0), size, size, facecolor='#CCCCCC', edgecolor='None', linewidth=0) line = mlines.Line2D([-size * .5, size * 1.5], [size / 2, size / 2], color='black', linewidth=10) anchored_box = AnchoredOffsetbox(loc=10, child=da, pad=0., frameon=False, bbox_to_anchor=(.5, .5), bbox_transform=ax.transAxes, borderpad=0.) da.add_artist(bg) da.add_artist(line) ax.add_artist(anchored_box) fig.canvas.draw() assert not fig.stale da.clip_children = True assert fig.stale
def plot_rectangle(self, figure, axis): ''' plots the legend rectangle above the left corner of the figure :param figure: figure on which to add the label :param axis: axis on which to add the label :return: - ''' box1 = TextArea(" True: \n False: \n NaN: ", textprops=dict(color="k", size=10)) # box2 = DrawingArea(20, 27.5, 0, 0) # el1 = Rectangle((5, 15), width=10, height=10, angle=0, fc="g") # el2 = Rectangle((5, 2.5), width=10, height=10, angle=0, fc="r") box2 = DrawingArea(20, 45, 0, 0) el1 = Rectangle((5, 30), width=10, height=10, angle=0, fc="g") el2 = Rectangle((5, 18.5), width=10, height=10, angle=0, fc="r") el3 = Rectangle((5, 7), width=10, height=10, angle=0, fc='#d3d3d3') box2.add_artist(el1) box2.add_artist(el2) box2.add_artist(el3) box = HPacker(children=[box1, box2], align="center", pad=0, sep=5) anchored_box = AnchoredOffsetbox( loc=3, child=box, pad=0., frameon=True, bbox_to_anchor=(0., 1.02), bbox_transform=axis.transAxes, borderpad=0., ) axis.add_artist(anchored_box) figure.subplots_adjust(top=0.8)
def generateGraph(self, data=None, outputFilename=None, timeDivisions=6, graphWidth=1920, graphHeight=300, darkMode=False, rainVariance=False, minMaxTemperature=False, fontSize=12, symbolZoom=1.0, symbolDivision=1, showCityName=None, writeMetaData=None): logging.debug("Initializing graph...") if darkMode: colors = self.colorsDarkMode else: colors = self.colorsLightMode fig = plt.figure(0) # Main figure rainAxis = fig.add_subplot(111) # set font sizes plt.rcParams.update({'font.size': fontSize}) # Temperature Y axis and day names rainAxis.tick_params(axis='y', labelsize=fontSize) # Rain Y axis plt.xticks(fontsize=fontSize) # Time axis if not graphWidth: graphWidth = 1280 if not graphHeight: graphHeight = 300 logging.debug("Graph size: %d x %d pixel" % (graphWidth, graphHeight)) fig.set_size_inches( float(graphWidth) / fig.get_dpi(), float(graphHeight) / fig.get_dpi()) # Plot dimension and borders bbox = rainAxis.get_window_extent().transformed( fig.dpi_scale_trans.inverted()) width, height = bbox.width * fig.dpi, bbox.height * fig.dpi # plot size in pixel plt.margins(x=0) rainAxis.margins(x=0) plt.subplots_adjust(left=40 / width, right=1 - 40 / width, top=1 - 35 / height, bottom=40 / height) bbox = rainAxis.get_window_extent().transformed( fig.dpi_scale_trans.inverted()) width, height = bbox.width * fig.dpi, bbox.height * fig.dpi # plot size in pixel xPixelsPerDay = width / data["noOfDays"] # Dimensions of the axis in pixel firstDayX = math.ceil(bbox.x0 * fig.dpi) firstDayY = math.ceil(bbox.y0 * fig.dpi) dayWidth = math.floor((bbox.x1 - bbox.x0) * fig.dpi) / data["noOfDays"] dayHeight = math.floor((bbox.y1 - bbox.y0) * fig.dpi) # Show gray background on every 2nd day for day in range(0, data["noOfDays"], 2): plt.axvspan(data["timestamps"][0 + day * 24], data["timestamps"][23 + day * 24] + 3600, facecolor='gray', alpha=0.2) # Time axis and ticks plt.xticks(data["timestamps"][::timeDivisions], data["formatedTime"][::timeDivisions]) rainAxis.tick_params(axis='x', colors=colors["x-axis"]) # Rain (data gets splitted to stacked bars) logging.debug("Creating rain plot...") rainBars = [0] * len(self.rainColorSteps) for i in range(0, len(self.rainColorSteps)): rainBars[i] = [] for rain in data["rainfall"]: for i in range(0, len(self.rainColorSteps)): if rain > self.rainColorSteps[i]: rainBars[i].append(self.rainColorStepSizes[i]) else: if i > 0: rainBars[i].append( max(rain - self.rainColorSteps[i - 1], 0)) else: rainBars[i].append(rain) continue rainAxis.bar(data["timestamps"], rainBars[0], width=3000, color=self.rainColors[0], align='edge') bottom = [0] * len(rainBars[0]) for i in range(1, len(self.rainColorSteps)): bottom = np.add(bottom, rainBars[i - 1]).tolist() rainAxis.bar(data["timestamps"], rainBars[i], bottom=bottom, width=3000, color=self.rainColors[i], align='edge') rainAxis.tick_params(axis='y', labelcolor=colors["rain-axis"], width=0, length=8) rainYRange = plt.ylim() rainScaleMax = max( data["rainfall"] ) + 1 # Add a bit to make sure we do not bang our head plt.ylim(0, rainScaleMax) rainAxis.locator_params(axis='y', nbins=7) # TODO find a better way than rounding rainAxis.yaxis.set_major_formatter(FormatStrFormatter('%0.1f')) # Rain color bar as y axis plt.xlim( data["timestamps"][0], data["timestamps"][-1] + (data["timestamps"][1] - data["timestamps"][0])) pixelToRainX = 1 / xPixelsPerDay * (data["timestamps"][23] - data["timestamps"][0]) x = data["timestamps"][-1] + ( data["timestamps"][1] - data["timestamps"][0]) # end of x w = 7 * pixelToRainX for i in range(0, len(self.rainColorSteps)): y = self.rainColorSteps[i] - self.rainColorStepSizes[i] if y > rainScaleMax: break h = self.rainColorSteps[i] + self.rainColorStepSizes[i] if y + h >= rainScaleMax: # reached top h = rainScaleMax - y rainScaleBar = Rectangle((x, y), w, h, fc=self.rainColors[i], alpha=1) rainAxis.add_patch(rainScaleBar) rainScaleBar.set_clip_on(False) rainScaleBorder = Rectangle((x, 0), w, rainScaleMax, fc="black", fill=False, alpha=1) rainAxis.add_patch(rainScaleBorder) rainScaleBorder.set_clip_on(False) # Rain variance if rainVariance: rainfallVarianceAxis = rainAxis.twinx( ) # instantiate a second axes that shares the same x-axis rainfallVarianceAxis.axes.yaxis.set_visible(False) timestampsCentered = [i + 1500 for i in data["timestamps"]] rainfallVarianceMin = np.subtract( np.array(data["rainfall"]), np.array(data["rainfallVarianceMin"])) rainfallVarianceMax = np.subtract( np.array(data["rainfallVarianceMax"]), np.array(data["rainfall"])) rainfallVarianceAxis.errorbar( timestampsCentered, data["rainfall"], yerr=[rainfallVarianceMin, rainfallVarianceMax], fmt="none", elinewidth=1, alpha=0.5, ecolor='black', capsize=3) plt.ylim(0, rainScaleMax) # Show when the model was last calculated timestampLocal = data[ "modelCalculationTimestamp"] + self.utcOffset * 3600 l = mlines.Line2D([timestampLocal, timestampLocal], [rainYRange[0], rainScaleMax]) rainAxis.add_line(l) # Temperature logging.debug("Creating temerature plot...") temperatureAxis = rainAxis.twinx( ) # instantiate a second axes that shares the same x-axis temperatureAxis.plot(data["timestamps"], data["temperature"], label="temperature", color=self.temperatureColor, linewidth=4) #temperatureAxis.set_ylabel('Temperature', color=self.temperatureColor) temperatureAxis.tick_params(axis='y', labelcolor=colors["temperature-axis"]) temperatureAxis.grid(True) # Position the Y Scales temperatureAxis.yaxis.tick_left() rainAxis.yaxis.tick_right() # Make sure the temperature scaling has a gap of 45 pixel, so we can fit the labels interimPixelToTemperature = (np.nanmax(data["temperature"]) - np.nanmin(data["temperature"])) / height temperatureScaleMin = np.nanmin( data["temperature"]) - float(45) * interimPixelToTemperature temperatureScaleMax = np.nanmax( data["temperature"]) + float(45) * interimPixelToTemperature plt.ylim(temperatureScaleMin, temperatureScaleMax) temperatureAxis.locator_params(axis='y', nbins=6) temperatureAxis.yaxis.set_major_formatter(FormatStrFormatter('%0.1f')) pixelToTemperature = (temperatureScaleMax - temperatureScaleMin) / height # Temperature variance temperatureVarianceAxis = temperatureAxis.twinx( ) # instantiate a second axes that shares the same x-axis temperatureVarianceAxis.axes.yaxis.set_visible(False) temperatureVarianceAxis.fill_between(data["timestamps"], data["temperatureVarianceMin"], data["temperatureVarianceMax"], facecolor=self.temperatureColor, alpha=0.2) temperatureVarianceAxis.tick_params(axis='y', labelcolor=self.temperatureColor) plt.ylim(temperatureScaleMin, temperatureScaleMax) logging.debug("Adding various additional information to the graph...") # Mark min/max temperature per day if minMaxTemperature: da = DrawingArea(2, 2, 0, 0) da.add_artist( Circle((1, 1), 4, color=self.temperatureColor, fc="white", lw=2)) for day in range(0, data["noOfDays"]): dayXPixelMin = day * xPixelsPerDay dayXPixelMax = (day + 1) * xPixelsPerDay - 1 maxTemperatureOfDay = {"data": -100, "timestamp": 0} minTemperatureOfDay = {"data": +100, "timestamp": 0} for h in range(0, 24): if data["temperature"][day * 24 + h] > maxTemperatureOfDay["data"]: maxTemperatureOfDay["data"] = data["temperature"][day * 24 + h] maxTemperatureOfDay["timestamp"] = data["timestamps"][ day * 24 + h] maxTemperatureOfDay["xpixel"] = ( data["timestamps"][day * 24 + h] - data["timestamps"][0]) / (24 * 3600) * xPixelsPerDay maxTemperatureOfDay["ypixel"] = ( data["temperature"][day * 24 + h] - temperatureScaleMin) / ( temperatureScaleMax - temperatureScaleMin) * height if data["temperature"][day * 24 + h] < minTemperatureOfDay["data"]: minTemperatureOfDay["data"] = data["temperature"][day * 24 + h] minTemperatureOfDay["timestamp"] = data["timestamps"][ day * 24 + h] minTemperatureOfDay["xpixel"] = ( data["timestamps"][day * 24 + h] - data["timestamps"][0]) / (24 * 3600) * xPixelsPerDay minTemperatureOfDay["ypixel"] = ( data["temperature"][day * 24 + h] - temperatureScaleMin) / ( temperatureScaleMax - temperatureScaleMin) * height # Circles temperatureVarianceAxis.add_artist( AnnotationBbox(da, (maxTemperatureOfDay["timestamp"], maxTemperatureOfDay["data"]), xybox=(maxTemperatureOfDay["timestamp"], maxTemperatureOfDay["data"]), xycoords='data', boxcoords=("data", "data"), frameon=False)) temperatureVarianceAxis.add_artist( AnnotationBbox(da, (minTemperatureOfDay["timestamp"], minTemperatureOfDay["data"]), xybox=(minTemperatureOfDay["timestamp"], minTemperatureOfDay["data"]), xycoords='data', boxcoords=("data", "data"), frameon=False)) # Max Temperature Labels text = str(int(round(maxTemperatureOfDay["data"], 0))) + "°C" f = plt.figure( 1 ) # Temporary figure to get the dimensions of the text label t = plt.text(0, 0, text, weight='bold') temporaryLabel = t.get_window_extent( renderer=f.canvas.get_renderer()) plt.figure(0) # Select Main figure again # Check if text is fully within the day (x axis) if maxTemperatureOfDay[ "xpixel"] - temporaryLabel.width / 2 < dayXPixelMin: # To far left maxTemperatureOfDay[ "xpixel"] = dayXPixelMin + temporaryLabel.width / 2 + self.textShadowWidth / 2 if maxTemperatureOfDay[ "xpixel"] + temporaryLabel.width / 2 > dayXPixelMax: # To far right maxTemperatureOfDay[ "xpixel"] = dayXPixelMax - temporaryLabel.width / 2 - self.textShadowWidth / 2 temperatureVarianceAxis.annotate( text, xycoords=('axes pixels'), xy=(maxTemperatureOfDay["xpixel"], maxTemperatureOfDay["ypixel"] + 8), ha="center", va="bottom", color=colors["temperature-label"], weight='bold', path_effects=[ path_effects.withStroke(linewidth=self.textShadowWidth, foreground="w") ]) # Min Temperature Labels text = str(int(round(minTemperatureOfDay["data"], 0))) + "°C" f = plt.figure( 1 ) # Temporary figure to get the dimensions of the text label t = plt.text(0, 0, text, weight='bold') temporaryLabel = t.get_window_extent( renderer=f.canvas.get_renderer()) plt.figure(0) # Select Main figure again # Check if text is fully within the day (x axis) if minTemperatureOfDay[ "xpixel"] - temporaryLabel.width / 2 < dayXPixelMin: # To far left minTemperatureOfDay[ "xpixel"] = dayXPixelMin + temporaryLabel.width / 2 + self.textShadowWidth / 2 if minTemperatureOfDay[ "xpixel"] + temporaryLabel.width / 2 > dayXPixelMax: # To far right minTemperatureOfDay[ "xpixel"] = dayXPixelMax - temporaryLabel.width / 2 - self.textShadowWidth / 2 temperatureVarianceAxis.annotate( text, xycoords=('axes pixels'), xy=(minTemperatureOfDay["xpixel"], minTemperatureOfDay["ypixel"] - 12), ha="center", va="top", color=colors["temperature-label"], weight='bold', path_effects=[ path_effects.withStroke(linewidth=self.textShadowWidth, foreground="w") ]) # Print day names for day in range(0, data["noOfDays"]): rainAxis.annotate(data['dayNames'][day], xy=(day * xPixelsPerDay + xPixelsPerDay / 2, -45), xycoords='axes pixels', ha="center", weight='bold', color=colors["x-axis"]) # Show y-axis units rainAxis.annotate("mm\n/h", linespacing=0.8, xy=(width + 25, height + 12), xycoords='axes pixels', ha="center", color=colors["rain-axis"]) rainAxis.annotate("°C", xy=(-20, height + 10), xycoords='axes pixels', ha="center", color=colors["temperature-axis"]) # Show Symbols above the graph for i in range(0, len(data["symbols"]), symbolDivision): symbolFile = os.path.dirname( os.path.realpath(__file__)) + "/symbols/" + str( data["symbols"][i]) + ".png" if not os.path.isfile(symbolFile): logging.warning( "The symbol file %s seems to be missing. Please check the README.md!" % symbolFile) continue symbolImage = mpimg.imread(symbolFile) imagebox = OffsetImage(symbolImage, zoom=symbolZoom / 1.41 * 0.15) xyPos = ( (data["symbolsTimestamps"][i] - data["symbolsTimestamps"][0]) / (24 * 3600) + len(data["symbols"]) / 24 / 6 / data["noOfDays"]) * xPixelsPerDay, height + 22 ab = AnnotationBbox(imagebox, xy=xyPos, xycoords='axes pixels', frameon=False) rainAxis.add_artist(ab) # Show city name in graph # TODO find a way to show the label in the background if showCityName: logging.debug("Adding city name to plot...") text = fig.text(1 - 7 / width, 1 - 20 / height, self.cityName, color='gray', ha='right', transform=rainAxis.transAxes) text.set_path_effects([ path_effects.Stroke(linewidth=self.textShadowWidth, foreground='white'), path_effects.Normal() ]) # Save the graph in a png image file logging.debug("Saving graph to %s" % outputFilename) plt.savefig(outputFilename, facecolor=colors["background"]) plt.close() # Write Meta Data if writeMetaData: logging.debug("Saving Meta Data to %s" % writeMetaData) metaData = {} metaData['city'] = self.cityName metaData['imageHeight'] = graphHeight metaData['imageWidth'] = graphWidth metaData['firstDayX'] = firstDayX metaData['firstDayY'] = firstDayY metaData['dayWidth'] = dayWidth metaData['dayHeight'] = dayHeight metaData['modelTimestamp'] = self.data[ "modelCalculationTimestamp"] # Seconds in UTC with open(writeMetaData, 'w') as metaFile: json.dump(metaData, metaFile)
def test_annotationbbox_extents(): plt.rcParams.update(plt.rcParamsDefault) fig, ax = plt.subplots(figsize=(4, 3), dpi=100) ax.axis([0, 1, 0, 1]) an1 = ax.annotate("Annotation", xy=(.9, .9), xytext=(1.1, 1.1), arrowprops=dict(arrowstyle="->"), clip_on=False, va="baseline", ha="left") da = DrawingArea(20, 20, 0, 0, clip=True) p = mpatches.Circle((-10, 30), 32) da.add_artist(p) ab3 = AnnotationBbox(da, [.5, .5], xybox=(-0.2, 0.5), xycoords='data', boxcoords="axes fraction", box_alignment=(0., .5), arrowprops=dict(arrowstyle="->")) ax.add_artist(ab3) im = OffsetImage(np.random.rand(10, 10), zoom=3) im.image.axes = ax ab6 = AnnotationBbox(im, (0.5, -.3), xybox=(0, 75), xycoords='axes fraction', boxcoords="offset points", pad=0.3, arrowprops=dict(arrowstyle="->")) ax.add_artist(ab6) fig.canvas.draw() renderer = fig.canvas.get_renderer() # Test Annotation bb1w = an1.get_window_extent(renderer) bb1e = an1.get_tightbbox(renderer) target1 = [332.9, 242.8, 467.0, 298.9] assert_allclose(bb1w.extents, target1, atol=2) assert_allclose(bb1e.extents, target1, atol=2) # Test AnnotationBbox bb3w = ab3.get_window_extent(renderer) bb3e = ab3.get_tightbbox(renderer) target3 = [-17.6, 129.0, 200.7, 167.9] assert_allclose(bb3w.extents, target3, atol=2) assert_allclose(bb3e.extents, target3, atol=2) bb6w = ab6.get_window_extent(renderer) bb6e = ab6.get_tightbbox(renderer) target6 = [180.0, -32.0, 230.0, 92.9] assert_allclose(bb6w.extents, target6, atol=2) assert_allclose(bb6e.extents, target6, atol=2) # Test bbox_inches='tight' buf = io.BytesIO() fig.savefig(buf, bbox_inches='tight') buf.seek(0) shape = plt.imread(buf).shape targetshape = (350, 504, 4) assert_allclose(shape, targetshape, atol=2) # Simple smoke test for tight_layout, to make sure it does not error out. fig.canvas.draw() fig.tight_layout() fig.canvas.draw()
def plot_tsne(X, Y, gt_Y, train_X, train_Y, filenames_val): fig = plt.figure() cmap = get_cmap(len(expressionNames) + 1) ax2 = fig.add_subplot(111) scs = [] legendNames = [] for id in np.unique(train_Y): idx = np.where(train_Y == id)[0] sc = ax2.scatter(train_X[idx, 1], train_X[idx, 0], color=cmap(id), alpha=0.4, s=75.0, linewidths=0) scs.append(sc) for id in np.unique(Y): print(len(np.where((Y == id))[0])) idx_correct = np.where((Y == id) & (Y == gt_Y))[0] idx_incorrect = np.where((Y == id) & (Y != gt_Y))[0] sc = ax2.scatter(X[idx_correct, 1], X[idx_correct, 0], color=cmap(id), marker='s', edgecolor='k') scs.append(sc) ax2.scatter(X[idx_incorrect, 1], X[idx_incorrect, 0], color=cmap(id), marker='v', edgecolor='k') expr = expressionNames[id] legendNames.append("{} ({})".format(expr, str(len(idx_correct)+len(idx_incorrect)))) plt.legend(scs, legendNames, scatterpoints=1, loc='lower left', ncol=4, fontsize=7) if True: for nImg, img_file in enumerate(filenames_val): is_correct = Y[nImg] == gt_Y[nImg] img_size = 36 img_path = os.path.join(affectNetRoot, img_file) print("{}: {}".format(nImg, img_path)) arr_img = cv2.imread(img_path) arr_img = cv2.cvtColor(arr_img, cv2.COLOR_BGR2RGB) arr_img = arr_img.astype(np.float)/255.0 arr_img = cv2.resize(arr_img, (img_size,img_size), interpolation=cv2.INTER_CUBIC) cv2.circle(arr_img, (img_size-6, 6), 3, (0,255,0) if is_correct else (255,0,0), -1) border_size = 2 da = DrawingArea(img_size+2*border_size, img_size+2*border_size, 0, 0) p = Rectangle((0, 0), img_size+2*border_size, img_size+2*border_size, color=cmap(Y[nImg])) da.add_artist(p) border = AnnotationBbox(da, X[nImg][::-1], #xybox=(120., -80.), xybox=(0., 0.), xycoords='data', boxcoords="offset points", pad=0.0, arrowprops=dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=90,rad=3") ) ax2.add_artist(border) im = OffsetImage(arr_img, interpolation='gaussian') ab = AnnotationBbox(im, X[nImg][::-1], #xybox=(120., -80.), xybox=(0., 0.), xycoords='data', boxcoords="offset points", pad=0.0, arrowprops=dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=90,rad=3") ) ax2.add_artist(ab)
def _tick_label( self, x, y, size, ax=None, linewidth=None, xybox=None, xycoords=None, box_alignment=None, horizontalalignment=None, verticalalignment=None, rotation=None, ): """ uses the icon as a ticklabel at location x Args: x (float) the horizontal position of the tick y (float) the vertical position of the tick size (float) scaling the size of the icon ax (matplotlib axis) the axis to put the label on """ ret_val = {} if ax is None: ax = plt.gca() tickline_color = self.color # np.any chokes on str input so need to test for this first if not (isinstance(tickline_color, str) or np.any(tickline_color)): tickline_color = [0.8, 0.8, 0.8] if self.final_image is not None: imagebox = OffsetImage(self.final_image, zoom=size, dpi_cor=True) ret_val['image'] = AnnotationBbox( imagebox, (x, y), xybox=xybox, xycoords=xycoords, box_alignment=box_alignment, boxcoords="offset points", bboxprops={ "edgecolor": "none", "facecolor": "none" }, arrowprops={ "linewidth": linewidth, "color": tickline_color, "arrowstyle": "-", "shrinkA": 0, "shrinkB": 1, }, pad=0.0, annotation_clip=False, ) zorder = ret_val['image'].zorder ax.add_artist(ret_val['image']) else: zorder = 0 if self.marker: if self.final_image is not None: markersize = max(self.final_image.size) else: markersize = 50 markersize = markersize * size d = DrawingArea(markersize, markersize) if self.marker_front: zorder_marker = zorder + 0.1 else: zorder_marker = zorder - 0.1 d.set_zorder(zorder_marker) d.set_alpha(0) if self.marker_front: d.add_artist( plt.Line2D( [markersize / 2], [markersize / 2], marker=self.marker, markeredgecolor=self.color, markerfacecolor=(0, 0, 0, 0), markersize=markersize, markeredgewidth=self.markeredgewidth, transform=d.get_transform(), zorder=zorder_marker, )) else: d.add_artist( plt.Line2D( [markersize / 2], [markersize / 2], marker=self.marker, markeredgecolor=self.color, markerfacecolor=self.color, markersize=markersize, markeredgewidth=self.markeredgewidth, transform=d.get_transform(), zorder=zorder_marker, )) ret_val['marker'] = AnnotationBbox( d, (x, y), xybox=xybox, xycoords=xycoords, box_alignment=box_alignment, boxcoords="offset points", bboxprops={ "edgecolor": "none", "facecolor": "none" }, arrowprops={ "linewidth": linewidth, "color": tickline_color, "arrowstyle": "-", "shrinkA": 0, "shrinkB": 1, }, pad=0.0, annotation_clip=False, ) ret_val['marker'].set_zorder(zorder_marker) ret_val['marker'].set_alpha(0) ax.add_artist(ret_val['marker']) if self.string is not None: ret_val['string'] = ax.annotate( self.string, (x, y), xytext=xybox, xycoords=xycoords, textcoords="offset points", horizontalalignment=horizontalalignment, verticalalignment=verticalalignment, arrowprops={ "linewidth": linewidth, "color": tickline_color, "arrowstyle": "-", "shrinkA": 0, "shrinkB": 1, }, zorder=zorder + 0.2, fontsize=self.font_size, fontname=self.font_name, color=self.font_color, rotation=rotation, ) return ret_val
def plot_maze_abstract_transitions( all_inputs, all_abs_inputs, model, global_step, plot_dir ): """Plots the abstract representation from the CRAR agent for the SimpleMaze environment. Heavily borrowed from: https://github.com/VinF/deer/blob/master/examples/test_CRAR/simple_maze_env.py """ if not isinstance(plot_dir, Path): plot_dir = Path(plot_dir) exp_seq = list(reversed(most_recent(model.replay_buffer.buffer, 1000))) n = 1000 history = [] for i, (obs, *_) in enumerate(exp_seq): history.append(obs) history = np.array(history) abstract_states = model.agent.encode(history) m = cm.ScalarMappable(cmap=cm.jet) x, y = abstract_states.detach().cpu().numpy().T fig = plt.figure() ax = fig.add_subplot(111) ax.set_xlabel(r"$X_1$") ax.set_ylabel(r"$X_2$") for i in range(n - 1): predicted1 = ( model.agent.compute_transition( abstract_states[i : i + 1], torch.as_tensor([0], device="cuda") ) .detach() .cpu() .numpy() ) predicted2 = ( ( model.agent.compute_transition( abstract_states[i : i + 1], torch.as_tensor([1], device="cuda") ) ) .detach() .cpu() .numpy() ) predicted3 = ( ( model.agent.compute_transition( abstract_states[i : i + 1], torch.as_tensor([2], device="cuda") ) ) .detach() .cpu() .numpy() ) predicted4 = ( ( model.agent.compute_transition( abstract_states[i : i + 1], torch.as_tensor([3], device="cuda") ) ) .detach() .cpu() .numpy() ) ax.plot( np.concatenate([x[i : i + 1], predicted1[0, :1]]), np.concatenate([y[i : i + 1], predicted1[0, 1:2]]), color="royalblue", alpha=0.75, ) ax.plot( np.concatenate([x[i : i + 1], predicted2[0, :1]]), np.concatenate([y[i : i + 1], predicted2[0, 1:2]]), color="crimson", alpha=0.75, ) ax.plot( np.concatenate([x[i : i + 1], predicted3[0, :1]]), np.concatenate([y[i : i + 1], predicted3[0, 1:2]]), color="mediumspringgreen", alpha=0.75, ) ax.plot( np.concatenate([x[i : i + 1], predicted4[0, :1]]), np.concatenate([y[i : i + 1], predicted4[0, 1:2]]), color="black", alpha=0.75, ) # Plot the dots at each time step depending on the action taken length_block = [[0, 18], [18, 19], [19, 31]] for i in range(3): colors = ["blue", "orange", "green"] line3 = ax.scatter( all_abs_inputs[length_block[i][0] : length_block[i][1], 0], all_abs_inputs[length_block[i][0] : length_block[i][1], 1], c=colors[i], marker="x", edgecolors="k", alpha=0.5, s=100, ) axes_lims = [ax.get_xlim(), ax.get_ylim()] box1b = TextArea( " Estimated transitions (action 0, 1, 2 and 3): ", textprops=dict(color="k") ) box2b = DrawingArea(90, 20, 0, 0) el1b = Rectangle((5, 10), 15, 2, fc="royalblue", alpha=0.75) el2b = Rectangle((25, 10), 15, 2, fc="crimson", alpha=0.75) el3b = Rectangle((45, 10), 15, 2, fc="mediumspringgreen", alpha=0.75) el4b = Rectangle((65, 10), 15, 2, fc="black", alpha=0.75) box2b.add_artist(el1b) box2b.add_artist(el2b) box2b.add_artist(el3b) box2b.add_artist(el4b) boxb = HPacker(children=[box1b, box2b], align="center", pad=0, sep=5) anchored_box = AnchoredOffsetbox( loc=3, child=boxb, pad=0.0, frameon=True, bbox_to_anchor=(0.0, 0.98), bbox_transform=ax.transAxes, borderpad=0.0, ) ax.add_artist(anchored_box) plot_dir.mkdir(parents=True, exist_ok=True) plt.savefig(plot_dir / f"plot_{global_step}.pdf")
def add_sketch(self, **kwargs): """Draw section sketch at current value [beta, alpha]. Draw also: * potential preferential fault network; * slip directions on fault network. """ self.sketch_size_factor = kwargs.get("sketch_size_factor", 1.0) # Renaming is cheapper than multiple access. alpha, beta = self._alpha, self._beta a_deg, b_deg = self.alpha, self.beta padding = self._padding # Surface of prism : arbitrary set, allows a cst looking. # Box distance from enveloppe. box_dist_from_curve = (self._point_top[1] - self._point_bottom[1]) / 10.0 try: L = sqrt(self._sketch_surface / sin((alpha + beta) / 2.0) * cos( (alpha + beta) / 2.0)) except ZeroDivisionError: # alpha + beta == 0. means there is no prism ! return self._L = L # Init sketching aera ans draw background of prism. # Prism is a basal and a topo line, so discribed by 3 points. if alpha < 0.0: x1 = padding + 2.0 * L * sin((alpha + beta) / 2.0) * sin( (beta - alpha) / 2.0) y1 = padding elif beta < 0.0: x1, y1 = padding, padding + L * sin(-beta) else: x1, y1 = padding + L * (1.0 - cos(beta)), padding x2, y2 = x1 + L * cos(beta), y1 + L * sin(beta) x3, y3 = x2 - L * cos(alpha), y2 + L * sin(alpha) self._prism_tip = (x2, y2) # Prism is also discribed by two lines. # Slope of vectors [1-2] and [2-3] self._a_basal, self._a_topo = tan(beta), -tan(alpha) # Initial ordinates self._b_basal = y2 - self._a_basal * x2 self._b_topo = y2 - self._a_topo * x2 self._sketch_box_width = x2 + padding self._sketch_box_height = max(y3, y2) - min(y1, y2) + 2 * padding # Content of annotationbox self.drawing_aera = DrawingArea(self._sketch_box_width, self._sketch_box_height, 0.0, 0.0) # Fill the prism XY = [[x1, y1], [x2, y2]] for angle in np.arange(alpha, -beta, -(alpha + beta) / 1.0e2): XY.append([x2 - L * cos(angle), y2 + L * sin(angle)]) p = patches.Polygon(XY, edgecolor="none", facecolor="w") self.drawing_aera.add_artist(p) # Identify wich part of critical enveloppe is concerned. slope = abs( atan((self._point_center[1] - a_deg) / (self._point_center[0] - b_deg))) dist_from_curve_b = box_dist_from_curve * cos(slope) dist_from_curve_a = box_dist_from_curve * sin(slope) (alpha1, ), (alpha2, ) = self.compute_alpha(deg=False) a_mid = alpha1 + (alpha2 - alpha1) / 2.0 if alpha <= a_mid: # bottom part -> inverse faults if b_deg < self._point_bottom[0]: # bottom left quadrant quadrant, solution = "BL", "B" else: # bottom right quadrant quadrant, solution = "BR", "A" else: # upper part -> normal faults if b_deg < self._point_top[0]: # Top left quadrant quadrant, solution = "TL", "A" else: # Top right quadrant quadrant, solution = "TR", "B" if solution == "A": g = self._get_gamma_A() t = self._get_theta_A() else: g = self._get_gamma_B() t = self._get_theta_B() if quadrant == "TL": box_alignment, xshift, yshift = (1.0, 0.0), -1.0, 1.0 elif quadrant == "TR": box_alignment, xshift, yshift = (0.0, 0.0), 1.0, 1.0 elif quadrant == "BL": box_alignment, xshift, yshift = (1.0, 1.0), -1.0, -1.0 elif quadrant == "BR": box_alignment, xshift, yshift = (0.0, 1.0), 1.0, -1.0 # Fault network. xgap = self._fault_gap * cos((beta - alpha) / 2.0) ygap = self._fault_gap * sin((beta - alpha) / 2.0) L_A, L_B, angle = L / 3.0, L * 2.0 / 3.0, alpha - (alpha + beta) / 2.0 # Gamma oriented faults L_g = L_A if quadrant in ["TL", "BL"] else L_B x_g, y_g = x2 - L_g * cos(angle), y2 + L_g * sin(angle) xgap_g = xgap / sin(g - (beta - alpha) / 2.0) ygap_g = ygap / sin(g - (beta - alpha) / 2.0) a_g = tan(g) self._draw_faults(a_g, x_g, y_g, xgap_g, ygap_g, -1, 1) self._draw_faults(a_g, x_g, y_g, xgap_g, ygap_g, 0, -1) # Theta oriented faults L_t = L_B if quadrant in ["TL", "BL"] else L_A x_t, y_t = x2 - L_t * cos(angle), y2 + L_t * sin(angle) xgap_t = xgap / sin(t + (beta - alpha) / 2.0) ygap_t = ygap / sin(t + (beta - alpha) / 2.0) a_t = -tan(t) # Fault slope theta self._draw_faults(a_t, x_t, y_t, xgap_t, ygap_t, -1, 1) self._draw_faults(a_t, x_t, y_t, xgap_t, ygap_t, 0, -1) # Prism limits. # Drawed above faults to mask faults tips. p = lines.Line2D([x1, x2, x3], [y1, y2, y3], lw=2, color="gray") self.drawing_aera.add_artist(p) # Arrows. # Gamma oriented inverse arrows. self._draw_arrow(g, x_g, y_g, solution, gamma=True) # Theta oriented inverse arrows. self._draw_arrow(t, x_t, y_t, solution, gamma=False) # arrows base x, y = x1 + L * cos(beta) / 2.0, y1 + L * sin(beta) / 2.0 if self.context == "Compression": solution = "B" else: solution = "A" self._draw_arrow(beta, x, y, solution, gamma=True) # Set and display annotation box. ab = AnnotationBbox( self.drawing_aera, [b_deg, a_deg], xybox=( b_deg + xshift * dist_from_curve_b, a_deg + yshift * dist_from_curve_a, ), xycoords="data", boxcoords=("data", "data"), box_alignment=box_alignment, bboxprops=dict(boxstyle="round", fc=(0.9, 0.9, 0.9), ec="none"), arrowprops=dict( arrowstyle="wedge,tail_width=2.", fc=(0.9, 0.9, 0.9), ec=(0.8, 0.8, 0.8), patchA=None, relpos=(0.5, 0.5), ), ) self.axe.add_artist(ab).draggable()
cbar = plt.colorbar(im, cax=cax, norm=norm, label=unit) cbar.set_label(unit, size=24) cbar.ax.tick_params(labelsize=20) if var == "acc": cbar.cmap.set_under('w') cbar.set_clim(0, cmap_range_max) print(var, cmap_range_min, cmap_range_max) #, data_array if var == "comp": norm = mpl.colors.Normalize(vmin=cmap_range_min, vmax=cmap_range_max) im = ax.imshow(data_array, cmap=var_cmap, norm=norm) # im = ax.imshow(data_array, cmap=var_cmap) box1 = DrawingArea(50, 20, 0, 0) rt1 = plt.Rectangle((0, 5), 20, 13, fc="#1f78b4") rt2 = plt.Rectangle((25, 5), 20, 13, fc="#a6cee3") box1.add_artist(rt1) box1.add_artist(rt2) box2 = TextArea("Solid silicates", textprops=dict(color="k", size=20)) box = HPacker(children=[box1, box2], align="center", pad=0, sep=5) anchored_box1 = AnchoredOffsetbox( loc=2, child=box, pad=0., frameon=False,
def _init_legend_box(self, handles, labels, markerfirst=True): """ Initialize the legend_box. The legend_box is an instance of the OffsetBox, which is packed with legend handles and texts. Once packed, their location is calculated during the drawing time. """ fontsize = self._fontsize # legend_box is a HPacker, horizontally packed with columns. # Each column is a VPacker, vertically packed with legend items. # Each legend item is a HPacker packed with: # - handlebox: a DrawingArea which contains the legend handle. # - labelbox: a TextArea which contains the legend text. text_list = [] # the list of text instances handle_list = [] # the list of handle instances handles_and_labels = [] # The approximate height and descent of text. These values are # only used for plotting the legend handle. descent = 0.35 * fontsize * (self.handleheight - 0.7) # heuristic. height = fontsize * self.handleheight - descent # each handle needs to be drawn inside a box of (x, y, w, h) = # (0, -descent, width, height). And their coordinates should # be given in the display coordinates. # The transformation of each handle will be automatically set # to self.get_transform(). If the artist does not use its # default transform (e.g., Collections), you need to # manually set their transform to the self.get_transform(). legend_handler_map = self.get_legend_handler_map() for orig_handle, label in zip(handles, labels): handler = self.get_legend_handler(legend_handler_map, orig_handle) if handler is None: _api.warn_external( "Legend does not support {!r} instances.\nA proxy artist " "may be used instead.\nSee: " "https://matplotlib.org/users/legend_guide.html" "#creating-artists-specifically-for-adding-to-the-legend-" "aka-proxy-artists".format(orig_handle)) # No handle for this artist, so we just defer to None. handle_list.append(None) else: textbox = TextArea(label, multilinebaseline=True, textprops=dict(verticalalignment='baseline', horizontalalignment='left', fontproperties=self.prop)) handlebox = DrawingArea(width=self.handlelength * fontsize, height=height, xdescent=0., ydescent=descent) text_list.append(textbox._text) # Create the artist for the legend which represents the # original artist/handle. handle_list.append( handler.legend_artist(self, orig_handle, fontsize, handlebox)) handles_and_labels.append((handlebox, textbox)) columnbox = [] # array_split splits n handles_and_labels into ncol columns, with the # first n%ncol columns having an extra entry. filter(len, ...) handles # the case where n < ncol: the last ncol-n columns are empty and get # filtered out. for handles_and_labels_column \ in filter(len, np.array_split(handles_and_labels, self._ncol)): # pack handlebox and labelbox into itembox itemboxes = [ HPacker(pad=0, sep=self.handletextpad * fontsize, children=[h, t] if markerfirst else [t, h], align="baseline") for h, t in handles_and_labels_column ] # pack columnbox alignment = "baseline" if markerfirst else "right" columnbox.append( VPacker(pad=0, sep=self.labelspacing * fontsize, align=alignment, children=itemboxes)) mode = "expand" if self._mode == "expand" else "fixed" sep = self.columnspacing * fontsize self._legend_handle_box = HPacker(pad=0, sep=sep, align="baseline", mode=mode, children=columnbox) self._legend_title_box = TextArea("") self._legend_box = VPacker( pad=self.borderpad * fontsize, sep=self.labelspacing * fontsize, align="center", children=[self._legend_title_box, self._legend_handle_box]) self._legend_box.set_figure(self.figure) self._legend_box.axes = self.axes self.texts = text_list self.legendHandles = handle_list
def _init_legend_box(self, handles, labels): """ Initiallize the legend_box. The legend_box is an instance of the OffsetBox, which is packed with legend handles and texts. Once packed, their location is calculated during the drawing time. """ fontsize = self.fontsize # legend_box is a HPacker, horizontally packed with # columns. Each column is a VPacker, vertically packed with # legend items. Each legend item is HPacker packed with # legend handleBox and labelBox. handleBox is an instance of # offsetbox.DrawingArea which contains legend handle. labelBox # is an instance of offsetbox.TextArea which contains legend # text. text_list = [] # the list of text instances handle_list = [] # the list of text instances label_prop = dict( verticalalignment='baseline', horizontalalignment='left', fontproperties=self.prop, ) labelboxes = [] for l in labels: textbox = TextArea(l, textprops=label_prop, multilinebaseline=True, minimumdescent=True) text_list.append(textbox._text) labelboxes.append(textbox) handleboxes = [] # The approximate height and descent of text. These values are # only used for plotting the legend handle. height = self._approx_text_height() * 0.7 descent = 0. # each handle needs to be drawn inside a box of (x, y, w, h) = # (0, -descent, width, height). And their corrdinates should # be given in the display coordinates. # NOTE : the coordinates will be updated again in # _update_legend_box() method. # The transformation of each handle will be automatically set # to self.get_trasnform(). If the artist does not uses its # default trasnform (eg, Collections), you need to # manually set their transform to the self.get_transform(). for handle in handles: if isinstance(handle, RegularPolyCollection): npoints = self.scatterpoints else: npoints = self.numpoints if npoints > 1: # we put some pad here to compensate the size of the # marker xdata = np.linspace(0.3 * fontsize, (self.handlelength - 0.3) * fontsize, npoints) xdata_marker = xdata elif npoints == 1: xdata = np.linspace(0, self.handlelength * fontsize, 2) xdata_marker = [0.5 * self.handlelength * fontsize] if isinstance(handle, Line2D): ydata = ((height - descent) / 2.) * np.ones(xdata.shape, float) legline = Line2D(xdata, ydata) legline.update_from(handle) self._set_artist_props(legline) # after update legline.set_clip_box(None) legline.set_clip_path(None) legline.set_drawstyle('default') legline.set_marker('None') handle_list.append(legline) legline_marker = Line2D(xdata_marker, ydata[:len(xdata_marker)]) legline_marker.update_from(handle) self._set_artist_props(legline_marker) legline_marker.set_clip_box(None) legline_marker.set_clip_path(None) legline_marker.set_linestyle('None') # we don't want to add this to the return list because # the texts and handles are assumed to be in one-to-one # correpondence. legline._legmarker = legline_marker elif isinstance(handle, Patch): p = Rectangle( xy=(0., 0.), width=self.handlelength * fontsize, height=(height - descent), ) p.update_from(handle) self._set_artist_props(p) p.set_clip_box(None) p.set_clip_path(None) handle_list.append(p) elif isinstance(handle, LineCollection): ydata = ((height - descent) / 2.) * np.ones(xdata.shape, float) legline = Line2D(xdata, ydata) self._set_artist_props(legline) legline.set_clip_box(None) legline.set_clip_path(None) lw = handle.get_linewidth()[0] dashes = handle.get_dashes()[0] color = handle.get_colors()[0] legline.set_color(color) legline.set_linewidth(lw) legline.set_dashes(dashes) handle_list.append(legline) elif isinstance(handle, RegularPolyCollection): #ydata = self._scatteryoffsets ydata = height * self._scatteryoffsets size_max, size_min = max(handle.get_sizes()),\ min(handle.get_sizes()) # we may need to scale these sizes by "markerscale" # attribute. But other handle types does not seem # to care about this attribute and it is currently ignored. if self.scatterpoints < 4: sizes = [.5 * (size_max + size_min), size_max, size_min] else: sizes = (size_max - size_min) * np.linspace( 0, 1, self.scatterpoints) + size_min p = type(handle)( handle.get_numsides(), rotation=handle.get_rotation(), sizes=sizes, offsets=zip(xdata_marker, ydata), transOffset=self.get_transform(), ) p.update_from(handle) p.set_figure(self.figure) p.set_clip_box(None) p.set_clip_path(None) handle_list.append(p) else: handle_list.append(None) handlebox = DrawingArea(width=self.handlelength * fontsize, height=height, xdescent=0., ydescent=descent) handle = handle_list[-1] handlebox.add_artist(handle) if hasattr(handle, "_legmarker"): handlebox.add_artist(handle._legmarker) handleboxes.append(handlebox) # We calculate number of lows in each column. The first # (num_largecol) columns will have (nrows+1) rows, and remaing # (num_smallcol) columns will have (nrows) rows. nrows, num_largecol = divmod(len(handleboxes), self._ncol) num_smallcol = self._ncol - num_largecol # starting index of each column and number of rows in it. largecol = safezip(range(0, num_largecol * (nrows + 1), (nrows + 1)), [nrows + 1] * num_largecol) smallcol = safezip( range(num_largecol * (nrows + 1), len(handleboxes), nrows), [nrows] * num_smallcol) handle_label = safezip(handleboxes, labelboxes) columnbox = [] for i0, di in largecol + smallcol: # pack handleBox and labelBox into itemBox itemBoxes = [ HPacker(pad=0, sep=self.handletextpad * fontsize, children=[h, t], align="baseline") for h, t in handle_label[i0:i0 + di] ] # minimumdescent=False for the text of the last row of the column itemBoxes[-1].get_children()[1].set_minimumdescent(False) # pack columnBox columnbox.append( VPacker(pad=0, sep=self.labelspacing * fontsize, align="baseline", children=itemBoxes)) if self._mode == "expand": mode = "expand" else: mode = "fixed" sep = self.columnspacing * fontsize self._legend_box = HPacker(pad=self.borderpad * fontsize, sep=sep, align="baseline", mode=mode, children=columnbox) self._legend_box.set_figure(self.figure) self.texts = text_list self.legendHandles = handle_list
def __init__(self, width, height, xdescent, ydescent, loc, pad=0.4, borderpad=0.5, prop=None, frameon=True, **kwargs): """ An anchored container with a fixed size and fillable DrawingArea. Artists added to the *drawing_area* will have their coordinates interpreted as pixels. Any transformations set on the artists will be overridden. Parameters ---------- width, height : float width and height of the container, in pixels. xdescent, ydescent : float descent of the container in the x- and y- direction, in pixels. loc : int Location of this artist. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : float, optional, default: 0.4 Padding around the child objects, in fraction of the font size. borderpad : float, optional, default: 0.5 Border padding, in fraction of the font size. prop : `matplotlib.font_manager.FontProperties`, optional Font property used as a reference for paddings. frameon : bool, optional, default: True If True, draw a box around this artists. **kwargs Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- drawing_area : `matplotlib.offsetbox.DrawingArea` A container for artists to display. Examples -------- To display blue and red circles of different sizes in the upper right of an axes *ax*: >>> ada = AnchoredDrawingArea(20, 20, 0, 0, ... loc='upper right', frameon=False) >>> ada.drawing_area.add_artist(Circle((10, 10), 10, fc="b")) >>> ada.drawing_area.add_artist(Circle((30, 10), 5, fc="r")) >>> ax.add_artist(ada) """ self.da = DrawingArea(width, height, xdescent, ydescent) self.drawing_area = self.da super().__init__(loc, pad=pad, borderpad=borderpad, child=self.da, prop=None, frameon=frameon, **kwargs)
def summarizePerformance(self, test_data_set, learning_algo, *args, **kwargs): """ Plot of the low-dimensional representation of the environment built by the model """ all_possib_inp = [ ] # Will store all possible inputs (=observation) for the CRAR agent labels_maze = [] self.create_map() for y_a in range(self._size_maze): for x_a in range(self._size_maze): state = copy.deepcopy(self._map) state[self._size_maze // 2, self._size_maze // 2] = 0 if (state[x_a, y_a] == 0): if (self._higher_dim_obs == True): all_possib_inp.append( self.get_higher_dim_obs([[x_a, y_a]], [self._pos_goal])) else: state[x_a, y_a] = 0.5 all_possib_inp.append(state) ## labels #if(y_a<self._size_maze//2): # labels_maze.append(0.) #elif(y_a==self._size_maze//2): # labels_maze.append(1.) #else: # labels_maze.append(2.) #arr=np.array(all_possib_inp) #if(self._higher_dim_obs==False): # arr=arr.reshape(arr.shape[0],-1) #else: # arr=arr.reshape(arr.shape[0],-1) # #np.savetxt('tsne_python/mazesH_X.txt',arr.reshape(arr.shape[0],-1)) #np.savetxt('tsne_python/mazesH_labels.txt',np.array(labels_maze)) all_possib_inp = np.expand_dims(np.array(all_possib_inp, dtype='float'), axis=1) all_possib_abs_states = learning_algo.encoder.predict(all_possib_inp) if (all_possib_abs_states.ndim == 4): all_possib_abs_states = np.transpose( all_possib_abs_states, (0, 3, 1, 2)) # data_format='channels_last' --> 'channels_first' n = 1000 historics = [] for i, observ in enumerate(test_data_set.observations()[0][0:n]): historics.append(np.expand_dims(observ, axis=0)) historics = np.array(historics) abs_states = learning_algo.encoder.predict(historics) if (abs_states.ndim == 4): abs_states = np.transpose( abs_states, (0, 3, 1, 2)) # data_format='channels_last' --> 'channels_first' actions = test_data_set.actions()[0:n] if self.inTerminalState() == False: self._mode_episode_count += 1 print("== Mean score per episode is {} over {} episodes ==".format( self._mode_score / (self._mode_episode_count + 0.0001), self._mode_episode_count)) m = cm.ScalarMappable(cmap=cm.jet) x = np.array(abs_states)[:, 0] y = np.array(abs_states)[:, 1] if (self.intern_dim > 2): z = np.array(abs_states)[:, 2] fig = plt.figure() if (self.intern_dim == 2): ax = fig.add_subplot(111) ax.set_xlabel(r'$X_1$') ax.set_ylabel(r'$X_2$') else: ax = fig.add_subplot(111, projection='3d') ax.set_xlabel(r'$X_1$') ax.set_ylabel(r'$X_2$') ax.set_zlabel(r'$X_3$') # Plot the estimated transitions for i in range(n - 1): predicted1 = learning_algo.transition.predict( [abs_states[i:i + 1], np.array([[1, 0, 0, 0]])]) predicted2 = learning_algo.transition.predict( [abs_states[i:i + 1], np.array([[0, 1, 0, 0]])]) predicted3 = learning_algo.transition.predict( [abs_states[i:i + 1], np.array([[0, 0, 1, 0]])]) predicted4 = learning_algo.transition.predict( [abs_states[i:i + 1], np.array([[0, 0, 0, 1]])]) if (self.intern_dim == 2): ax.plot(np.concatenate([x[i:i + 1], predicted1[0, :1]]), np.concatenate([y[i:i + 1], predicted1[0, 1:2]]), color="0.9", alpha=0.75) ax.plot(np.concatenate([x[i:i + 1], predicted2[0, :1]]), np.concatenate([y[i:i + 1], predicted2[0, 1:2]]), color="0.65", alpha=0.75) ax.plot(np.concatenate([x[i:i + 1], predicted3[0, :1]]), np.concatenate([y[i:i + 1], predicted3[0, 1:2]]), color="0.4", alpha=0.75) ax.plot(np.concatenate([x[i:i + 1], predicted4[0, :1]]), np.concatenate([y[i:i + 1], predicted4[0, 1:2]]), color="0.15", alpha=0.75) else: ax.plot(np.concatenate([x[i:i + 1], predicted1[0, :1]]), np.concatenate([y[i:i + 1], predicted1[0, 1:2]]), np.concatenate([z[i:i + 1], predicted1[0, 2:3]]), color="0.9", alpha=0.75) ax.plot(np.concatenate([x[i:i + 1], predicted2[0, :1]]), np.concatenate([y[i:i + 1], predicted2[0, 1:2]]), np.concatenate([z[i:i + 1], predicted2[0, 2:3]]), color="0.65", alpha=0.75) ax.plot(np.concatenate([x[i:i + 1], predicted3[0, :1]]), np.concatenate([y[i:i + 1], predicted3[0, 1:2]]), np.concatenate([z[i:i + 1], predicted3[0, 2:3]]), color="0.4", alpha=0.75) ax.plot(np.concatenate([x[i:i + 1], predicted4[0, :1]]), np.concatenate([y[i:i + 1], predicted4[0, 1:2]]), np.concatenate([z[i:i + 1], predicted4[0, 2:3]]), color="0.15", alpha=0.75) # Plot the dots at each time step depending on the action taken length_block = [[0, 18], [18, 19], [19, 31]] for i in range(3): colors = ['blue', 'orange', 'green'] if (self.intern_dim == 2): line3 = ax.scatter(all_possib_abs_states[ length_block[i][0]:length_block[i][1], 0], all_possib_abs_states[ length_block[i][0]:length_block[i][1], 1], c=colors[i], marker='x', edgecolors='k', alpha=0.5, s=100) else: line3 = ax.scatter(all_possib_abs_states[ length_block[i][0]:length_block[i][1], 0], all_possib_abs_states[ length_block[i][0]:length_block[i][1], 1], all_possib_abs_states[ length_block[i][0]:length_block[i][1], 2], marker='x', depthshade=True, edgecolors='k', alpha=0.5, s=50) if (self.intern_dim == 2): axes_lims = [ax.get_xlim(), ax.get_ylim()] else: axes_lims = [ax.get_xlim(), ax.get_ylim(), ax.get_zlim()] # Plot the legend for transition estimates box1b = TextArea(" Estimated transitions (action 0, 1, 2 and 3): ", textprops=dict(color="k")) box2b = DrawingArea(90, 20, 0, 0) el1b = Rectangle((5, 10), 15, 2, fc="0.9", alpha=0.75) el2b = Rectangle((25, 10), 15, 2, fc="0.65", alpha=0.75) el3b = Rectangle((45, 10), 15, 2, fc="0.4", alpha=0.75) el4b = Rectangle((65, 10), 15, 2, fc="0.15", alpha=0.75) box2b.add_artist(el1b) box2b.add_artist(el2b) box2b.add_artist(el3b) box2b.add_artist(el4b) boxb = HPacker(children=[box1b, box2b], align="center", pad=0, sep=5) anchored_box = AnchoredOffsetbox( loc=3, child=boxb, pad=0., frameon=True, bbox_to_anchor=(0., 0.98), bbox_transform=ax.transAxes, borderpad=0., ) ax.add_artist(anchored_box) #plt.show() plt.savefig('fig_base' + str(learning_algo.update_counter) + '.pdf') # # Plot the Q_vals # c = learning_algo.Q.predict(np.concatenate((np.expand_dims(x,axis=1),np.expand_dims(y,axis=1),np.expand_dims(z,axis=1)),axis=1)) # #print "actions,C" # #print actions # #print c # #c=np.max(c,axis=1) # m1=ax.scatter(x, y, z+zrange/20, c=c[:,0], vmin=-1., vmax=1., cmap=plt.cm.RdYlGn) # m2=ax.scatter(x, y, z+3*zrange/40, c=c[:,1], vmin=-1., vmax=1., cmap=plt.cm.RdYlGn) # # #plt.colorbar(m3) # ax2 = fig.add_axes([0.85, 0.15, 0.025, 0.7]) # cmap = matplotlib.cm.RdYlGn # norm = matplotlib.colors.Normalize(vmin=-1, vmax=1) # # # ColorbarBase derives from ScalarMappable and puts a colorbar # # in a specified axes, so it has everything needed for a # # standalone colorbar. There are many more kwargs, but the # # following gives a basic continuous colorbar with ticks # # and labels. # cb1 = matplotlib.colorbar.ColorbarBase(ax2, cmap=cmap,norm=norm,orientation='vertical') # cb1.set_label('Estimated expected return') # # #plt.show() # plt.savefig('fig_w_V'+str(learning_algo.update_counter)+'.pdf') # # # # fig_visuV # fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # # x = np.array([i for i in range(5) for jk in range(25)])/4.*(axes_lims[0][1]-axes_lims[0][0])+axes_lims[0][0] # y = np.array([j for i in range(5) for j in range(5) for k in range(5)])/4.*(axes_lims[1][1]-axes_lims[1][0])+axes_lims[1][0] # z = np.array([k for i in range(5) for j in range(5) for k in range(5)])/4.*(axes_lims[2][1]-axes_lims[2][0])+axes_lims[2][0] # # c = learning_algo.Q.predict(np.concatenate((np.expand_dims(x,axis=1),np.expand_dims(y,axis=1),np.expand_dims(z,axis=1)),axis=1)) # c=np.max(c,axis=1) # #print "c" # #print c # # m=ax.scatter(x, y, z, c=c, vmin=-1., vmax=1., cmap=plt.hot()) # #plt.colorbar(m) # fig.subplots_adjust(right=0.8) # ax2 = fig.add_axes([0.875, 0.15, 0.025, 0.7]) # cmap = matplotlib.cm.hot # norm = matplotlib.colors.Normalize(vmin=-1, vmax=1) # # # ColorbarBase derives from ScalarMappable and puts a colorbar # # in a specified axes, so it has everything needed for a # # standalone colorbar. There are many more kwargs, but the # # following gives a basic continuous colorbar with ticks # # and labels. # cb1 = matplotlib.colorbar.ColorbarBase(ax2, cmap=cmap,norm=norm,orientation='vertical') # cb1.set_label('Estimated expected return') # # #plt.show() # plt.savefig('fig_visuV'+str(learning_algo.update_counter)+'.pdf') # # # # fig_visuR # fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # # x = np.array([i for i in range(5) for jk in range(25)])/4.*(axes_lims[0][1]-axes_lims[0][0])+axes_lims[0][0] # y = np.array([j for i in range(5) for j in range(5) for k in range(5)])/4.*(axes_lims[1][1]-axes_lims[1][0])+axes_lims[1][0] # z = np.array([k for i in range(5) for j in range(5) for k in range(5)])/4.*(axes_lims[2][1]-axes_lims[2][0])+axes_lims[2][0] # # coords=np.concatenate((np.expand_dims(x,axis=1),np.expand_dims(y,axis=1),np.expand_dims(z,axis=1)),axis=1) # repeat_nactions_coord=np.repeat(coords,self.nActions(),axis=0) # identity_matrix = np.diag(np.ones(self.nActions())) # tile_identity_matrix=np.tile(identity_matrix,(5*5*5,1)) # # c = learning_algo.R.predict([repeat_nactions_coord,tile_identity_matrix]) # c=np.max(np.reshape(c,(125,self.nActions())),axis=1) # #print "c" # #print c # #mini=np.min(c) # #maxi=np.max(c) # # m=ax.scatter(x, y, z, c=c, vmin=-1., vmax=1., cmap=plt.hot()) # #plt.colorbar(m) # fig.subplots_adjust(right=0.8) # ax2 = fig.add_axes([0.875, 0.15, 0.025, 0.7]) # cmap = matplotlib.cm.hot # norm = matplotlib.colors.Normalize(vmin=-1, vmax=1) # # # ColorbarBase derives from ScalarMappable and puts a colorbar # # in a specified axes, so it has everything needed for a # # standalone colorbar. There are many more kwargs, but the # # following gives a basic continuous colorbar with ticks # # and labels. # cb1 = matplotlib.colorbar.ColorbarBase(ax2, cmap=cmap,norm=norm,orientation='vertical') # cb1.set_label('Estimated expected return') # # #plt.show() # plt.savefig('fig_visuR'+str(learning_algo.update_counter)+'.pdf') matplotlib.pyplot.close("all") # avoids memory leaks
def plot_rst(xList, yList, fnameList): '''docstring for plot_rst()''' fig = plt.gcf() fig.clf() ax = plt.subplot2grid((5, 1), (0, 0), rowspan=4) #ax = plt.subplot(111) xy = (0.5, 0.7) offsetbox = TextArea("Test", minimumdescent=False) ab = AnnotationBbox(offsetbox, xy, xybox=(1.02, xy[1]), xycoords='data', boxcoords=("axes fraction", "data"), box_alignment=(0., 0.5), arrowprops=dict(arrowstyle="->")) ax.add_artist(ab) from matplotlib.patches import Circle da = DrawingArea(20, 20, 0, 0) p = Circle((10, 10), 10) da.add_artist(p) xy = [0.3, 0.55] ab = AnnotationBbox(da, xy, xybox=(1.02, xy[1]), xycoords='data', boxcoords=("axes fraction", "data"), box_alignment=(0., 0.5), arrowprops=dict(arrowstyle="->")) #arrowprops=None) ax.add_artist(ab) # another image from matplotlib._png import read_png #fn = get_sample_data("./61.png", asfileobj=False) arr_lena = read_png("./61.png") imagebox = OffsetImage(arr_lena, zoom=0.2) xy = (0.1, 0.1) print fnameList for i in range(0, len(fnameList)): ax.add_artist( AnnotationBbox( imagebox, xy, xybox=(0.1 + i * 0.2, -0.15), xycoords='data', boxcoords=("axes fraction", "data"), #boxcoords="offset points", pad=0.1, arrowprops=dict( arrowstyle="->", connectionstyle="angle,angleA=0,angleB=90,rad=3"))) ax.set_xlim(0, 1) ax.set_ylim(0, 1) plt.draw() plt.show()
def __init__(self, width, height, xdescent, ydescent, loc, pad=0.4, borderpad=0.5, prop=None, frameon=True): self.da = DrawingArea(width, height, xdescent, ydescent) super().__init__(loc, pad=pad, borderpad=borderpad, child=self.da, prop=None, frameon=frameon)
def y_tick_label(self, y, size, offset=7, ax=None): """ uses the icon as a ticklabel at location x Args: y (float) the position of the tick size (float) scaling the size of the icon offset (integer) how far the icon should be from the axis in points ax (matplotlib axis) the axis to put the label on """ if ax is None: ax = plt.gca() if self.final_image is not None: imagebox = OffsetImage(self.final_image, zoom=size) ab = AnnotationBbox(imagebox, (0, y), xybox=(-offset, 0), xycoords=('axes fraction', 'data'), box_alignment=(1, .5), boxcoords='offset points', bboxprops={ 'edgecolor': 'none', 'facecolor': 'none' }, arrowprops={ 'arrowstyle': '-', 'shrinkA': 0, 'shrinkB': 1 }, pad=0.1) ax.add_artist(ab) zorder = ab.zorder else: zorder = 0 if self.marker: if self.final_image is not None: markersize = max(self.final_image.size) else: markersize = 50 markersize = markersize * size d = DrawingArea(markersize, markersize) if self.marker_front: zorder_marker = zorder + 0.1 else: zorder_marker = zorder - 0.1 d.set_zorder(zorder_marker) d.set_alpha(0) if self.marker_front: d.add_artist( plt.Line2D([markersize / 2], [markersize / 2], marker=self.marker, markeredgecolor=self.col, markerfacecolor=(0, 0, 0, 0), markersize=markersize, markeredgewidth=self.markeredgewidth, transform=d.get_transform(), zorder=zorder_marker)) else: d.add_artist( plt.Line2D([markersize / 2], [markersize / 2], marker=self.marker, markeredgecolor=self.col, markerfacecolor=self.col, markersize=markersize, markeredgewidth=self.markeredgewidth, transform=d.get_transform(), zorder=zorder_marker)) ab_marker = AnnotationBbox(d, (0, y), xybox=(-offset, 0), xycoords=('axes fraction', 'data'), box_alignment=(1, 0.5), boxcoords='offset points', bboxprops={ 'edgecolor': 'none', 'facecolor': 'none' }, arrowprops={ 'arrowstyle': '-', 'shrinkA': 0, 'shrinkB': 1 }, pad=0.1) ab_marker.set_zorder(zorder_marker) ab_marker.set_alpha(0) ax.add_artist(ab_marker) if self.string is not None: ax.annotate(self.string, (0, y), xytext=(-offset, 0), xycoords=('axes fraction', 'data'), textcoords='offset points', horizontalalignment='right', verticalalignment='center', arrowprops={ 'arrowstyle': '-', 'shrinkA': 0, 'shrinkB': 1 }, zorder=zorder + 1, fontsize=self.fontsize, fontname=self.fontname, color=self.fontcolor)
def _plot_altitude(self, figure, site, tgt_data, tz): """ Plot into `figure` an altitude chart using target data from `info` with time plotted in timezone `tz` (a tzinfo instance). """ ## site = info.site ## tgt_data = info.target_data # Urk! This seems to be necessary even though we are plotting # python datetime objects with timezone attached and setting # date formatters with the timezone tz_str = tz.tzname(None) mpl.rcParams['timezone'] = tz_str # set major ticks to hours majorTick = mpl_dt.HourLocator(tz=tz) majorFmt = mpl_dt.DateFormatter('%Hh') # set minor ticks to 15 min intervals minorTick = mpl_dt.MinuteLocator(list(range(0,59,15)), tz=tz) figure.clf() ax1 = figure.add_subplot(111) figure.set_tight_layout(False) figure.subplots_adjust(left=0.05, right=0.65, bottom=0.12, top=0.95) #lstyle = 'o' lstyle = '-' lt_data = list(map(lambda info: info.ut.astimezone(tz), tgt_data[0].history)) # sanity check on dates in preferred timezone ## for dt in lt_data[:10]: ## print(dt.strftime("%Y-%m-%d %H:%M:%S")) min_interval = 12 # hour/5min mt = lt_data[0:-1:min_interval] targets = [] legend = [] # plot targets elevation vs. time for i, info in enumerate(tgt_data): alt_data = numpy.array(list(map(lambda info: info.alt_deg, info.history))) alt_min = numpy.argmin(alt_data) alt_data_dots = alt_data color = self.colors[i % len(self.colors)] lc = color + lstyle # ax1.plot_date(lt_data, alt_data, lc, linewidth=1.0, alpha=0.3, aa=True, tz=tz) ax1.plot_date(lt_data, alt_data_dots, lc, linewidth=2.0, aa=True, tz=tz) legend.extend(ax1.plot_date(lt_data, alt_data_dots, lc, linewidth=2.0,aa=True, tz=tz)) targets.append("{0} {1} {2}".format(info.target.name, info.target.ra, info.target.dec)) alt_interval = alt_data[0:-1:min_interval] moon_sep = numpy.array(map(lambda info: info.moon_sep, info.history)) moon_sep = moon_sep[0:-1:min_interval] # plot moon separations for x, y, v in zip(mt, alt_interval, moon_sep): if y < 0: continue ax1.text(x, y, '%.1f' %v, fontsize=7, ha='center', va='bottom') ax1.plot_date(x, y, 'ko', ms=3) #xs, ys = mpl.mlab.poly_between(lt_data, 2.02, alt_data) #ax1.fill(xs, ys, facecolor=self.colors[i], alpha=0.2) # plot object label targname = info.target.name ax1.text(mpl_dt.date2num(lt_data[alt_data.argmax()]), alt_data.max() + 4.0, targname, color=color, ha='center', va='center') # legend moon distance box1 = TextArea("Moon distance(deg) ", textprops=dict(color="k", size=7.5)) box2 = DrawingArea(48, 10, 0, 0) circle1 = Circle((5, 5), 2.3, fc="k", ec=None) box2.add_artist(circle1) box = HPacker(children=[box2, box1], align="center", pad=2.5, sep=-35) args = dict(alpha=0.5,) anchored_box = AnchoredOffsetbox(loc=3, child=box, pad=0., frameon=True, bbox_to_anchor=(0.009, 0.9630), #(0.25, -0.140) bbox_transform=ax1.transAxes, borderpad=0.) ax1.add_artist(anchored_box) #x = mpl_dt.date2num(lt_data[len(lt_data)/2]) # legend target list self.fig.legend(legend, sorted(targets), 'upper right', fontsize=9, framealpha=0.5, frameon=True, ncol=1, bbox_to_anchor=[0.3, 0.865, .7, 0.1]) ax1.set_ylim(0.0, 90.0) ax1.set_xlim(lt_data[0], lt_data[-1]) ax1.xaxis.set_major_locator(majorTick) ax1.xaxis.set_minor_locator(minorTick) ax1.xaxis.set_major_formatter(majorFmt) labels = ax1.get_xticklabels() ax1.grid(True, color='#999999') # label axes localdate = lt_data[0].astimezone(tz).strftime("%Y-%m-%d") title = 'Visibility for the night of %s' % (localdate) ax1.set_title(title) ax1.set_xlabel(tz.tzname(None)) ax1.set_ylabel('Altitude') # Plot moon trajectory and illumination moon_data = numpy.array(list(map(lambda info: info.moon_alt, tgt_data[0].history))) illum_time = lt_data[moon_data.argmax()] moon_illum = site.moon_phase(date=illum_time) moon_color = '#666666' moon_name = "Moon (%.2f %%)" % (moon_illum*100) ax1.plot_date(lt_data, moon_data, moon_color, linewidth=2.0, alpha=0.5, aa=True, tz=tz) ax1.text(mpl_dt.date2num(illum_time), moon_data.max() + 4.0, moon_name, color=moon_color, ha='center', va='center') # Plot airmass scale altitude_ticks = numpy.array([20, 30, 40, 50, 60, 70, 80, 90]) airmass_ticks = 1.0/numpy.cos(numpy.radians(90 - altitude_ticks)) airmass_ticks = list(map(lambda n: "%.3f" % n, airmass_ticks)) ax2 = ax1.twinx() #ax2.set_ylim(None, 0.98) #ax2.set_xlim(lt_data[0], lt_data[-1]) ax2.set_yticks(altitude_ticks) ax2.set_yticklabels(airmass_ticks) ax2.set_ylim(ax1.get_ylim()) ax2.set_ylabel('Airmass') ax2.set_xlabel('') ax2.yaxis.tick_right() ## mxs, mys = mpl.mlab.poly_between(lt_data, 0, moon_data) ## # ax2.fill(mxs, mys, facecolor='#666666', alpha=moon_illum) # plot moon label targname = "moon" ## ax1.text(mpl_dt.date2num(moon_data[moon_data.argmax()]), ## moon_data.max() + 0.08, targname.upper(), color=color, ## ha='center', va='center') # plot lower and upper safe limits for clear observing min_alt, max_alt = 30.0, 75.0 self._plot_limits(ax1, min_alt, max_alt) self._plot_twilight(ax1, site, tz) # plot current hour lo = datetime.now(tz) hi = lo + timedelta(0, 3600.0) if lt_data[0] < lo < lt_data[-1]: self._plot_current_time(ax1, lo, hi) canvas = self.fig.canvas if canvas is not None: canvas.draw()
def draw(self): """ Draw guide Returns ------- out : matplotlib.offsetbox.Offsetbox A drawing of this legend """ obverse = slice(0, None) reverse = slice(None, None, -1) nbars = len(self.bar) direction = self.direction colors = self.bar['color'].tolist() labels = self.key['label'].tolist() themeable = self.theme.figure._themeable _d = self._default # 1.45 makes the default colourbar wider than the # legend entry boxes. width = (self.barwidth or _d('legend_key_width') or 16) * 1.45 height = (self.barheight or _d('legend_key_height') or 16) * 1.45 height *= 5 length = height # When there is more than one guide, we keep # record of all of them using lists if 'legend_title' not in themeable: themeable['legend_title'] = [] if 'legend_text_colorbar' not in themeable: themeable['legend_text_colorbar'] = [] # .5 puts the ticks in the middle of the bars when # raster=False. So when raster=True the ticks are # in between interpolation points and the matching is # close though not exactly right. _from = self.bar['value'].min(), self.bar['value'].max() tick_locations = rescale(self.key['value'], (.5, nbars - .5), _from) * length / nbars if direction == 'horizontal': width, height = height, width length = width if self.reverse: colors = colors[::-1] labels = labels[::-1] tick_locations = length - tick_locations[::-1] # title # title_box = TextArea(self.title, textprops=dict(color='black')) themeable['legend_title'].append(title_box) # colorbar and ticks # da = DrawingArea(width, height, 0, 0) if self.raster: add_interpolated_colorbar(da, colors, direction) else: add_segmented_colorbar(da, colors, direction) if self.ticks: _locations = tick_locations if not self.draw_ulim: _locations = _locations[:-1] if not self.draw_llim: _locations = _locations[1:] add_ticks(da, _locations, direction) # labels # if self.label: labels_da, legend_text = create_labels(da, labels, tick_locations, direction) themeable['legend_text_colorbar'].extend(legend_text) else: labels_da = DrawingArea(0, 0) # colorbar + labels # if direction == 'vertical': packer, align = HPacker, 'bottom' align = 'center' else: packer, align = VPacker, 'right' align = 'center' slc = obverse if self.label_position == 'right' else reverse if self.label_position in ('right', 'bottom'): slc = obverse else: slc = reverse main_box = packer(children=[da, labels_da][slc], sep=self._label_margin, align=align, pad=0) # title + colorbar(with labels) # lookup = { 'right': (HPacker, reverse), 'left': (HPacker, obverse), 'bottom': (VPacker, reverse), 'top': (VPacker, obverse) } packer, slc = lookup[self.title_position] children = [title_box, main_box][slc] box = packer(children=children, sep=self._title_margin, align=self._title_align, pad=0) return box
""" ============== Anchored Box04 ============== """ from matplotlib.patches import Ellipse import matplotlib.pyplot as plt from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, DrawingArea, HPacker fig = plt.figure(1, figsize=(3, 3)) ax = plt.subplot(111) box1 = TextArea(" Test : ", textprops=dict(color="k")) box2 = DrawingArea(60, 20, 0, 0) el1 = Ellipse((10, 10), width=16, height=5, angle=30, fc="r") el2 = Ellipse((30, 10), width=16, height=5, angle=170, fc="g") el3 = Ellipse((50, 10), width=16, height=5, angle=230, fc="b") box2.add_artist(el1) box2.add_artist(el2) box2.add_artist(el3) box = HPacker(children=[box1, box2], align="center", pad=0, sep=5) anchored_box = AnchoredOffsetbox( loc=3, child=box, pad=0., frameon=True, bbox_to_anchor=(0., 1.02),
arrowprops=dict(arrowstyle="->")) ax.add_artist(ab) offsetbox = TextArea("Test", minimumdescent=False) ab = AnnotationBbox(offsetbox, xy, xybox=(1.02, xy[1]), xycoords='data', boxcoords=("axes fraction", "data"), box_alignment=(0., 0.5), arrowprops=dict(arrowstyle="->")) ax.add_artist(ab) from matplotlib.patches import Circle da = DrawingArea(20, 20, 0, 0) p = Circle((10, 10), 10) da.add_artist(p) xy = [0.3, 0.55] ab = AnnotationBbox(da, xy, xybox=(1.02, xy[1]), xycoords='data', boxcoords=("axes fraction", "data"), box_alignment=(0., 0.5), arrowprops=dict(arrowstyle="->")) #arrowprops=None) ax.add_artist(ab)
def summarizePerformance(self, test_data_set, learning_algo, *args, **kwargs): """ Plot of the low-dimensional representation of the environment built by the model """ all_possib_inp = [] #labels=[] for x_b in range(self._nx_block): #[1]:#range(self._nx_block): for y_b in range(self._height): for x_p in range(self._width - self._width_paddle + 1): state = self.get_observation( y_b, x_b * ((self._width - 1) // (self._nx_block - 1)), x_p) all_possib_inp.append(state) #labels.append(x_b) #arr=np.array(all_possib_inp) #arr=arr.reshape(arr.shape[0],-1) #np.savetxt('tsne_python/catcherH_X.txt',arr.reshape(arr.shape[0],-1)) #np.savetxt('tsne_python/cacherH_labels.txt',np.array(labels)) all_possib_inp = np.expand_dims(all_possib_inp, axis=1) all_possib_abs_states = learning_algo.encoder.predict(all_possib_inp) n = self._height - 1 historics = [] for i, observ in enumerate(test_data_set.observations()[0][0:n]): historics.append(np.expand_dims(observ, axis=0)) historics = np.array(historics) abs_states = learning_algo.encoder.predict(historics) actions = test_data_set.actions()[0:n] if self.inTerminalState() == False: self._mode_episode_count += 1 print("== Mean score per episode is {} over {} episodes ==".format( self._mode_score / (self._mode_episode_count + 0.0001), self._mode_episode_count)) import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import matplotlib.cm as cm m = cm.ScalarMappable(cmap=cm.jet) x = np.array(abs_states)[:, 0] y = np.array(abs_states)[:, 1] z = np.array(abs_states)[:, 2] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.set_xlabel(r'$X_1$') ax.set_ylabel(r'$X_2$') ax.set_zlabel(r'$X_3$') for j in range(3): # Plot the trajectory for i in range(30): #(n-1): ax.plot(x[j * 24 + i:j * 24 + i + 2], y[j * 24 + i:j * 24 + i + 2], z[j * 24 + i:j * 24 + i + 2], color=plt.cm.cool(255 * i / n), alpha=0.5) # Plot the estimated transitions for i in range(n - 1): predicted1 = learning_algo.transition.predict( [abs_states[i:i + 1], np.array([[1, 0]])]) predicted2 = learning_algo.transition.predict( [abs_states[i:i + 1], np.array([[0, 1]])]) ax.plot(np.concatenate([x[i:i + 1], predicted1[0, :1]]), np.concatenate([y[i:i + 1], predicted1[0, 1:2]]), np.concatenate([z[i:i + 1], predicted1[0, 2:3]]), color="0.75", alpha=0.75) ax.plot(np.concatenate([x[i:i + 1], predicted2[0, :1]]), np.concatenate([y[i:i + 1], predicted2[0, 1:2]]), np.concatenate([z[i:i + 1], predicted2[0, 2:3]]), color="0.25", alpha=0.75) # Plot the colorbar for the trajectory fig.subplots_adjust(right=0.7) ax1 = fig.add_axes([0.725, 0.15, 0.025, 0.7]) # Set the colormap and norm to correspond to the data for which the colorbar will be used. cmap = matplotlib.cm.cool norm = matplotlib.colors.Normalize(vmin=0, vmax=1) # ColorbarBase derives from ScalarMappable and puts a colorbar in a specified axes, so it has # everything needed for a standalone colorbar. There are many more kwargs, but the # following gives a basic continuous colorbar with ticks and labels. cb1 = matplotlib.colorbar.ColorbarBase(ax1, cmap=cmap, norm=norm, orientation='vertical') cb1.set_label('Beginning to end of trajectory') # Plot the dots at each time step depending on the action taken length_block = self._height * (self._width - self._width_paddle + 1) for i in range(self._nx_block): line3 = ax.scatter( all_possib_abs_states[i * length_block:(i + 1) * length_block, 0], all_possib_abs_states[i * length_block:(i + 1) * length_block, 1], all_possib_abs_states[i * length_block:(i + 1) * length_block, 2], s=10, marker='x', depthshade=True, edgecolors='k', alpha=0.3) line2 = ax.scatter(x, y, z, c=np.tile(np.expand_dims(1 - actions / 2., axis=1), (1, 3)) - 0.25, s=50, marker='o', edgecolors='k', alpha=0.75, depthshade=True) axes_lims = [ax.get_xlim(), ax.get_ylim(), ax.get_zlim()] zrange = axes_lims[2][1] - axes_lims[2][0] # Plot the legend for the dots from matplotlib.patches import Circle, Rectangle from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, DrawingArea, HPacker box1 = TextArea(" State representation (action 0, action 1): ", textprops=dict(color="k")) box2 = DrawingArea(60, 20, 0, 0) el1 = Circle((10, 10), 5, fc="0.75", edgecolor="k", alpha=0.75) el2 = Circle((30, 10), 5, fc="0.25", edgecolor="k", alpha=0.75) #el3 = Circle((50, 10), 5, fc="0", edgecolor="k") box2.add_artist(el1) box2.add_artist(el2) #box2.add_artist(el3) box = HPacker(children=[box1, box2], align="center", pad=0, sep=5) anchored_box = AnchoredOffsetbox( loc=3, child=box, pad=0., frameon=True, bbox_to_anchor=(0., 1.07), bbox_transform=ax.transAxes, borderpad=0., ) ax.add_artist(anchored_box) # Plot the legend for transition estimates box1b = TextArea(" Estimated transitions (action 0, action 1): ", textprops=dict(color="k")) box2b = DrawingArea(60, 20, 0, 0) el1b = Rectangle((5, 10), 15, 2, fc="0.75", alpha=0.75) el2b = Rectangle((25, 10), 15, 2, fc="0.25", alpha=0.75) box2b.add_artist(el1b) box2b.add_artist(el2b) boxb = HPacker(children=[box1b, box2b], align="center", pad=0, sep=5) anchored_box = AnchoredOffsetbox( loc=3, child=boxb, pad=0., frameon=True, bbox_to_anchor=(0., 0.98), bbox_transform=ax.transAxes, borderpad=0., ) ax.add_artist(anchored_box) ax.w_xaxis.set_pane_color((0.99, 0.99, 0.99, 0.99)) ax.w_yaxis.set_pane_color((0.99, 0.99, 0.99, 0.99)) ax.w_zaxis.set_pane_color((0.99, 0.99, 0.99, 0.99)) #plt.savefig('fig_base'+str(learning_algo.update_counter)+'.pdf') # Plot the Q_vals c = learning_algo.Q.predict( np.concatenate((np.expand_dims(x, axis=1), np.expand_dims( y, axis=1), np.expand_dims(z, axis=1)), axis=1)) m1 = ax.scatter(x, y, z + zrange / 20, c=c[:, 0], vmin=-1., vmax=1., cmap=plt.cm.RdYlGn) m2 = ax.scatter(x, y, z + 3 * zrange / 40, c=c[:, 1], vmin=-1., vmax=1., cmap=plt.cm.RdYlGn) #plt.colorbar(m3) ax2 = fig.add_axes([0.85, 0.15, 0.025, 0.7]) cmap = matplotlib.cm.RdYlGn norm = matplotlib.colors.Normalize(vmin=-1, vmax=1) # ColorbarBase derives from ScalarMappable and puts a colorbar # in a specified axes, so it has everything needed for a # standalone colorbar. There are many more kwargs, but the # following gives a basic continuous colorbar with ticks # and labels. cb1 = matplotlib.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm, orientation='vertical') cb1.set_label('Estimated expected return') # plt.show() for ii in range(-15, 345, 30): ax.view_init(elev=20., azim=ii) plt.savefig('fig_w_V_div5_forcelr_forcessdiv2' + str(learning_algo.update_counter) + '_' + str(ii) + '.pdf') # fig_visuV fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = np.array([i for i in range(5) for jk in range(25)]) / 4. * ( axes_lims[0][1] - axes_lims[0][0]) + axes_lims[0][0] y = np.array([j for i in range(5) for j in range(5) for k in range(5)]) / 4. * ( axes_lims[1][1] - axes_lims[1][0]) + axes_lims[1][0] z = np.array([k for i in range(5) for j in range(5) for k in range(5)]) / 4. * ( axes_lims[2][1] - axes_lims[2][0]) + axes_lims[2][0] c = learning_algo.Q.predict( np.concatenate((np.expand_dims(x, axis=1), np.expand_dims( y, axis=1), np.expand_dims(z, axis=1)), axis=1)) c = np.max(c, axis=1) m = ax.scatter(x, y, z, c=c, vmin=-1., vmax=1., cmap=plt.hot()) fig.subplots_adjust(right=0.8) ax2 = fig.add_axes([0.875, 0.15, 0.025, 0.7]) cmap = matplotlib.cm.hot norm = matplotlib.colors.Normalize(vmin=-1, vmax=1) # ColorbarBase derives from ScalarMappable and puts a colorbar # in a specified axes, so it has everything needed for a # standalone colorbar. There are many more kwargs, but the # following gives a basic continuous colorbar with ticks # and labels. cb1 = matplotlib.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm, orientation='vertical') cb1.set_label('Estimated expected return') #plt.show() #plt.savefig('fig_visuV'+str(learning_algo.update_counter)+'.pdf') # fig_visuR fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = np.array([i for i in range(5) for jk in range(25)]) / 4. * ( axes_lims[0][1] - axes_lims[0][0]) + axes_lims[0][0] y = np.array([j for i in range(5) for j in range(5) for k in range(5)]) / 4. * ( axes_lims[1][1] - axes_lims[1][0]) + axes_lims[1][0] z = np.array([k for i in range(5) for j in range(5) for k in range(5)]) / 4. * ( axes_lims[2][1] - axes_lims[2][0]) + axes_lims[2][0] coords = np.concatenate((np.expand_dims( x, axis=1), np.expand_dims(y, axis=1), np.expand_dims(z, axis=1)), axis=1) repeat_nactions_coord = np.repeat(coords, self.nActions(), axis=0) identity_matrix = np.diag(np.ones(self.nActions())) tile_identity_matrix = np.tile(identity_matrix, (5 * 5 * 5, 1)) c = learning_algo.R.predict( [repeat_nactions_coord, tile_identity_matrix]) c = np.max(np.reshape(c, (125, self.nActions())), axis=1) m = ax.scatter(x, y, z, c=c, vmin=-1., vmax=1., cmap=plt.hot()) fig.subplots_adjust(right=0.8) ax2 = fig.add_axes([0.875, 0.15, 0.025, 0.7]) cmap = matplotlib.cm.hot norm = matplotlib.colors.Normalize(vmin=-1, vmax=1) # ColorbarBase derives from ScalarMappable and puts a colorbar # in a specified axes, so it has everything needed for a # standalone colorbar. There are many more kwargs, but the # following gives a basic continuous colorbar with ticks # and labels. cb1 = matplotlib.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm, orientation='vertical') cb1.set_label('Estimated expected return') #plt.show() #plt.savefig('fig_visuR'+str(learning_algo.update_counter)+'.pdf') matplotlib.pyplot.close("all") # avoids memory leaks
def _init_legend_box(self, handles, labels, markerfirst=True): """ Initialize the legend_box. The legend_box is an instance of the OffsetBox, which is packed with legend handles and texts. Once packed, their location is calculated during the drawing time. """ fontsize = self._fontsize # legend_box is a HPacker, horizontally packed with # columns. Each column is a VPacker, vertically packed with # legend items. Each legend item is HPacker packed with # legend handleBox and labelBox. handleBox is an instance of # offsetbox.DrawingArea which contains legend handle. labelBox # is an instance of offsetbox.TextArea which contains legend # text. text_list = [] # the list of text instances handle_list = [] # the list of text instances label_prop = dict( verticalalignment='baseline', horizontalalignment='left', fontproperties=self.prop, ) labelboxes = [] handleboxes = [] # The approximate height and descent of text. These values are # only used for plotting the legend handle. descent = 0.35 * self._approx_text_height() * (self.handleheight - 0.7) # 0.35 and 0.7 are just heuristic numbers and may need to be improved. height = self._approx_text_height() * self.handleheight - descent # each handle needs to be drawn inside a box of (x, y, w, h) = # (0, -descent, width, height). And their coordinates should # be given in the display coordinates. # The transformation of each handle will be automatically set # to self.get_trasnform(). If the artist does not use its # default transform (e.g., Collections), you need to # manually set their transform to the self.get_transform(). legend_handler_map = self.get_legend_handler_map() for orig_handle, lab in zip(handles, labels): handler = self.get_legend_handler(legend_handler_map, orig_handle) if handler is None: warnings.warn( "Legend does not support {!r} instances.\nA proxy artist " "may be used instead.\nSee: " "http://matplotlib.org/users/legend_guide.html" "#using-proxy-artist".format(orig_handle)) # We don't have a handle for this artist, so we just defer # to None. handle_list.append(None) else: textbox = TextArea(lab, textprops=label_prop, multilinebaseline=True, minimumdescent=True) text_list.append(textbox._text) labelboxes.append(textbox) handlebox = DrawingArea(width=self.handlelength * fontsize, height=height, xdescent=0., ydescent=descent) handleboxes.append(handlebox) # Create the artist for the legend which represents the # original artist/handle. handle_list.append( handler.legend_artist(self, orig_handle, fontsize, handlebox)) if len(handleboxes) > 0: # We calculate number of rows in each column. The first # (num_largecol) columns will have (nrows+1) rows, and remaining # (num_smallcol) columns will have (nrows) rows. ncol = min(self._ncol, len(handleboxes)) nrows, num_largecol = divmod(len(handleboxes), ncol) num_smallcol = ncol - num_largecol # starting index of each column and number of rows in it. largecol = safezip( list(xrange(0, num_largecol * (nrows + 1), (nrows + 1))), [nrows + 1] * num_largecol) smallcol = safezip( list( xrange(num_largecol * (nrows + 1), len(handleboxes), nrows)), [nrows] * num_smallcol) else: largecol, smallcol = [], [] handle_label = safezip(handleboxes, labelboxes) columnbox = [] for i0, di in largecol + smallcol: # pack handleBox and labelBox into itemBox itemBoxes = [ HPacker(pad=0, sep=self.handletextpad * fontsize, children=[h, t] if markerfirst else [t, h], align="baseline") for h, t in handle_label[i0:i0 + di] ] # minimumdescent=False for the text of the last row of the column if markerfirst: itemBoxes[-1].get_children()[1].set_minimumdescent(False) else: itemBoxes[-1].get_children()[0].set_minimumdescent(False) # pack columnBox if markerfirst: alignment = "baseline" else: alignment = "right" columnbox.append( VPacker(pad=0, sep=self.labelspacing * fontsize, align=alignment, children=itemBoxes)) if self._mode == "expand": mode = "expand" else: mode = "fixed" sep = self.columnspacing * fontsize self._legend_handle_box = HPacker(pad=0, sep=sep, align="baseline", mode=mode, children=columnbox) self._legend_title_box = TextArea("") self._legend_box = VPacker( pad=self.borderpad * fontsize, sep=self.labelspacing * fontsize, align="center", children=[self._legend_title_box, self._legend_handle_box]) self._legend_box.set_figure(self.figure) self.texts = text_list self.legendHandles = handle_list
#gs = plt.GridSpec(100,100,bottom=0,left=0,right=1,top=1) #ax_off = fig.add_subplot(gs[0:100,0:100]) # #star=mlines.Line2D([],[],color='red',marker='*',mec='k',linestyle='None',markersize=12,label='AFT used in this study') #circle=mlines.Line2D([],[],color='red',marker='o',mec='k',linestyle='None',markersize=10,label='AFT Available') # #ax_off.legend(handles=[circle,star],fontsize=8,labelspacing=0.2,handletextpad=0.25,borderpad=0.1,loc=3,facecolor=(0.773,0.898,0.961),fancybox=True,edgecolor=(0,0,0,1)) # #ax_off.patch.set_alpha(0) #ax_off.patch.set_visible(False) #ax_off.axis('off') #%%====================================TEST=================================%%# boxc1 = TextArea(' AHeA available:\n AHeA used for the study:', textprops=dict(color='k', fontsize=9)) Boxc2 = DrawingArea(12.5, 20) Recc0 = Rectangle((5, 0), width=6, height=6, angle=45, fc='darkred', ec='darkred') Recc1 = Circle((5, 15), radius=3.5, fc='darkred', ec='darkred') Boxc2.add_artist(Recc0) Boxc2.add_artist(Recc1) boxc = HPacker(children=[boxc1, Boxc2], align="center", pad=3, sep=2.5) anchored_boxc = AnchoredOffsetbox(loc=3, child=boxc,