def export_graphe():
    global counter
    filename = 'pp-{cc}.tex'.format(cc=counter)
    visual_style['vertex_color'] = [color_dict[g] for g in g1.vs["vu"]]
    visual_style['node_label'] = g1.vs["date"]
    plot(g1, filename, **visual_style)
    counter = counter + 1
예제 #2
0
def test_layout(net):

    layout_style = {}
    layout_style['layout'] = 'fr'
    layout_style['seed'] = 1
    _layout = layout(net, **layout_style)

    plot(net, layout=_layout)
예제 #3
0
파일: base.py 프로젝트: Dih5/comod
    def plot_tikz(self, filename=None, **kwargs):
        """
        Get a plot of a graph representing the model using tikz.

        Args:
            filename: How to save the generated tikz. Posible patterns follow:
                      - None: If a jupyter environment is detected, returns a Pillow image if libraries are available.
                              Otherwise, open a pdf in an external editor.
                      - "*.tex": Generate a TeX file with the code.
                      - "*.pdf": Generate a rendered pdf file.
            **kwargs: Additional arguments to pass to network2tikz.plot. Cf. https://pypi.org/project/network2tikz/.

        Returns:
            PIL.Image.Image or None: An image with a preview of the graph if filename is None and running as a notebook.

        """
        assert network2tikz is not None, "network2tikz not available"
        nodes = [_escape_state(s) for s in self.states]
        edges = [(_escape_state(r[0]), _escape_state(r[1]))
                 for r in self.rules]

        style = {}
        style['node_label'] = [
            "$%s$" % _latex_normalize(s) for s in self.states
        ]
        style['edge_label'] = [
            "$%s$" % self._coef_to_latex(r[2]) for r in self.rules
        ]
        style['node_color'] = ["blue"] * len(self.states)
        style['edge_directed'] = True

        if any([self.nihil_state in x[:2] for x in self.rules]):
            # prepend nihil_state
            nodes = [self.nihil_state] + nodes
            style['node_color'] = ["red"] + style['node_color']
            style['node_label'] = [""] + style['node_label']

        style = {**style, **kwargs}

        # network2tikz shows a pdf in an external editor if filename is None. Change if running in a notebook
        if filename is None and _is_notebook():
            if pdf2image is None:
                warnings.warn(
                    "Image will be shown externally since pdf2image is not available"
                )
                # Default behaviour below
            else:
                with tempfile.NamedTemporaryFile(suffix=".pdf") as f:
                    network2tikz.plot((nodes, edges), f.name, **style)
                    return pdf2image(f.name)[0]

        # In any case
        return network2tikz.plot((nodes, edges), filename, **style)
예제 #4
0
def visite(i,depth):
    global counter
    i["vu"] = True
    # à chaque fois qu'on visite un sommet, on exporte le graphe
    filename = 'pp-{cc}.tex'.format(cc=counter)
    visual_style['vertex_color'] = [color_dict[g] for g in g1.vs["vu"]]
    plot(g1, filename,**visual_style)
    counter = counter+1
    for j in g1.successors(i):
        if g1.vs[j]["vu"] == False:
            g1.vs[j]["parent"] = i["name"]
            visite(g1.vs[j],depth+1)
예제 #5
0
def test_plot(net, _layout, color_dict):

    # plot(net) # plot_01.png

    # plot(net,layout=layout) # plot_02.png

    # plot(net, layout=layout, canvas=(8,8), margin=1) # plot_03.png

    visual_style = {}
    visual_style['layout'] = _layout
    visual_style['node_size'] = .5
    visual_style['node_color'] = {
        n: color_dict[a['gender']]
        for n, a in net.nodes.items()
    }
    visual_style['node_opacity'] = .7
    visual_style['node_label'] = {n: a['name'] for n, a in net.nodes.items()}
    visual_style['node_label_position'] = 'below'
    visual_style['edge_width'] = {
        e: 1 + 2 * int(a['is_formal'])
        for e, a in net.edges.items()
    }
    visual_style['edge_curved'] = 0.1
    visual_style['canvas'] = (8, 8)
    visual_style['margin'] = 1

    plot(net, 'network.tex', **visual_style)

    plot(net, 'network.csv', **visual_style)

    plot(net, 'network.pdf', **visual_style)

    plot(net, **visual_style)
