show me examples of plotting, diagrams and graphs in python
- User
show me examples of plotting, diagrams and graphs in python
- Assistant
Below is a buffet-style sampler of popular ways to create plots, diagrams, and graphs in Python. Copy-paste any cell into a Jupyter notebook (or plain
.pyfile) and run it; every snippet is self-contained.(If you’re in a fresh environment, first do
pip install matplotlib seaborn plotly networkx graphviz diagrams)
1. Classic 2-D Charts with Matplotlib
import numpy as np import matplotlib.pyplot as plt # Fake data x = np.linspace(0, 10, 200) y1 = np.sin(x) y2 = np.cos(x) plt.figure(figsize=(8,4)) plt.plot(x, y1, label='sin(x)', linewidth=2) plt.plot(x, y2, label='cos(x)', linewidth=2, linestyle='--') plt.title("Sine & Cosine Waves") plt.xlabel("x") plt.ylabel("y") plt.legend() plt.grid(True) plt.tight_layout() plt.show()What you get ➜ two clean, labeled curves.
2. Multiple Subplots
fig, ax = plt.subplots(2, 2, figsize=(8,6)) # Top-left: scatter rng = np.random.RandomState(0) ax[0,0].scatter(rng.randn(100), rng.randn(100), c=rng.randn(100), cmap='viridis') ax[0,0].set_title("Scatter") # Top-right: bar categories = ['A','B','C','D'] values = [4,7,1,8] ax[0,1].bar(categories, values, color='coral') ax[0,1].set_title("Bar") # Bottom-left: histogram ax[1,0].hist(rng.randn(500), bins=30, color='slateblue') ax[1,0].set_title("Histogram") # Bottom-right: line ax[1,1].plot(x, np.exp(-x/3)*np.sin(5*x), color='green') ax[1,1].set_title("Damped Sine") plt.tight_layout() plt.show()
3. Statistical & “Pretty” Plots with Seaborn
import seaborn as sns tips = sns.load_dataset("tips") # 3a. Violin + swarm overlay plt.figure(figsize=(6,4)) sns.violinplot(x='day', y='total_bill', data=tips, inner=None, color='lightgrey') sns.swarmplot(x='day', y='total_bill', data=tips, hue='sex', dodge=True, size=3) plt.title("Bill Distribution by Day & Sex") plt.legend(bbox_to_anchor=(1,1)) plt.tight_layout() plt.show() # 3b. Correlation heat-map plt.figure(figsize=(5,4)) sns.heatmap(tips.corr(numeric_only=True), annot=True, cmap='coolwarm') plt.title("Correlation matrix") plt.show()
4. Interactive Plot with Plotly
import plotly.express as px df = px.data.gapminder().query("year==2007") fig = px.scatter( df, x='gdpPercap', y='lifeExp', size='pop', color='continent', hover_name='country', log_x=True, size_max=60, title="Gapminder 2007: Bubble chart") fig.show()Now you can zoom, pan, hover, and export the figure right in your browser / JupyterLab session. 🔥
5. Graph/Network Diagrams with NetworkX
import networkx as nx import matplotlib.pyplot as plt G = nx.barabasi_albert_graph(25, 2) # scale-free example graph pos = nx.spring_layout(G, seed=42) plt.figure(figsize=(6,6)) nx.draw_networkx_nodes(G, pos, node_color=range(25), cmap='plasma', node_size=200) nx.draw_networkx_edges(G, pos, alpha=.5) plt.axis('off') plt.title("Scale-Free Network (Barabási–Albert)") plt.show()
6. Flowcharts / System Diagrams (Graphviz)
from graphviz import Digraph dot = Digraph(comment="ETL Pipeline") dot.attr(rankdir='LR') dot.node('A', 'Raw CSV') dot.node('B', 'Cleaned\nDataFrame') dot.node('C', 'DB Table') dot.node('D', 'Dashboard') dot.edges(['AB','BC','CD']) dot # In Jupyter this displays inline; otherwise use dot.render('etl', format='png')Produces a crisp left-to-right ETL flowchart.
7. Cloud Architecture Sketches (“diagrams” library)
from diagrams import Diagram from diagrams.aws.compute import EC2 from diagrams.aws.database import RDS from diagrams.aws.network import ELB with Diagram("3-Tier Web App", show=False): lb = ELB("Load Balancer") web = [EC2("Web1"), EC2("Web2"), EC2("Web3")] db = RDS("Aurora") lb >> web >> dbA PNG (
3-Tier Web App.png) is generated; each icon is from AWS’s official set.
8. Matplotlib Styles & Themes (bonus)
plt.style.use("fivethirtyeight") # Try: 'ggplot', 'seaborn-v0_8-dark', etc. plt.plot(x, np.tan(x), color='steelblue') plt.title("Quick style swap") plt.show()
TL;DR
• Matplotlib = bread-and-butter 📈
• Seaborn = statistical / aesthetic 🎨
• Plotly = interactive & web-ready 🚀
• NetworkX / Graphviz / diagrams = “nodes & edges” or architecture 🗺️Happy plotting!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.