A partire dal 29 aprile 2025, i modelli Gemini 1.5 Pro e Gemini 1.5 Flash non sono disponibili nei progetti che non li hanno mai utilizzati, inclusi i nuovi progetti. Per maggiori dettagli, vedi Versioni e ciclo di vita dei modelli.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Scrivi codice Python per visualizzare un grafo diretto ponderato con dati di esempio
Puoi eseguire query direttamente su un modello e testare i risultati restituiti quando utilizzi valori di parametro diversi con la console Cloud o chiamando direttamente l'API Vertex AI.
Scrivi codice Python per disegnare un grafo diretto utilizzando matplotlib e networkx. Mostra come utilizzare il codice con alcuni dati di esempio. Includi tutte le istruzioni di importazione necessarie.
Risposta
importmatplotlib.pyplotaspltimportnetworkxasnx# Example data: a directed graph with nodes and edgesnodes=['A','B','C','D','E']edges=[('A','B'),('A','C'),('B','D'),('C','E'),('D','E')]# Create a directed graph objectgraph=nx.DiGraph()# Add nodes to the graphgraph.add_nodes_from(nodes)# Add edges to the graphgraph.add_edges_from(edges)# Draw the graph using matplotlibnx.draw(graph,with_labels=True,node_color='lightblue',node_size=500,font_size=10,arrowsize=20)# Display the graphplt.show()
Spiegazione:
Importa le librerie necessarie:
matplotlib.pyplot per tracciare il grafico.
networkx per creare e manipolare il grafico.
Definisci i dati di esempio:
nodes: un elenco di nomi di nodi.
edges: un elenco di tuple che rappresentano archi diretti, dove ogni tupla è (nodo di origine, nodo di destinazione).
Crea un oggetto grafo diretto:
graph = nx.DiGraph(): crea un oggetto grafico diretto vuoto.
Aggiungi nodi ed archi:
graph.add_nodes_from(nodes): aggiunge al grafico tutti i nodi dell'elenco nodes.
graph.add_edges_from(edges): aggiunge al grafico tutti gli spigoli dell'elenco edges.
Disegna il grafico:
nx.draw(graph, ...): disegna il grafico utilizzando matplotlib.
with_labels=True: mostra le etichette dei nodi.
node_color='lightblue': imposta il colore del nodo su azzurro chiaro.
node_size=500: imposta la dimensione del nodo.
font_size=10: imposta le dimensioni dei caratteri per le etichette.
arrowsize=20: imposta le dimensioni delle frecce per i bordi.
Visualizza il grafico:
plt.show(): mostra il grafico tracciato.
Output:
Questo codice genera una visualizzazione di grafo diretto con i nodi e gli archi specificati. I nodi saranno etichettati, colorati in blu chiaro e collegati da frecce che rappresentano la direzione degli spigoli.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[],[],null,["Write Python code to visualize a weighted directed graph with example data\n\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| You can query a model directly and test the results returned when using different parameter values with the Cloud console, or by calling the Vertex AI API directly. - For an example of using the Vertex AI API, see [Quickstart using the Vertex AI API](/vertex-ai/generative-ai/docs/start/quickstarts/api-quickstart). - To view this sample in the Cloud console: [Go to Google Cloud console](https://console.cloud.google.com/vertex-ai/generative/language/prompt-examples/Python%20visualization) \u003cbr /\u003e ||\n| Prompt Freeform Write Python code to draw a directed graph using matplotlib and networkx. Show how to use the code with some example data. Include all necessary import statements. Response import matplotlib.pyplot as plt import networkx as nx # Example data: a directed graph with nodes and edges nodes = ['A', 'B', 'C', 'D', 'E'] edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E'), ('D', 'E')] # Create a directed graph object graph = nx.DiGraph() # Add nodes to the graph graph.add_nodes_from(nodes) # Add edges to the graph graph.add_edges_from(edges) # Draw the graph using matplotlib nx.draw(graph, with_labels=True, node_color='lightblue', node_size=500, font_size=10, arrowsize=20) # Display the graph plt.show() **Explanation:** 1. **Import necessary libraries:** 2. `matplotlib.pyplot` for plotting the graph. 3. `networkx` for creating and manipulating the graph. 4. **Define example data:** 5. `nodes`: A list of node names. 6. `edges`: A list of tuples representing directed edges, where each tuple is (source node, target node). 7. **Create a directed graph object:** 8. `graph = nx.DiGraph()`: Creates an empty directed graph object. 9. **Add nodes and edges:** 10. `graph.add_nodes_from(nodes)`: Adds all nodes from the `nodes` list to the graph. 11. `graph.add_edges_from(edges)`: Adds all edges from the `edges` list to the graph. 12. **Draw the graph:** 13. `nx.draw(graph, ...)`: Draws the graph using matplotlib. 14. `with_labels=True`: Displays node labels. 15. `node_color='lightblue'`: Sets the node color to light blue. 16. `node_size=500`: Sets the node size. 17. `font_size=10`: Sets the font size for labels. 18. `arrowsize=20`: Sets the arrow size for edges. 19. **Display the graph:** 20. `plt.show()`: Shows the plotted graph. **Output:** This code will generate a directed graph visualization with the specified nodes and edges. The nodes will be labeled, colored light blue, and connected by arrows representing the direction of the edges. | |------------------------|----------------------| | **Model:** | gemini-1.5-flash-001 | | **Temperature:** | 1 | | **Max output tokens:** | 8192 | | **TopK:** | 40 | | **TopP:** | 0.95 | |"]]