예제 #6
0
def test_plot_all_options(net, _layout, color_dict, shape_dict, style_dict):

    visual_style = {}
    # node styles
    # -----------
    visual_style['node_size'] = 5
    visual_style['node_color'] = [color_dict[g] for g in net.nodes('gender')]
    visual_style['node_opacity'] = .7
    visual_style['node_label'] = net.nodes['name']
    visual_style['node_label_position'] = 'below'
    visual_style['node_label_distance'] = 15
    visual_style['node_label_color'] = 'gray'
    visual_style['node_label_size'] = 3
    visual_style['node_shape'] = [shape_dict[g] for g in net.nodes('gender')]
    visual_style['node_style'] = [style_dict[g] for g in net.nodes('gender')]
    visual_style['node_label_off'] = {'e': True}
    visual_style['node_math_mode'] = [True]
    visual_style['node_label_as_id'] = {'f': True}
    visual_style['node_pseudo'] = {'d': True}

    # edge styles
    # -----------
    visual_style['edge_width'] = [.3 + .3 *
                                  int(f) for f in net.edges('is_formal')]
    visual_style['edge_color'] = 'black'
    visual_style['edge_opacity'] = .8
    visual_style['edge_curved'] = 0.1
    visual_style['edge_label'] = [e for e in net.edges]
    visual_style['edge_label_position'] = 'above'
    visual_style['edge_label_distance'] = .6
    visual_style['edge_label_color'] = 'gray'
    visual_style['edge_label_size'] = {'ac': 5}
    visual_style['edge_style'] = 'dashed'
    visual_style['edge_arrow_size'] = .2
    visual_style['edge_arrow_width'] = .2

    visual_style['edge_loop_size'] = 15
    visual_style['edge_loop_position'] = 90
    visual_style['edge_loop_shape'] = 45
    visual_style['edge_directed'] = [True, True, False, True, True, False, True,
                                     True, True, True]
    visual_style['edge_label'][1] = '\\frac{\\alpha}{\\beta}'
    visual_style['edge_math_mode'] = {'ac': True}
    visual_style['edge_not_in_bg'] = {'fa': True}

    # general options
    # ---------------
    visual_style['unit'] = 'mm'
    visual_style['layout'] = _layout
    visual_style["margin"] = {'top': 5, 'bottom': 8, 'left': 5, 'right': 5}
    visual_style["canvas"] = (100, 60)
    visual_style['keep_aspect_ratio'] = False

    plot(net, 'network.tex', **visual_style)

    plot(net, 'network.csv', **visual_style)

    plot(net, 'network.pdf', **visual_style)

    plot(net, **visual_style)
예제 #7
0
def test_plot(net, _layout, color_dict):

    # plot(net) # plot_01.png

    # plot(net,layout=layout) # plot_02.png

    # plot(net, layout=layout, canvas=(8,8), margin=1) # plot_03.png

    visual_style = {}
    visual_style['layout'] = _layout
    visual_style['vertex_size'] = .5
    visual_style['vertex_color'] = [color_dict[g] for g in net.vs['gender']]
    visual_style['vertex_opacity'] = .7
    visual_style['vertex_label'] = net.vs['name']
    visual_style['vertex_label_position'] = 'below'
    visual_style['edge_width'] = [1 + 2 * int(f) for f in net.es['is_formal']]
    visual_style['edge_curved'] = 0.1
    visual_style['canvas'] = (8, 8)
    visual_style['margin'] = 1

    plot(net, 'network.tex', **visual_style)

    plot(net, 'network.csv', **visual_style)

    plot(net, 'network.pdf', **visual_style)

    plot(net, **visual_style)
예제 #8
0
def test_plot(net, _layout, color_dict):

    # plot(net) # plot_01.png

    # plot(net,layout=layout) # plot_02.png

    # plot(net, layout=layout, canvas=(8,8), margin=1) # plot_03.png

    visual_style = {}
    visual_style['layout'] = _layout
    visual_style['vertex_size'] = .5
    visual_style['vertex_color'] = {n: color_dict[g]
                                    for n, g in nx.get_node_attributes(net, 'gender').items()}
    visual_style['vertex_opacity'] = .7
    visual_style['vertex_label'] = nx.get_node_attributes(net, 'name')
    visual_style['vertex_label_position'] = 'below'
    visual_style['edge_width'] = {
        e: 1 + 2 * int(f) for e, f in nx.get_edge_attributes(net, 'is_formal').items()}
    visual_style['edge_curved'] = 0.1
    visual_style['canvas'] = (8, 8)
    visual_style['margin'] = 1

    plot(net, 'network.tex', **visual_style)

    plot(net, 'network.csv', **visual_style)

    plot(net, 'network.pdf', **visual_style)

    plot(net, **visual_style)
