Python Visualization

Write Python code to visualize a weighted directed graph with example data

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.

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