def plot(self, vertexcolor=None, edgecolor=None, vertexsize=None, edgewidth=None, vertextext=None, edgetext=None): """Plot a 2D representation of the network. Parameters ---------- vertexcolor : dict, optional A dictionary mapping vertex identifiers to colors. edgecolor : dict, optional A dictionary mapping edge identifiers to colors. vertexsize : dict, optional A dictionary mapping vertex identifiers to sizes. edgewidth : dict, optional A dictionary mapping edge identifiers to widths. vertextext : dict, optional A dictionary mappping vertex identifiers to labels. edgetext : dict, optional A dictionary mappping edge identifiers to labels. Examples -------- .. plot:: :include-source: import compas from compas.datastructures import Network network = Network.from_obj(compas.get('lines.obj')) network.plot() """ from compas.plotters import NetworkPlotter plotter = NetworkPlotter(self) plotter.draw_vertices(facecolor=vertexcolor, radius=vertexsize, text=vertextext) plotter.draw_edges(color=edgecolor, width=edgewidth, text=edgetext) plotter.show()
from compas.datastructures import network_transformed from compas.geometry import Rotation from compas_assembly.datastructures import Assembly from compas_assembly.datastructures import assembly_interfaces from compas_rbe.equilibrium import compute_interface_forces_cvx assembly = Assembly.from_json(compas_assembly.get('wall.json')) keys = list(assembly.vertices_where({'is_support': True})) print(keys) if keys: assembly_interfaces(assembly) R = Rotation.from_axis_and_angle([1.0, 0, 0], -pi / 2) network = network_transformed(assembly, R) plotter = NetworkPlotter(network, figsize=(10, 7)) plotter.draw_vertices(facecolor={ key: '#ff0000' for key in network.vertices_where({'is_support': True}) }) plotter.draw_edges() plotter.show() compute_interface_forces_cvx(assembly, solver='CVXOPT', verbose=True) assembly.to_json(compas_assembly.get('assembly_result.json'))
# make a network network = Network.from_obj(FILE) # identify the fixed vertices network.set_vertices_attribute('is_fixed', True, keys=network.vertices_where( {'vertex_degree': 1})) # assign random prescribed force densities to the edges for uv in network.edges(): network.set_edge_attribute(uv, 'qpre', 1.0 * random.randint(1, 7)) # make a plotter for (dynamic) visualization plotter = NetworkPlotter(network, figsize=(10, 7)) # plot the starting configuration plotter.draw_vertices(facecolor={ key: '#000000' for key in network.vertices_where({'is_fixed': True}) }) plotter.draw_edges() plotter.update(pause=1.0) # run the DR network_dr(network, callback=callback) # plot the final configuration plotter.draw_vertices(facecolor={ key: '#000000'
plotter.show() # ============================================================================== # Main # ============================================================================== if __name__ == '__main__': import compas from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) plotter = NetworkPlotter(network, figsize=(10, 7)) plotter.defaults['vertex.fontsize'] = 8 network.delete_vertex(17) plotter.draw_vertices(text='key', radius=0.2) plotter.draw_edges() plotter.show() vertices = {44: [0.0, 0.0, 0.0], 38: [1.0, 0.0, 0.0], 2: [2.0, 0.0, 0.0]} edges = [(44, 38), (38, 2)] network = Network.from_vertices_and_edges(vertices, edges) print(network)
from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('fink.obj')) crossings = network_find_crossings(network) print(network_count_crossings(network)) print(crossings) print(len(crossings)) ecolor = {} for e1, e2 in crossings: ecolor[e1] = '#ff0000' ecolor[e2] = '#ff0000' plotter = NetworkPlotter(network, figsize=(10, 7)) plotter.draw_vertices() plotter.draw_edges(color=ecolor) plotter.show() # embedding = network.copy() # fix = (1, 12) # if network_embed_in_plane(embedding, fix=fix): # plotter = NetworkPlotter(embedding, figsize=(10, 7)) # plotter.draw_lines([{'start': network.vertex_coordinates(u, 'xy'), # 'end': network.vertex_coordinates(v, 'xy'),
# make network from sample file network = Network.from_obj(FILE) # specify start and end start = 21 end = 11 # compute the shortest path taking into account the edge weights path = shortest_path(network.adjacency, start, end) # convert the path to network edges edges = [(v, u) if not network.has_edge(u, v) else (u, v) for u, v in pairwise(path)] # make a plotter plotter = NetworkPlotter(network, figsize=(10, 7)) # set default font sizes plotter.defaults['vertex.fontsize'] = 6 plotter.defaults['edge.fontsize'] = 6 # draw the vertices plotter.draw_vertices( text='key', facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15) # draw the edges plotter.draw_edges(color={(u, v): '#ff0000' for u, v in edges},
import compas from compas.datastructures import Network from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) plotter = NetworkPlotter(network) plotter.draw_vertices(text='key', facecolor={key: '#ff0000' for key in network.leaves()}, radius=0.15) plotter.draw_edges() plotter.show()
import compas from compas.datastructures import Network from compas.topology import network_embed_in_plane from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('fink.obj')) embedding = network.copy() fix = (1, 12) if network_embed_in_plane(embedding, fix=fix): plotter = NetworkPlotter(embedding) plotter.draw_lines([{ 'start': network.vertex_coordinates(u, 'xy'), 'end': network.vertex_coordinates(v, 'xy'), 'color': '#cccccc' } for u, v in network.edges()]) plotter.draw_vertices(radius=0.3, text={key: key for key in embedding.vertices()}, facecolor={key: '#ff0000' for key in fix}) plotter.draw_edges() plotter.show()
from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) a = network.split_edge(0, 22) b = network.split_edge(2, 30) c = network.split_edge(17, 21) d = network.split_edge(28, 16) lines = [] for u, v in network.edges(): lines.append({ 'start': network.vertex_coordinates(u, 'xy'), 'end' : network.vertex_coordinates(v, 'xy'), 'arrow': 'end', 'width': 4.0, 'color': '#00ff00' }) plotter = NetworkPlotter(network) plotter.draw_vertices(radius=0.2, facecolor={key: '#ff0000' for key in (a, b, c, d)}, text={key: key for key in network.vertices()}) plotter.draw_edges(color={(u, v): '#cccccc' for u, v in network.edges()}) plotter.draw_lines(lines) plotter.show()
network = Network.from_obj(compas.get('grid_irregular.obj')) adjacency = {key: network.vertex_neighbours(key) for key in network.vertices()} start = 21 end = 2 path = shortest_path(adjacency, start, end) edges = [] for i in range(len(path) - 1): u = path[i] v = path[i + 1] if v not in network.edge[u]: u, v = v, u edges.append([u, v]) plotter = NetworkPlotter(network) plotter.draw_vertices( text={key: key for key in path}, facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15 ) plotter.draw_edges( color={(u, v): '#ff0000' for u, v in edges}, width={(u, v): 2.0 for u, v in edges} ) plotter.show()
if __name__ == '__main__': import compas from compas.datastructures import Network from compas.datastructures import network_is_planar from compas.datastructures import network_find_crossings from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) network.add_edge(6, 15) if not network_is_planar(network): crossings = network_find_crossings(network) else: crossings = [] print(crossings) plotter = NetworkPlotter(network) plotter.draw_vertices(radius=0.15, text={key: key for key in network.vertices()}) plotter.draw_edges( color={edge: '#ff0000' for edges in crossings for edge in edges}) plotter.show()
weight[(18, 1)] = 1000 weight[(1, 18)] = 1000 # specify start and end start = 21 end = 19 # compute the shortest path taking into account the edge weights path = dijkstra_path(network.adjacency, weight, start, end) # convert the path to network edges edges = [(v, u) if not network.has_edge(u, v) else (u, v) for u, v in pairwise(path)] # make a plotter plotter = NetworkPlotter(network, figsize=(10, 7)) # set default font sizes plotter.defaults['vertex.fontsize'] = 6 plotter.defaults['edge.fontsize'] = 6 # draw the vertices plotter.draw_vertices( text='key', facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15) # set the edge widths and colors color = {} width = {}
} start = 21 end = 11 path = shortest_path(adjacency, start, end) edges = [] for i in range(len(path) - 1): u = path[i] v = path[i + 1] if v not in network.edge[u]: u, v = v, u edges.append([u, v]) plotter = NetworkPlotter(network, figsize=(10, 8), fontsize=6) plotter.draw_vertices( text={key: key for key in network.vertices()}, facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15) plotter.draw_edges(color={(u, v): '#ff0000' for u, v in edges}, width={(u, v): 5.0 for u, v in edges}) plotter.show()
} start = 21 end = 22 path = shortest_path(adjacency, start, end) edges = [] for i in range(len(path) - 1): u = path[i] v = path[i + 1] if v not in network.edge[u]: u, v = v, u edges.append([u, v]) plotter = NetworkPlotter(network, figsize=(10, 8), fontsize=6) plotter.draw_vertices( text={key: key for key in network.vertices()}, facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15) plotter.draw_edges(color={(u, v): '#ff0000' for u, v in edges}, width={(u, v): 5.0 for u, v in edges}) plotter.show()
fpre = network.get_edges_attribute('fpre') lpre = network.get_edges_attribute('lpre') linit = network.get_edges_attribute('linit') E = network.get_edges_attribute('E') radius = network.get_edges_attribute('radius') lines = [] for u, v in network.edges(): lines.append({ 'start': network.vertex_coordinates(u, 'xy'), 'end': network.vertex_coordinates(v, 'xy'), 'color': '#cccccc', 'width': 1.0 }) plotter = NetworkPlotter(network) plotter.draw_lines(lines) xyz, q, f, l, r = dr_numpy(vertices, edges, fixed, loads, qpre, fpre, lpre, linit, E, radius) for key, attr in network.vertices(True): index = k_i[key] attr['x'] = xyz[index, 0] attr['y'] = xyz[index, 1] attr['z'] = xyz[index, 2] plotter.draw_vertices(facecolor={ key: '#ff0000' for key in network.vertices_where({'is_fixed': True}) })
'EIy': EI }) structure.update_default_edge_attributes({'E': 50, 'A': 1, 'l0': L / n}) structure.set_vertices_attributes(['B', 'is_fixed'], [[0, 0, 0], True], structure.leaves()) structure.attributes['beams'] = {'beam': {'nodes': list(range(n))}} lines = [] for u, v in structure.edges(): lines.append({ 'start': structure.vertex_coordinates(u, 'xy'), 'end': structure.vertex_coordinates(v, 'xy'), 'color': '#cccccc' }) plotter = NetworkPlotter(structure, figsize=(10, 7)) plotter.draw_vertices( radius=0.005, facecolor={ i: '#ff0000' for i in structure.vertices_where({'is_fixed': True}) }) plotter.draw_lines(lines) plotter.draw_edges() def callback(X, k_i): for key in structure.vertices(): x, y, z = X[k_i[key], :] structure.set_vertex_attributes(key, 'xyz', [x, y, z]) plotter.update_edges()
edges = [(key_index[u], key_index[v]) for u, v in network.edges()] C = connectivity_matrix(edges, rtype='list') C = matlab.double(C) # compute coordinate differences in Matlab # # using an engine function # uv = matlab.engine.mtimes(C, xyz) # using workspace data matlab.engine.workspace['C'] = C matlab.engine.workspace['xyz'] = xyz uv = matlab.engine.eval('C * xyz') # compute edge lengths in Python l = normrow(uv) l = l.flatten().tolist() # plot results as edge labels plotter = NetworkPlotter(network, figsize=(10, 7), fontsize=6) plotter.draw_vertices() plotter.draw_edges(text={(u, v): '%.1f' % l[index] for index, (u, v) in enumerate(network.edges())}) plotter.show()
from compas.datastructures import Network from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) u, v = network.get_any_edge() a = network.split_edge(u, v) lines = [] for u, v in network.edges(): lines.append({ 'start': network.vertex_coordinates(u, 'xy'), 'end' : network.vertex_coordinates(v, 'xy'), 'arrow': 'end', 'width': 4.0, 'color': '#00ff00' }) plotter = NetworkPlotter(network) plotter.draw_lines(lines) plotter.draw_vertices( radius=0.2, text={key: key for key in network.vertices()}, facecolor={key: '#ff0000' for key in (a,)} ) plotter.draw_edges() plotter.show()
cloads = Array2D(loads, 'double') cq = Array1D(q, 'double') cfixed = Array1D(fixed, 'int') cfree = Array1D(free, 'int') lib.fd.argtypes = [ ctypes.c_int, ctypes.c_int, ctypes.c_int, cvertices.ctype, cedges.ctype, cloads.ctype, cq.ctype, cfixed.ctype, cfree.ctype ] lib.fd(ctypes.c_int(len(vertices)), ctypes.c_int(len(edges)), ctypes.c_int(len(fixed)), cvertices.cdata, cedges.cdata, cloads.cdata, cq.cdata, cfixed.cdata, cfree.cdata) xyz = cvertices.pydata for key, attr in network.vertices(True): attr['x'] = float(xyz[key][0]) attr['y'] = float(xyz[key][1]) attr['z'] = float(xyz[key][2]) zmax = max(network.get_vertices_attribute('z')) plotter = NetworkPlotter(network, figsize=(10, 7)) plotter.draw_vertices(facecolor={ key: i_to_red(attr['z'] / zmax) for key, attr in network.vertices(True) }) plotter.draw_edges() plotter.show()
if __name__ == '__main__': import compas_ags from compas.plotters import NetworkPlotter form = FormDiagram.from_obj(compas_ags.get('paper/fink.obj')) lines = [] for u, v in form.edges(): lines.append({ 'start': form.vertex_coordinates(u), 'end': form.vertex_coordinates(v), 'color': '#cccccc', 'width': 0.5, }) form.identify_fixed() vcolor = {key: '#ff0000' for key in form.fixed()} vlabel = {key: key for key in form.vertices()} elabel = {(u, v): str(index) for index, (u, v) in enumerate(form.edges())} plotter = NetworkPlotter(form, figsize=(10.0, 7.0), fontsize=8) plotter.draw_lines(lines) plotter.draw_vertices(facecolor=vcolor, text=vlabel, radius=0.3) plotter.draw_edges(text=elabel) plotter.show()
network = Network.from_obj(compas.get('lines.obj')) # identify the fixed vertices # and assign random prescribed force densities to the edges for key, attr in network.vertices(True): attr['is_fixed'] = network.vertex_degree(key) == 1 for u, v, attr in network.edges(True): attr['qpre'] = 1.0 * random.randint(1, 7) # make a plotter for (dynamic) visualization # and define a callback function # for plotting the intermediate configurations plotter = NetworkPlotter(network, figsize=(10, 7), fontsize=6) def callback(k, xyz, crits, args): print(k) plotter.update_vertices() plotter.update_edges() plotter.update(pause=0.001) for key, attr in network.vertices(True): attr['x'] = xyz[key][0] attr['y'] = xyz[key][1] attr['z'] = xyz[key][2] # plot the starting configuration plotter.draw_vertices(facecolor={ key: '#000000'
# -------------------------------------------------------------------------- # -------------------------------------------------------------------------- # selections # -------------------------------------------------------------------------- # inherited from VertexAttributesMixin # inherited from EdgeAttributesMixin # ============================================================================== # Main # ============================================================================== if __name__ == '__main__': import compas from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) plotter = NetworkPlotter(network) network.delete_vertex(17) plotter.draw_vertices(text='key') plotter.draw_edges() plotter.show()
end = choice(leaves) # construc an adjacency dict # add weight to the edges corresponding to their length # compute the shortest path adjacency = {key: network.vertex_neighbors(key) for key in network.vertices()} weight = {(u, v): network.edge_length(u, v) for u, v in network.edges()} weight.update({(v, u): weight[(u, v)] for u, v in network.edges()}) path = dijkstra_path(adjacency, weight, start, end) # visualize the result plotter = NetworkPlotter(network, figsize=(10, 8), fontsize=6) edges = [] for u, v in pairwise(path): if v not in network.edge[u]: u, v = v, u edges.append([u, v]) plotter.draw_vertices( text={key: key for key in (start, end)}, facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15) plotter.draw_edges(color={(u, v): '#ff0000'
import compas from compas.datastructures import Network from compas.plotters import NetworkPlotter from compas.geometry import network_smooth_centroid network = Network.from_obj(compas.get('grid_irregular.obj')) fixed = [key for key in network.vertices() if network.vertex_degree(key) == 1] network_smooth_centroid(network, fixed=fixed) plotter = NetworkPlotter(network) plotter.draw_vertices(facecolor={key: '#ff0000' for key in fixed}) plotter.draw_edges() plotter.show()
edges = [[i, i + 1] for i in range(n - 1)] network = Network.from_vertices_and_edges(vertices=vertices, edges=edges) network.update_default_vertex_attributes({ 'is_fixed': False, 'P': [1, -2, 0], 'EIx': EI, 'EIy': EI }) network.update_default_edge_attributes({'E': 50, 'A': 1, 'l0': L / n}) network.set_vertices_attributes(pins, {'B': [0, 0, 0], 'is_fixed': True}) network.beams = {'beam': {'nodes': list(range(n))}} # Plotter plotter = NetworkPlotter(network, figsize=(10, 7)) lines = [] for u, v in network.edges(): lines.append({ 'start': network.vertex_coordinates(u, 'xy'), 'end': network.vertex_coordinates(v, 'xy'), 'color': '#cccccc', 'width': 1.0 }) plotter.draw_lines(lines) plotter.draw_vertices( radius=0.005, facecolor={
import compas from compas.datastructures import Network from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) plotter = NetworkPlotter(network) plotter.draw_vertices(text={key: key for key in network.vertices()}, radius=0.2) plotter.draw_edges() plotter.show()
import compas from compas.datastructures import Network from compas.plotters import NetworkPlotter from compas.topology import vertex_coloring network = Network.from_obj(compas.get('grid_irregular.obj')) key_color = vertex_coloring(network.adjacency) colors = ['#ff0000', '#00ff00', '#0000ff'] plotter = NetworkPlotter(network) plotter.draw_vertices(facecolor={key: colors[key_color[key]] for key in network.vertices()}) plotter.draw_edges() plotter.show()
network = Network.from_vertices_and_edges(vertices=vertices, edges=edges) network.update_default_vertex_attributes({ 'is_fixed': False, 'P': [1, -2, 0], 'EIx': EI, 'EIy': EI }) network.update_default_edge_attributes({'E': 50, 'A': 1, 'l0': L / n}) network.set_vertices_attributes(['B', 'is_fixed'], [[0, 0, 0], True], keys=pins) network.attributes['beams'] = {'beam': {'nodes': list(range(n))}} # Plotter plotter = NetworkPlotter(network, figsize=(10, 7)) # Initial configuration lines = [] for u, v in network.edges(): lines.append({ 'start': network.vertex_coordinates(u, 'xy'), 'end': network.vertex_coordinates(v, 'xy'), 'color': '#cccccc', 'width': 1.0 }) plotter.draw_lines(lines) plotter.draw_vertices(radius=0.005, facecolor={key: '#ff0000' for key in pins}) plotter.draw_edges()
# ============================================================================== # Main # ============================================================================== if __name__ == "__main__": import compas from compas.datastructures import Network from compas.plotters import NetworkPlotter network = Network.from_obj(compas.get('grid_irregular.obj')) plotter = NetworkPlotter(network, figsize=(10, 8)) plotter.draw_vertices(radius=0.1, picker=10) plotter.draw_edges() default = [ plotter.defaults['vertex.facecolor'] for key in network.vertices() ] highlight = '#ff0000' def on_pick(event): index = event.ind[0] colors = default[:] colors[index] = highlight
width={uv: 5.0 for uv in edges}) plotter.update() # callback for the pick event def onpick(event): index = event.ind[0] shortest_path_to(index) # path to the sample file DATA = os.path.join(os.path.dirname(__file__), '..', 'data') FILE = os.path.join(DATA, 'grid_irregular.obj') # load a network from an OBJ file network = Network.from_obj(FILE) # define the starting point start = 21 # create plotter # draw the original configuration # register the pick callback # show the viewer plotter = NetworkPlotter(network, figsize=(10, 7)) plotter.draw_vertices(facecolor={start: '#ff0000'}, radius=0.15, picker=10) plotter.draw_edges() plotter.register_listener(onpick) plotter.show()