예제 #9
0
def visite(i, col):
    global counter
    i["vu"] = True
    i["col"] = col
    # à chaque fois qu'on visite un sommet, on exporte le graphe
    filename = 'biparti-{cc}.pdf'.format(cc=counter)
    visual_style['vertex_color'] = [color_dict[g] for g in g1.vs["col"]]
    plot(g1, filename, **visual_style)
    counter = counter + 1
    for j in g1.successors(i):
        if g1.vs[j]["vu"] == False:
            visite(g1.vs[j], 1 - col)
        else:
            if g1.vs[j]["col"] == col:
                print('pas biparti')
                exit()
예제 #10
0
    def draw(self, save='temp.pdf', seed: int = None) -> None:
        """
        draw the graph

        :param save: save the save path (`reference <https://stackoverflow.com/a/20382152>`_)
        :type save: str
        :param seed: the node location random seed
        :type seed: int

        if you haven't installed network2tikz,
        you need install it by

        .. code-block:: bash

            pip install -U network2tikz

        """
        from network2tikz import plot
        nodes = list(self.states)
        node_colors = [
            'green!20' if node not in self.final_states else 'blue!20'
            for node in self.states
        ]
        node_colors[nodes.index(self.start_state)] = "red!20"
        edges = []
        edge_labels = []
        for from_state, to_states in self.transitions.items():
            for to_state, symbols in to_states.items():
                edges.append((from_state, to_state))
                labels = []
                for symbol in symbols:
                    labels.append(symbol)
                edge_labels.append("| ".join(labels))

        plot(
            (nodes, edges),
            save,
            #  layout="spring_layout",
            seed=seed,
            canvas=(10, 10),
            node_label_as_id=True,
            node_color=node_colors,
            edge_label=edge_labels,
            edge_math_mode=True,
            edge_directed=True,
            edge_curved=0.2,
            edge_label_position='left')
예제 #11
0
def draw_graph(i, g, k, fname, node_size=500):
    pos = nx.spring_layout(g,
                           k=k * 1 / np.sqrt(len(g.nodes())),
                           iterations=100)
    res = color_tree(g, 0, "tugreen", "tuorange")
    plt.figure(3, figsize=(17, 13))
    nx.draw_networkx(g, node_size=node_size, pos=pos)
    plt.savefig(fname + "_" + str(i))
    plt.close()
    visual_style = {}
    visual_style['vertex_size'] = .5
    visual_style['vertex_opacity'] = .7
    visual_style['layout'] = pos
    visual_style['canvas'] = (17, 15)
    visual_style['margin'] = 1
    visual_style['vertex_color'] = res
    visual_style['edge_width'] = 0.1
    visual_style['edge_color'] = 'black'
    plot(g, fname + "_" + str(i) + '.tex', **visual_style)
예제 #12
0
def test_fruchterman_reingold(net):
    net.summary()
    A = net.adjacency_matrix().todense()
    print(A.shape)
    #L = Layout(net)
    # layout = L._fruchterman_reingold(A)

    # print(layout)
    _layout = {
        'a': (0, 0),
        'b': (1, 1),
        'c': (2, 2),
        'd': (3, 3),
        'e': (4, 4),
        'f': (5, 5),
        'g': (6, 6)
    }

    layout_style = {}
    layout_style['layout'] = 'fr'
    layout_style['layout_seed'] = 1
    layout_style['layout_weight'] = 'force'
    _layout = layout(net, **layout_style)

    visual_style = {}
    visual_style['node_label_as_id'] = True
    visual_style['layout'] = _layout
    visual_style['canvas'] = (10, 10)
    visual_style['margin'] = 1
    plot(net, **visual_style)

    visual_style = {}
    visual_style['node_label_as_id'] = True
    visual_style['canvas'] = (10, 10)
    visual_style['margin'] = 1
    visual_style['layout'] = 'fr'  # _layout
    visual_style['layout_seed'] = 1
    visual_style['layout_weight'] = 'force'
    plot(net, **visual_style)
