def generate_artist(self, *args, **kywds): # print 'entering fig_grp:: generate_artist' for name, child in self.get_children(): if child._suppress: child.del_artist(delall=True) else: child.generate_artist(*args, **kywds) ax = self.get_figaxes() c = self.get_container() if self.isempty(): a = Rectangle((0, 0), 1, 1, facecolor='none', fill=False, edgecolor='none', alpha=0) a.figobj = self a.figobj_hl = [] self._artists = [a] c.patches.append(a) if ax is not None and ax.get_3d(): import mpl_toolkits.mplot3d.art3d as art3d art3d.patch_2d_to_3d(a) figure = self.get_figpage()._artists[0] a.set_figure(figure) if self.get_figaxes() is not None: if len(self.get_figaxes()._artists) != 0: a.axes = self.get_figaxes()._artists[0]
def generate_artist(self, *args, **kywds): # print 'entering fig_grp:: generate_artist' for name, child in self.get_children(): if child._suppress: child.del_artist(delall=True) else: child.generate_artist(*args, **kywds) ax = self.get_figaxes() c = self.get_container() if self.isempty(): a = Rectangle((0,0), 1, 1, facecolor='none', fill=False, edgecolor='none', alpha=0) a.figobj=self a.figobj_hl=[] self._artists = [a] c.patches.append(a) if ax is not None and ax.get_3d(): import mpl_toolkits.mplot3d.art3d as art3d art3d.patch_2d_to_3d(a) figure = self.get_figpage()._artists[0] a.set_figure(figure) if self.get_figaxes() is not None: if len(self.get_figaxes()._artists) != 0: a.axes = self.get_figaxes()._artists[0]
def draw_bool_plume(plume, ax=None): """Note: tried to add ells to Pathcollection and then plot that, but kept having a ValueError Also tried using an EclipseCollection but the patchcollection_2d_to_3d function does not work on it # xy is actually the yz plane # val headers: [u'x_position', u'z_position', u'small_radius', u'y_position'] """ if plume.condition in 'controlControlCONTROL': print "No plume in ", plume.condition return ells = [Ellipse(xy=(val[3], val[1]), width=2 * val[2], height=2 * val[2] * 3, angle=0, # linestyle='dotted', facecolor='none', alpha=0.05, edgecolor='r') for i, val in plume.data.iterrows()] for i, val in plume.data['x_position'].iteritems(): ell = ells[i] ax.add_patch(ell) art3d.patch_2d_to_3d(ell, z=val, zdir="x") # art3d.patch_collection_2d_to_3d(ells) plt.show()
def draw(): try: fig = plt.figure() ax = fig.gca(projection='3d') # Define theta theta = np.linspace(0, 2 * np.pi, 50) # cone z = np.linspace(0, 10, 50) theta_cone, z_cone = np.meshgrid(theta, z) x_cone = (z_cone / 2) * np.cos(theta_cone) y_cone = (z_cone / 2) * np.sin(theta_cone) ax.plot_surface(x_cone, y_cone, z_cone) # Cylinder zc = np.linspace(0, 10, 50) theta_cylinder, z_cylinder = np.meshgrid(theta, zc) x_cylinder = 5 * np.cos(theta_cylinder) y_cylinder = 5 * np.sin(theta_cylinder) ax.plot_surface(x_cylinder, y_cylinder, z_cylinder, alpha=0.5) # Flat p = Circle((0, 0), 5, alpha=0.5) ax.add_patch(p) art3d.patch_2d_to_3d(p, z=0, zdir='z') ax.set_zlim(0, 10) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) plt.show() except Exception as err: print(err)
def test_patch_modification(): fig = plt.figure() ax = fig.add_subplot(projection="3d") circle = Circle((0, 0)) ax.add_patch(circle) art3d.patch_2d_to_3d(circle) circle.set_facecolor((1.0, 0.0, 0.0, 1)) assert mcolors.same_color(circle.get_facecolor(), (1, 0, 0, 1)) fig.canvas.draw() assert mcolors.same_color(circle.get_facecolor(), (1, 0, 0, 1))
def plot_response( p, response, bar_width=1, source_loc=None, fig=None, offset=(0.0, 0.0), color="#00ceaa", draw_extras=True, ax=None, label=None, set_zlim=True, ): if fig is None: fig = plt.gcf() if source_loc is None: source_loc = p.source.R dx, dy = offset detector_locs = np.array([i.R for i in p.detectors]) if ax is None: ax = fig.add_subplot(111, projection='3d') ax.bar3d(detector_locs[:, 0] + dx, detector_locs[:, 1] + dy, np.zeros_like(response), bar_width, bar_width, response, color=color, edgecolor=color) if draw_extras: ax.scatter([source_loc[0]], [source_loc[1]], marker='*', color="red", label="Source") ax.scatter(*detector_locs.T, label="Detector") patches = build_patches(p, alpha=0.3) for patch in patches: ax.add_patch(patch) art3d.patch_2d_to_3d(patch) set_bounds(p, ax=ax) if set_zlim: ax.set_zlim([0, 1.1 * max(response)]) return (fig, ax)
def mplArt_from_shape( shapes, in3d=False, z=0.0, zdir='z' ) : """ can be made an Arc with: matplotlib.patches.Arc(xy, width, height, angle=0.0, theta1=0.0, theta2=360.0, **kwargs) for circle width=height=R """ """ > dictionary with keys 'dashed' (for edge drawing) and 'solid' (for the geometry) > shape has type ( 'arc' or 'segment' ), and relevent details """ Art = [] for style in [ 'solid', 'dashed' ] : if not style in shapes : continue shape = shapes[style] if shape.type == 'arc' : piece = mpl.patches.Arc( shape.center, 2*shape.radius, 2*shape.radius, angle = 0.0, theta1 = 180.0 * shape.theta1 / np.pi, theta2 = 180.0 * shape.theta2 / np.pi, linestyle=style, color='b' ) if in3d : piece = art3d.patch_2d_to_3d( piece, z=z, zdir=zdir ) elif shape.type == 'segment' : piece = mpl.lines.Line2D( shape.xdata, shape.ydata, linestyle=style ) if in3d : piece = art3d.line_2d_to_3d( piece, zs=z, zdir=zdir ) Art.append(piece) return Art
def art_convert3d( art, z=0.0, zdir='z' ) : """ doesn't effing work """ if isinstance( art, mpl.lines.Line2D ) : new_art = art3d.line_2d_to_3d( art, zs=z, zdir=zdir ) elif isinstance( art, mpl.patches.Patch ) : new_art = art3d.patch_2d_to_3d( art, z=z, zdir=zdir ) else : raise Exception('not a valid type of art') return new_art
def mplArt_from_shape( shapes, in3d=False, z=0.0, zdir='z' ) : Art = [] for style in [ 'solid', 'dashed' ] : if not style in shapes : continue shape = shapes[style] if shape.type == 'arc' : piece = mpl.patches.Arc( shape.center, 2*shape.radius, 2*shape.radius, angle = 0.0, theta1 = 180.0 * shape.theta1 / np.pi, theta2 = 180.0 * shape.theta2 / np.pi, linestyle=style, color='b' ) if in3d : piece = art3d.patch_2d_to_3d( piece, z=z, zdir=zdir ) elif shape.type == 'segment' : piece = mpl.lines.Line2D( shape.xdata, shape.ydata, linestyle=style ) if in3d : piece = art3d.line_2d_to_3d( piece, zs=z, zdir=zdir ) Art.append(piece) return Art
def add_rhabdomeres(ax, x0, y0, z0, x1, y1, z1, mirror_lr=False, mirror_bf=False, scale=0.015, camerapos=None, **kwargs): ''' Add rhabdomeres R1-R7/8 patches to a 3D plot. ax : object Matplotlib Axes object to add attach the patches onto x0,y0,z0 : float Coordinates of the rhabdomeres center point (R6/R7) x1,y1,z1 : float Vector pointing the direction of R6R3 line mirror_lr, mirror_bf : True, False (or auto for bf) Wheter to mirror this rhabdomere or not (left/right, back/front) scale : float Scale of the rhabdemeres **kwargs : dict To matplotlib.patches.CirclePolygon Returns a list of matplotlib 3d patches ''' if camerapos and is_behind_sphere(*camerapos, (x0, y0, z0)): return None #v = np.asarray([x0,y0,z0]) #uv = v / np.linalg.norm(v) try: phi = math.asin(z0) except: phi = math.pi / 2 try: theta = math.atan(x0 / y0) except: theta = math.pi / 2 if y0 < 0: theta = theta + math.pi # Calculate rhabdomere rotation to match x1,y1,z1 by transforming # point [1,0,0] to the x0,y0,z0 point and calculating angle of # transformed ux and x1,y1,z1 ux = np.array([1, 0, 0]) ux = get_rotation_matrix('x', phi) @ ux ux = get_rotation_matrix('z', -theta) @ ux rot = np.arccos( np.inner(ux, [x1, y1, z1]) / (np.linalg.norm(ux) * np.linalg.norm([x1, y1, z1]))) if z1 < 0: rot = -rot patches = [] if mirror_bf == 'auto' and z0 > 0: mirror_bf = True elif mirror_bf is not True: mirror_bf = False for diameter, location in zip(RHABDOMERE_DIAMETERS, RHABDOMERE_LOCATIONS): patch = CirclePolygon((location[0] * scale, location[1] * scale), diameter / 2 * scale, **kwargs) patches.append(patch) ax.add_patch(patch) art3d.patch_2d_to_3d(patch) #if mirror_lr: # patch._segment3d = [get_rotation_matrix('y', math.pi) @ p for p in patch._segment3d] # Rotate according to the vector (x1,y1,z1) # First z rotation to set initial rotation patch._segment3d = [ get_rotation_matrix('z', RHABDOMERE_R3R6_ROTATION) @ p for p in patch._segment3d ] #if not mirror_lr and not mirror_bf: # pass if mirror_lr and not mirror_bf: patch._segment3d = [ get_rotation_matrix('x', math.pi) @ p for p in patch._segment3d ] if not mirror_lr and mirror_bf: patch._segment3d = [ get_rotation_matrix('x', math.pi) @ p for p in patch._segment3d ] #patch._segment3d = [get_rotation_matrix('z', math.pi) @ p for p in patch._segment3d] #if not mirror_lr and mirror_bf: # patch._segment3d = [get_rotation_matrix('x', math.pi) @ p for p in patch._segment3d] # patch._segment3d = [get_rotation_matrix('z', math.pi) @ p for p in patch._segment3d] #if mirror_lr and mirror_bf: # patch._segment3d = [get_rotation_matrix('z', math.pi) @ p for p in patch._segment3d] patch._segment3d = [ get_rotation_matrix('z', rot) @ p for p in patch._segment3d ] patch._segment3d = [ get_rotation_matrix('x', math.pi / 2) @ p for p in patch._segment3d ] patch._segment3d = [ get_rotation_matrix('x', phi) @ p for p in patch._segment3d ] patch._segment3d = [ get_rotation_matrix('z', -theta) @ p for p in patch._segment3d ] # Translate patch._segment3d = [(x + x0, y + y0, z + z0) for x, y, z in patch._segment3d] return patches
def _draw_rectangle(self, *args, z: float = 0, **kwargs): artist = super()._draw_rectangle(*args, **kwargs) art3d.patch_2d_to_3d(artist, z=z) return artist
ax.set_ylim([0, ARENA_SIZE]) ax.set_zlim([(-ARENA_SIZE * 2 / 3), 0]) ax.set_xlabel('X', fontsize=30) ax.set_ylabel('Y', fontsize=30) ax.set_zlabel('Z', fontsize=30) ax.xaxis.set_ticklabels([]) ax.yaxis.set_ticklabels([]) ax.zaxis.set_ticklabels([]) for spot_detail in zip(SPOT_POSITIONS, SPOT_COLOURS): spot = mpl.patches.Circle(spot_detail[0], SPOT_RADIUS, color=spot_detail[1], zorder=0) ax.add_patch(spot) art3d.patch_2d_to_3d(spot, z=0) # Read files arrow_magnitudes = np.zeros((16, 3)) arrow_positions = np.zeros((16, 3)) arrows = None # for now first_file_activity = '' first_file_datetime = '' first_file_filename = '' for file in files: # pylint: disable=invalid-sequence-index summary = mflog_utils.summarise_file(file) # The location of arena files is set to "x,y" where x and y are integers between 0 and 3 locx, locy = summary['location'].split(',', 1)