def run_query_with_viz(query, title, viz_query=None): print(f"n=== {title} ===") # Run and display query results as a DataFrame results = graph.run(query).data() df = pd.DataFrame(results) display(df) # Create visualization plt.figure(figsize=(12, 8)) G = nx.Graph() # Add nodes and edges for record in results: product_name = record['Product'] manufacturer_name = record['Manufacturer'] G.add_node(product_name, label=product_name[:30], type='Product') G.add_node(manufacturer_name, label=manufacturer_name, type='Manufacturer') G.add_edge(product_name, manufacturer_name) # Draw graph pos = nx.spring_layout(G) nx.draw_networkx_nodes(G, pos, nodelist=[n for n, attr in G.nodes(data=True) if attr['type'] == 'Product'], node_color='lightblue', node_size=500, label='Products') nx.draw_networkx_nodes(G, pos, nodelist=[n for n, attr in G.nodes(data=True) if attr['type'] == 'Manufacturer'], node_color='lightgreen', node_size=700, label='Manufacturers') nx.draw_networkx_edges(G, pos) nx.draw_networkx_labels(G, pos) plt.title(title) plt.legend() plt.axis('off') plt.show() # Find most expensive products query1 = """ MATCH (p:Product)-[:MANUFACTURED_BY]->(m:Manufacturer) RETURN m.name as Manufacturer, p.name as Product, p.price as Price ORDER BY p.price DESC LIMIT 5 """ run_query_with_viz(query1, "Most Expensive Products")