예제 #13
0
def test_plot(net, layout, color_dict):

    name = ['Alice', 'Bob', 'Claire', 'Dennis', 'Esther', 'Frank', 'George']
    age = [25, 31, 18, 47, 22, 23, 50]
    gender = ['f', 'm', 'f', 'm', 'f', 'm', 'm']
    is_formal = [
        False, False, True, True, True, False, True, False, False, False
    ]

    # plot(net) # plot_01.png

    # plot(net,layout=layout) # plot_02.png

    # plot(net, layout=layout, canvas=(8,8), margin=1) # plot_03.png

    visual_style = {}
    visual_style['layout'] = layout
    visual_style['node_size'] = .5
    visual_style['node_color'] = [color_dict[g] for g in gender]
    visual_style['node_opacity'] = .7
    visual_style['node_label'] = name
    visual_style['node_label_position'] = 'below'
    visual_style['edge_directed'] = True
    visual_style['edge_width'] = [1 + 2 * int(f) for f in is_formal]
    visual_style['edge_curved'] = 0.1
    visual_style['edge_color'] = [(230, 12, 102), (26, 213, 56)]
    visual_style['canvas'] = (8, 8)
    visual_style['margin'] = 1

    plot(net, 'network.tex', **visual_style)

    plot(net, 'network.csv', **visual_style)

    plot(net, 'network.pdf', **visual_style)

    plot(net, **visual_style)
예제 #14
0
def main():
    # Network
    # -------
    nodes = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    edges = [('a', 'b'), ('a', 'c'), ('b', 'c'), ('b', 'd'), ('d', 'e'),
             ('d', 'f'), ('d', 'g'), ('e', 'f'), ('f', 'g')]
    net = (nodes, edges)

    # Network attributes
    # ------------------
    layout = {
        'a': (0, 0),
        'b': (1, 1),
        'c': (0, 2),
        'd': (2, 1),
        'e': (3, 2),
        'f': (4, 1),
        'g': (3, 0)
    }

    # Network transition matrix
    # -------------------------
    T = np.array([[0, 1 / 2, 1 / 2, 0, 0, 0, 0],
                  [1 / 3, 0, 1 / 3, 1 / 3, 0, 0, 0],
                  [1 / 2, 1 / 2, 0, 0, 0, 0, 0],
                  [0, 1 / 4, 0, 0, 1 / 4, 1 / 4, 1 / 4],
                  [0, 0, 0, 1 / 2, 0, 1 / 2, 0],
                  [0, 0, 0, 1 / 3, 1 / 3, 0, 1 / 3],
                  [0, 0, 0, 1 / 2, 0, 1 / 2, 0]])

    # Starting vector
    x = np.array([1, 0, 0, 0, 0, 0, 0])

    # Visual style dict
    # -----------------
    visual_style = {}

    # node styles
    # -----------
    visual_style['node_size'] = .8
    visual_style['node_color'] = 'red'

    # edge styles
    # -----------
    visual_style['edge_width'] = 2
    visual_style['edge_curved'] = 0.1

    # general options
    # ---------------
    visual_style['layout'] = layout
    visual_style["canvas"] = (10, 7)

    # create images
    # -------------
    for step in range(10):
        # create file name for step n
        filename = '{num:02d}_network.pdf'.format(num=step)

        # get distribution for step n
        values = np.linalg.matrix_power(T, step).transpose().dot(x)

        # change node label
        visual_style['node_label'] = [str(n) for n in np.round(values, 3)]

        # change node oppacity
        visual_style['node_opacity'] = list(values)
        # Create a latex file

        plot(net, filename, **visual_style)
예제 #15
0
def export_graphe(graphe):
    global gn
    filename = 'kosaraju-{cc}.tex'.format(cc=gn)
    visual_style['vertex_color'] = [color_dict[g] for g in graphe.vs["vu"]]
    plot(graphe, filename, **visual_style)
    gn = gn + 1
예제 #16
0
    for i in v.successors():
        if i["vu"] == False:
            visite_k(ginv, i, ncb)


# initialisation
counter = 1
for s in g1.vs:
    s["vu"] = False
    s["nom"] = str(counter)
    s["date"] = ""
    counter = counter + 1
visual_style['node_label'] = g1.vs["nom"]
ordre_suffixe = []
# sauvegarde du graphe originel
plot(g1, 'kosaraju-0.tex', **visual_style)
counter = 1
for i in g1.vs:
    if i["vu"] == False:
        visite(g1, i)

print(ordre_suffixe)
plot(g1, 'kosaraju-p1.tex', **visual_style)

#inversion du graphe
ginv = ig.Graph(24, [], True)
n = 1
for v in ginv.vs:
    v["nom"] = str(n)
    v["vu"] = False
    v["date"] = ""
