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 networkx as nx
    import matplotlib.pyplot as plt

# Sample graph data
nodes = ['A', 'B', 'C', 'D', 'E']
edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E'), ('D', 'C')]

# Create a directed graph
graph = nx.DiGraph()
graph.add_nodes_from(nodes)
graph.add_edges_from(edges)

# Set node positions (optional, for better visualization)
pos = nx.spring_layout(graph) # You can experiment with different layouts

# Draw the graph
nx.draw(graph, pos, with_labels=True, node_color='lightblue', edge_color='gray', font_size=12, node_size=500, arrowsize=20)

# Customize the plot (optional)
plt.title("Directed Graph Example")
plt.show()

Model: gemini-1.5-pro-preview-0409
Temperature: 1
Max output tokens: 8192
TopK: 40
TopP: 0.95