Écrire du code Python pour visualiser un graphique dirigé pondéré avec des exemples de données
Vous pouvez interroger un modèle directement et tester les résultats renvoyés lors de l'utilisation de différentes valeurs de paramètre en utilisant la console Cloud ou en appelant directement l'API Vertex AI.
|
|||||||||||
Invite forme libreWrite Python code to draw nodes and edges of a weighted directed graph. Each edge should be color-coded according to its weight. Add example data to the graph to illustrate how to use the code. Réponse```python import networkx as nx import matplotlib.pyplot as plt # Create a weighted directed graph G = nx.DiGraph() G.add_weighted_edges_from([ ('A', 'B', 0.6), ('A', 'C', 0.2), ('B', 'D', 0.1), ('C', 'D', 0.7), ]) # Color-code the edges according to their weight edge_colors = [plt.cm.jet(weight) for weight in nx.get_edge_attributes(G, 'weight').values()] # Draw the graph nx.draw(G, with_labels=True, edge_color=edge_colors) plt.show() ``` |
|