예제 #17
0
def export_g6_to_tikz(g6code, folder, count):
    graph = nx.from_graph6_bytes(g6code.encode('utf-8'))
    tkz.plot(graph, f"{folder}\Graph_{count}.tex", layout='fr', node_size=0.4)
예제 #18
0
def main():
    # Network and attributes
    # ----------------------
    net = nx.DiGraph()
    net.add_node('a', name='Alice', age=25, gender='f')
    net.add_node('b', name='Bob', age=31, gender='m')
    net.add_node('c', name='Claire', age=18, gender='f')
    net.add_node('d', name='Dennis', age=47, gender='m')
    net.add_node('e', name='Esther', age=22, gender='f')
    net.add_node('f', name='Frank', age=23, gender='m')
    net.add_node('g', name='George', age=50, gender='m')

    net.add_edge('a', 'b', is_formal=False)
    net.add_edge('a', 'c', is_formal=False)
    net.add_edge('c', 'd', is_formal=True)
    net.add_edge('d', 'e', is_formal=True)
    net.add_edge('e', 'c', is_formal=True)
    net.add_edge('c', 'f', is_formal=False)
    net.add_edge('f', 'a', is_formal=True)
    net.add_edge('f', 'g', is_formal=False)
    net.add_edge('g', 'g', is_formal=False)
    net.add_edge('g', 'd', is_formal=False)

    # Network dicts
    # -------------
    color_dict = {"m": "blue", "f": "red"}
    shape_dict = {"m": "circle", "f": "rectangle"}
    style_dict = {"m": "{shading=ball}", "f": None}
    layout = {
        'a': (4.3191, -3.5352),
        'b': (0.5292, -0.5292),
        'c': (8.6559, -3.8008),
        'd': (12.4117, -7.5239),
        'e': (12.7, -1.7069),
        'f': (6.0022, -9.0323),
        'g': (9.7608, -12.7)
    }

    # Visual style dict
    # -----------------
    visual_style = {}

    # node styles
    # -----------
    visual_style['vertex_size'] = 5
    visual_style['vertex_color'] = {
        n: color_dict[g]
        for n, g in nx.get_node_attributes(net, 'gender').items()
    }
    visual_style['vertex_opacity'] = .7
    visual_style['vertex_label'] = nx.get_node_attributes(net, 'name')
    visual_style['vertex_label_position'] = 'below'
    visual_style['vertex_label_distance'] = 15
    visual_style['vertex_label_color'] = 'gray'
    visual_style['vertex_label_size'] = 3
    visual_style['vertex_shape'] = {
        n: shape_dict[g]
        for n, g in nx.get_node_attributes(net, 'gender').items()
    }
    visual_style['vertex_style'] = {
        n: style_dict[g]
        for n, g in nx.get_node_attributes(net, 'gender').items()
    }
    visual_style['vertex_label_off'] = {'e': True}
    visual_style['vertex_math_mode'] = {'a': True}
    visual_style['vertex_label_as_id'] = {'f': True}
    visual_style['vertex_pseudo'] = {'d': True}

    # edge styles
    # -----------
    visual_style['edge_width'] = {
        e: .3 + .3 * int(f)
        for e, f in nx.get_edge_attributes(net, 'is_formal').items()
    }
    visual_style['edge_color'] = 'black'
    visual_style['edge_opacity'] = .8
    visual_style['edge_curved'] = 0.1
    visual_style['edge_label'] = {e: e[0] + e[1] for e in net.edges}
    visual_style['edge_label_position'] = 'above'
    visual_style['edge_label_distance'] = .6
    visual_style['edge_label_color'] = 'gray'
    visual_style['edge_label_size'] = {('a', 'c'): 5}
    visual_style['edge_style'] = 'dashed'
    visual_style['edge_arrow_size'] = .2
    visual_style['edge_arrow_width'] = .2
    visual_style['edge_loop_size'] = 15
    visual_style['edge_loop_position'] = 90
    visual_style['edge_loop_shape'] = 45
    visual_style['edge_directed'] = {
        ('a', 'b'): True,
        ('a', 'c'): True,
        ('c', 'd'): False,
        ('d', 'e'): True,
        ('e', 'c'): True,
        ('c', 'f'): False,
        ('f', 'a'): True,
        ('f', 'g'): True,
        ('g', 'g'): True
    }
    visual_style['edge_label'][('a', 'c')] = '\\frac{\\alpha}{\\beta}'
    visual_style['edge_math_mode'] = {('a', 'c'): True}
    visual_style['edge_not_in_bg'] = {('f', 'a'): True}

    # general options
    # ---------------
    visual_style['unit'] = 'mm'
    visual_style['layout'] = layout
    visual_style["margin"] = {'top': 5, 'bottom': 8, 'left': 5, 'right': 5}
    visual_style["canvas"] = (100, 60)
    visual_style['keep_aspect_ratio'] = False

    # Create a latex file
    plot(net, 'network.tex', **visual_style)
