def addLetter(char, x, y, height, color): text = TextPath((0, 0), char, size=1, prop=fontprop) bbox = text.get_extents() (tx0, ty0), tw, th = bbox.min, bbox.width, bbox.height trans = Affine2D.identity() \ .translate(-tx0, -ty0) \ .scale(0.9/tw, height/th) \ .translate(x+0.5, y) t = trans.transform_path(text) pylab.gca().add_patch(PathPatch(t, fc=color, ec='none'))
def addletter(let, x, y, height, color, alpha=1): text = TextPath((0, 0), let, size=1, prop=fontprop) bbox = text.get_extents() tx0, ty0 = bbox.min tw, th = bbox.width, bbox.height trans = Affine2D.identity() \ .translate(-tx0,-ty0) \ .scale(0.9/tw,height/th) \ .translate(x,y) t = trans.transform_path(text) pylab.gca().add_patch(PathPatch(t, fc=color, ec='none', alpha=alpha))
def draw_2d_graph(wd): '''2次元グラフ表示処理 ''' ffig = plt.figure(figsize=(DIAMETER/0.9, DIAMETER/1.6), dpi=80) dAx = ffig.add_subplot(111) dAx.set_title("Trajectory of Turning Handle at 360 degrees") dAx.set_xlim(-(2*DIAMETER+2), 2*np.pi*10) dAx.set_ylim(-(2*DIAMETER+2), 2*DIAMETER+1) dAx.spines['right'].set_color('none') dAx.spines['top'].set_color('none') dAx.xaxis.set_ticks_position('bottom') dAx.spines['bottom'].set_position(('data', 0)) # plt.xticks([-40, -20, 0, 10*np.pi/2, 20*np.pi], # [r'$40$', r'$20$', r'$0$', r'$+\np.pi/2$', r'$+2\np.pi$']) dAx.yaxis.set_ticks_position('left') dAx.spines['left'].set_position(('data', 0)) dAx.set_xlabel("Turning Direction", horizontalalignment='right', x=1.0) dAx.set_ylabel("Traveling Direction", horizontalalignment='right', y=1.0) # グラフのグリッドを正方形にする dAx.set_aspect('equal') # 90度回転 t = CompositeGenericTransform(Affine2D.identity().rotate_deg(90), dAx.transData) # 回転しない(0°回転) # t = CompositeGenericTransform(Affine2D.identity().rotate_deg(0), # dAx.transData) dAx.plot(wd.xfG, wd.yfG, color="red", linestyle="-", transform=t) dAx.plot(wd.xfGC, wd.yfGC, color="red", linestyle="solid", linewidth=3, transform=t) dAx.plot(wd.xfGD, wd.yfGD, color="red", linestyle="solid", linewidth=3, transform=t) dAx.text(DIAMETER, DIAMETER, r'Chaster angle = {0} degree, Diameter = {1} ' .format(THETA, DIAMETER)) dAx.text(DIAMETER, DIAMETER - 2, r'Offset between center of the wheel and caster = {0} ' .format(OFFSET)) dAx.text(DIAMETER, DIAMETER - 4, r'Steering angle is {0} degree.' .format(STEERING_LIMIT)) dAx.text(DIAMETER, DIAMETER - 6, r'Thick red line shows a trajectory which touches a ground ' 'from -{0} degree to {1} degree in steering angle' .format(STEERING_LIMIT, STEERING_LIMIT), color='red') d = datetime.datetime.today() ffig.savefig("Trail_"+d.strftime("%B%d日%A")+"Caster="+str(THETA) + "_Wheel="+str(DIAMETER)+"_Offset="+str(OFFSET)+".png", dpi=100, transparent=False)
def plot_state_dependency_matrix( model, direct=False, knockout=[], axes=None): """ Creates a matrix showing state variable dependency distances. To show only direct (first-order) dependencies, set the optional argument ``direct`` to ``True``. Variables can be "knocked out" by adding them to the list in the ``knockout`` parameter. If x depends on y and y depends on z, knocking out y will prevent the method from findind x's dependency on z. Returns a matplotlib axes object. """ import matplotlib.pyplot as pl # Create dependency matrix m = create_state_dependency_matrix(model, direct, knockout) n = len(m) # Configure axes a = axes if axes is not None else pl.gca() a.set_aspect('equal') a.set_xlim(0, n + 2) a.set_ylim(0, n) # Create matrix plot from matplotlib.patches import Rectangle w = 1.0 / (max(max(m))) # color weight p1 = 0.1 # Padding p2 = 1.0 - 2 * p1 # Square size def c(v): # Colormap if v == 1: return (0, 0, 0) else: vv = v * w if (vv > 1.0): return (0, 1.0, 0) return (vv, vv, 1.0) for y, row in enumerate(m): y = n - y - 1 for x, v in enumerate(row): r = Rectangle((x + p1, y + p1), p2, p2, color=c(v)) a.add_patch(r) r.set_clip_box(a.bbox) # Add colorbar r = Rectangle((1 + n + p1, y + p1), p2, p2, color=c(1 + y)) a.add_patch(r) r.set_clip_box(a.bbox) a.text( 1.5 + n, y + 0.5, str(y + 1), horizontalalignment='center', verticalalignment='center') # Set tick labels names = [i.qname() for i in model.states()] a.set_xticks([i + 0.5 for i in xrange(0, n)]) a.set_xticklabels(names) from matplotlib.transforms import Affine2D r = Affine2D.identity() a.set_yticks([i + 0.5 for i in xrange(0, n)]) rnames = list(names) rnames.reverse() a.set_yticklabels(rnames) for tick in a.xaxis.iter_ticks(): tick[0].label2On = True tick[0].label1On = False tick[0].label2.set_rotation('vertical') # Add axes labels a.annotate( 'Affecting variable -->', xy=(0, -0.05), ha='left', va='center', xycoords='axes fraction', textcoords='offset points') # Adjust subplot margins return a
def build_cloud(wordweights, loose=False, seed=None, split_limit=2**-3, pad=1.10, visual_limit=2**-5, highest_weight=None ): """Convert a list of words and weights into a list of paths and weights. You should only use this function if you know what you're doing, or if you really don't want to cache the generated paths. Otherwise just use the WordCloud class. Args: wordweights: An iterator of the form [ (word, weight), (word, weight), ... ] such that the weights are in decreasing order. loose: If `true', words won't be broken up into rectangles after insertion. This results in a looser cloud, generated faster. seed: A random seed to use split_limit: When words are approximated by rectangles, the rectangles will have dimensions less than split_limit. Higher values result in a tighter cloud, at a cost of more CPU time. The largest word has height 1.0. pad: Expand a word's bounding box by a factor of `pad' before inserting it. This can actually result in a tighter cloud if you have many small words by leaving space between large words. visual_limit: Words with height smaller than visual_limit will be discarded. highest_weight: Experimental feature. If you provide an upper bound on the weights that will be seen you don't have to provide words and weights sorted. The resulting word cloud will be noticeably uglier. Generates: Tuples of the form (path, weight) such that: * No two paths intersect * Paths are fairly densely packed around the origin * All weights are normalized to fall in the interval [0, 1] """ if seed is not None: random.seed(seed) font_properties = font_manager.FontProperties( family="sans", weight="bold", stretch="condensed") xheight = TextPath((0,0), "x", prop=font_properties).get_extents().expanded(pad,pad).height # These are magic numbers. Most wordclouds will not exceed these bounds. # If they do, it will have to re-index all of the bounding boxes. index_bounds = (-16, -16, 16, 16) index = BboxQuadtree(index_bounds) if highest_weight is None: # Attempt to pull the first word and weight. If we fail, the wordweights # list is empty and we should just quit. # # All this nonsense is to ensure it accepts an iterator of words # correctly. iterwords = iter(wordweights) try: first_word, first_weight = iterwords.next() iterwords = chain([(first_word, first_weight)], iterwords) except StopIteration: return # We'll scale all of the weights down by this much. weight_scale = 1.0/first_weight else: weight_scale = 1.0/highest_weight iterwords = iter(wordweights) bboxes = list() bounds = transforms.Bbox(((-0.5, -0.5), (-0.5, -0.5))) for tword, tweight in iterwords: weight = tweight*weight_scale if weight < visual_limit: # You're not going to be able to see the word anyway. Quit # rendering words now. continue word_path = TextPath((0,0), tword, prop=font_properties) word_bbox = word_path.get_extents().expanded(pad, pad) # word_scale = weight/float(word_bbox.height) word_scale = weight/float(xheight) # When we build a TextPath at (0,0) it doesn't necessarily have # its corner at (0,0). So we have to translate to the origin, # scale down, then translate to center it. Feel free to simplify # this if you want. word_trans = Affine2D.identity().translate( -word_bbox.xmin, -word_bbox.ymin ).scale(word_scale).translate( -0.5*abs(word_bbox.width)*word_scale, -0.5*abs(word_bbox.height)*word_scale ) word_path = word_path.transformed(word_trans) word_bbox = word_path.get_extents().expanded(pad, pad) if weight > split_limit: # Big words we place carefully, trying to make the dimensions of # the cloud equal and center it around the origin. gaps = ( ("left", bounds.xmin), ("bottom", bounds.ymin), ("right", bounds.xmax), ("top", bounds.ymax) ) direction = min(gaps, key=lambda g: abs(g[1]))[0] else: # Small words we place randomly. direction = random.choice( [ "left", "bottom", "right", "top" ] ) # Randomly place the word along an edge... if direction in ( "top", "bottom" ): center = random_position(bounds.xmin, bounds.xmax) elif direction in ( "right", "left" ): center = random_position(bounds.ymin, bounds.ymax) # And push it toward an axis. if direction == "top": bbox = word_bbox.translated( center, index_bounds[3] ) xpos, ypos = push_bbox_down( bbox, bboxes, index ) elif direction == "right": bbox = word_bbox.translated( index_bounds[2], center ) xpos, ypos = push_bbox_left( bbox, bboxes, index ) elif direction == "bottom": bbox = word_bbox.translated( center, index_bounds[1] ) xpos, ypos = push_bbox_up( bbox, bboxes, index ) elif direction == "left": bbox = word_bbox.translated( index_bounds[0], center ) xpos, ypos = push_bbox_right( bbox, bboxes, index ) # Now alternate pushing the word toward different axes until either # it stops movign or we get sick of it. max_moves = 2 moves = 0 while moves < max_moves and (moves == 0 or prev_xpos != xpos or prev_ypos != ypos): moves += 1 prev_xpos = xpos prev_ypos = ypos if direction in ["top", "bottom", "vertical"]: if xpos > 0: bbox = word_bbox.translated( xpos, ypos ) xpos, ypos = push_bbox_left( bbox, bboxes, index ) elif xpos < 0: bbox = word_bbox.translated( xpos, ypos ) xpos, ypos = push_bbox_right( bbox, bboxes, index ) direction = "horizontal" elif direction in ["left", "right", "horizontal"]: if ypos > 0: bbox = word_bbox.translated( xpos, ypos ) xpos, ypos = push_bbox_down( bbox, bboxes, index ) elif ypos < 0: bbox = word_bbox.translated( xpos, ypos ) xpos, ypos = push_bbox_up( bbox, bboxes, index ) direction = "vertical" wordtrans = Affine2D.identity().translate( xpos, ypos ) transpath = word_path.transformed(wordtrans) bbox = transpath.get_extents() # Swallow the new word into the bounding box for the word cloud. bounds = matplotlib.transforms.Bbox.union( [ bounds, bbox ] ) # We need to check if we've expanded past the bounds of our quad tree. # If so we'll need to expand the bounds and then re-index. new_bounds = index_bounds while not BoxifyWord.bbox_covers( # FIXME: Why am I not just doing this with a couple of logarithms? matplotlib.transforms.Bbox(((new_bounds[0], new_bounds[1]), (new_bounds[2], new_bounds[3]))), bounds ): new_bounds = tuple( map( lambda x: 2*x, index_bounds ) ) if new_bounds != index_bounds: # We need to re-index. index_bounds = new_bounds index = BboxQuadtree(index_bounds) for i, b in enumerate(bboxes): index.add_bbox(i, b) # Approximate the new word by rectangles (unless it's too small) and # insert them into the index. if not loose and max(abs(bbox.width), abs(bbox.height)) > split_limit: for littlebox in BoxifyWord.splitword( bbox, transpath, limit=split_limit ): bboxes.append( littlebox ) index.add_bbox( len(bboxes)-1, littlebox ) else: bboxes.append( bbox ) index.add_bbox( len(bboxes)-1, bbox ) yield (transpath, weight)