예제 #19
0
visual_style['node_size'] = .8
visual_style['standalone'] = False
visual_style['node_opacity'] = 0.5
color_dict = {0: 'blue', 1: 'red', 2: 'green', -1: 'black'}

# layout grille
layout = {}
for y in range(0, 4):
    for x in range(0, 6):
        layout[y * 6 + x] = (x, -y)

# print(layout)

visual_style['layout'] = layout

plot(g1, 'biparti.pdf', **visual_style)

counter = 1


def visite(i, col):
    global counter
    i["vu"] = True
    i["col"] = col
    # à chaque fois qu'on visite un sommet, on exporte le graphe
    filename = 'biparti-{cc}.pdf'.format(cc=counter)
    visual_style['vertex_color'] = [color_dict[g] for g in g1.vs["col"]]
    plot(g1, filename, **visual_style)
    counter = counter + 1
    for j in g1.successors(i):
        if g1.vs[j]["vu"] == False:
예제 #20
0
def visite(l, i):
    global astar
    global gstar
    print('entering visite(' + str(l) + ',' + str(i) + ')')
    astar[l, i] = True
    if (~gstar[l, i]):
        gstar[l, i] = 1
    for j in gstar.successors(i):
        print('on essaie ' + str(l) + ',' + str(j))
        if l == j:
            print("auto")
        else:
            if astar[l, j] == False:
                visite(l, j)


def fermeture_transitive(g):
    global astar
    for e1 in gstar.vs:
        for e2 in gstar.vs:
            astar[e1.index, e2.index] = False
    for i in gstar.vs:
        visite(i.index, i.index)


g1, visual_style = mygraphs.build_graph_exemple_6()
plot(g1, "ft0.tex", **visual_style)
gstar = g1.copy()
fermeture_transitive(g1)
plot(gstar, "ft1.tex", **visual_style)
예제 #21
0
nodes = ['1','2','3','4','5']
edges = [('1','2'),('2','3'),('3','4'),('4','5'),('5','3')]
# gender = ['w' for i in range(N)]
# colors = {'g': 'gray', 'w': 'white'}

g = nx.DiGraph(edges)
style = {}
style["vertex_shape"] = "circle"
style['vertex_size'] = .9
style["edge_width"] = 0.9
#style["edge_label"] = ["+1","+1","+1","+1","+1"]
style['node_label'] = nodes
style['node_label_size'] = 12
style['node_color'] = ["white","white","white","white","white","white"]
#style['edge_color'] = ["red","","","","red"]
#style['node_opacity'] = .5
style['edge_curved'] = .0


#pos = nx.layout.spring_layout(g,k=5/np.sqrt(g.order()))
pos = nx.kamada_kawai_layout(g)
layout = nx.layout.spring_layout(g,pos = pos,iterations = 2)

plot((g),PATH + 'singlecluster.tex',layout= layout,**style)

import os

os.system('pdflatex -output-directory="tesi" tesi/singlecluster.tex')

예제 #22
0
G = json_graph.node_link_graph(data)
coords = nx.kamada_kawai_layout(G, scale=3)  # another layout

# normalize this to [0, 1] so that the colors look different
rank = nx.pagerank(G)
low = min(rank.values())
high = max(rank.values())
span = high - low
for v in rank:
    rank[v] = (rank[v] - low) / span

style = {}
style['node_label'] = [str(v) for v in G.nodes()]
style['node_color'] = [label(rank[v]) for v in G.nodes()]
style['node_opacity'] = 0.9
style['edge_curved'] = 0.2

plot((G.nodes(), G.edges()), 'temp.tex', **style)

# we need to add the palette definitions to the LaTeX source
with open('graph.tex', 'w') as target:
    with open('temp.tex') as source:
        print(source.readline().strip(),
              file=target)  # \documentclass{standalone}
        print(source.readline().strip(),
              file=target)  # \usepackage{tikz-network}
        for color in colors.values():
            print(color, file=target)
        for line in source:  # the rest of it
            print(line.strip(), file=target)
예제 #23
0
# running igraph native tests for install check


import igraph as ig 
from network2tikz import plot

print (ig.__version__)

g = ig.Graph.Famous("petersen")
ig.plot(g)
# also check tikz export
plot(g,'test.tex')
예제 #24
0
import networkx as nx
from network2tikz import plot

#   [1, 2, 3, 4, 5]
l = list(map(str, [3, 5, 1, 2, 4]))

nodes = l
edges = []
for i in range(len(l)):
    edges.append((l[i], str(i + 1)))
# edges = [('a','b'), ('a','c'), ('c','d'),('d','b')]

G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)

style = {}
style['node_label'] = l
# style['node_color'] = [colors[g] for g in gender]
# style['node_opacity'] = .5
style['edge_curved'] = .1
style["edge_directed"] = True

plot((nodes, edges),
     'network.tex',
     **style,
     canvas=(5, 5),
     layout="random",
     force=3 / 4)
예제 #25
0
import igraph as ig
from network2tikz import plot
import mygraphs
import math
import bellman

g = mygraphs.build_exemple_bellmann()
print(g)
visual_style = {}
visual_style['edge_width'] = 2
visual_style['node_size'] = .8
visual_style['standalone'] = False
visual_style['node_opacity'] = 0.5
visual_style['vertex_label'] = g.vs["name"]
visual_style['edge_label'] = g.es["w"]
visual_style['canvas'] = (9, 6)
layout = {
    0: (-1, 0),
    1: (2, 1),
    2: (2, -1),
    3: (4, 0),
    4: (3, -3),
    5: (6, -1),
}
visual_style['layout'] = layout
plot(g, 'bellmann-0.pdf', **visual_style)
bellman.bellman(g, g.vs.find(name="1"))
예제 #26
0
def main():
    # Network
    # -------
    nodes = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    edges = [('a', 'b'), ('a', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'c'),
             ('c', 'f'), ('f', 'a'), ('f', 'g'), ('g', 'd'), ('g', 'g')]
    net = (nodes, edges)

    # Network attributes
    # ------------------
    name = ['Alice', 'Bob', 'Claire', 'Dennis', 'Esther', 'Frank', 'George']
    age = [25, 31, 18, 47, 22, 23, 50]
    gender = ['f', 'm', 'f', 'm', 'f', 'm', 'm']
    is_formal = [
        False, False, True, True, True, False, True, False, False, False
    ]

    # Network dicts
    # -------------
    color_dict = {"m": "blue", "f": "red"}
    shape_dict = {"m": "circle", "f": "rectangle"}
    style_dict = {"m": "{shading=ball}", "f": None}
    layout = {
        'a': (4.3191, -3.5352),
        'b': (0.5292, -0.5292),
        'c': (8.6559, -3.8008),
        'd': (12.4117, -7.5239),
        'e': (12.7, -1.7069),
        'f': (6.0022, -9.0323),
        'g': (9.7608, -12.7)
    }

    # Visual style dict
    # -----------------
    visual_style = {}

    # node styles
    # -----------
    visual_style['node_size'] = 5
    visual_style['node_color'] = [color_dict[g] for g in gender]
    visual_style['node_opacity'] = .7
    visual_style['node_label'] = name
    visual_style['node_label_position'] = 'below'
    visual_style['node_label_distance'] = 15
    visual_style['node_label_color'] = 'gray'
    visual_style['node_label_size'] = 3
    visual_style['node_shape'] = [shape_dict[g] for g in gender]
    visual_style['node_style'] = [style_dict[g] for g in gender]
    visual_style['node_label_off'] = {'e': True}
    visual_style['node_math_mode'] = [True]
    visual_style['node_label_as_id'] = {'f': True}
    visual_style['node_pseudo'] = {'d': True}

    # edge styles
    # -----------
    visual_style['edge_width'] = [.3 + .3 * int(f) for f in is_formal]
    visual_style['edge_color'] = 'black'
    visual_style['edge_opacity'] = .8
    visual_style['edge_curved'] = 0.1
    visual_style['edge_label'] = {e: e[0] + e[1] for e in net[1]}
    visual_style['edge_label_position'] = 'above'
    visual_style['edge_label_distance'] = .6
    visual_style['edge_label_color'] = 'gray'
    visual_style['edge_label_size'] = {('a', 'c'): 5}
    visual_style['edge_style'] = 'dashed'
    visual_style['edge_arrow_size'] = .2
    visual_style['edge_arrow_width'] = .2

    visual_style['edge_loop_size'] = 15
    visual_style['edge_loop_position'] = 90
    visual_style['edge_loop_shape'] = 45
    visual_style['edge_directed'] = [
        True, True, False, True, True, False, True, True, True, True
    ]
    visual_style['edge_label'][('a', 'c')] = '\\frac{\\alpha}{\\beta}'
    visual_style['edge_math_mode'] = {('a', 'c'): True}
    visual_style['edge_not_in_bg'] = {('f', 'a'): True}

    # general options
    # ---------------
    visual_style['unit'] = 'mm'
    visual_style['layout'] = layout
    visual_style["margin"] = {'top': 5, 'bottom': 8, 'left': 5, 'right': 5}
    visual_style["canvas"] = (100, 60)
    visual_style['keep_aspect_ratio'] = False

    # Create a latex file
    plot(net, 'network.tex', **visual_style)
예제 #27
0
plt.tight_layout()
plt.savefig('chicken_out.png', dpi=250, pad=0.0, bbox_inches='tight')
plt.close()

# transforming your network to a latex source file
from network2tikz import plot
nodes = {(u, v): 'N{}'.format(i) for i, (u, v) in enumerate(X)}
edges = [(nodes[x_[i][0], y_[i][0]], nodes[x_[i][1], y_[i][1]])
         for i in range(len(x_))]

# add some additional style to your figure
visual_style = {}
visual_style['layout'] = {v: (k[0], -k[1]) for k, v in nodes.items()}
visual_style['node_size'] = .2
visual_style['edge_opacity'] = .8
visual_style['canvas'] = (25, 25)

# create the latex file
plot((nodes.values(), edges), 'chicken.tex', **visual_style)

# =============================================================================
# eof
#
# Local Variables:
# mode: python
# mode: linum
# mode: auto-fill
# fill-column: 80
# End:
예제 #28
0
        print(str_pred)
        print(str_dist)
        print('----------------------------------------------')

g = mygraphs.build_exemple_pcc()
print(g)
visual_style = {}
visual_style['edge_width'] = 2
visual_style['node_size'] = .8
visual_style['standalone'] = False
visual_style['node_opacity'] = 0.5
visual_style['vertex_label'] = g.vs["name"]
visual_style['edge_label'] = g.es["w"]
visual_style['canvas'] = (9,6)
layout = { 
    0: (-1,-1),
    1: (1,0),
    2: (1,-4),
    3: (2,-2),
    4: (3,-3),
    5: (5,0),
    6: (5,-3),
    7: (6,-4)
}
visual_style['layout'] = layout
plot(g,'ordinal-0.pdf',**visual_style)
ordinal(g,g.vs.find(name="s"))



import igraph as ig
from network2tikz import plot
import mygraphs
import math
import dijkstra

g = mygraphs.build_exemple_pcc()
print(g)
visual_style = {}
visual_style['edge_width'] = 2
visual_style['node_size'] = .8
visual_style['standalone'] = False
visual_style['node_opacity'] = 0.5
visual_style['vertex_label'] = g.vs["name"]
visual_style['edge_label'] = g.es["w"]
visual_style['canvas'] = (9, 6)
layout = {
    0: (-1, -1),
    1: (1, 0),
    2: (1, -4),
    3: (2, -2),
    4: (3, -3),
    5: (5, 0),
    6: (5, -3),
    7: (6, -4)
}
visual_style['layout'] = layout
plot(g, 'dijkstra-0.pdf', **visual_style)
dijkstra.dijkstra(g, g.vs.find(name="s"))
visual_style['edge_width'] = 2
visual_style['node_size'] = .8
visual_style['standalone'] = False
visual_style['node_opacity'] = 0.5
color_dict = {0: 'blue', 1: 'red', 2: 'green', -1: 'black'}

# layout grille
layout = {}
for y in range(0, 4):
    for x in range(0, 6):
        layout[y * 6 + x] = (x, -y)
visual_style['layout'] = layout

# ig.plot(g1, **visual_style)

plot(g1, 'pp-0.tex', **visual_style)

counter = 1


def export_graphe():
    global counter
    filename = 'pl-{cc}.tex'.format(cc=counter)
    visual_style['vertex_color'] = [color_dict[g] for g in g1.vs["vu"]]
    print(visual_style['vertex_color'])
    visual_style['node_label'] = g1.vs["date"]
    print(visual_style['node_label'])
    plot(g1, filename, **visual_style)
    counter = counter + 1