Data Visualization
Learn Python's three major visualization libraries — Matplotlib, Seaborn, and Plotly — through practical examples.
Installation
pip install matplotlib seaborn plotly
Matplotlib — Basic Graphs
import matplotlib.pyplot as plt
import numpy as np
# Basic line graph
x = np.linspace(0, 2 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.figure(figsize=(10, 4))
plt.plot(x, y1, label="sin(x)", color="blue", linewidth=2)
plt.plot(x, y2, label="cos(x)", color="red", linestyle="--")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Trigonometric Functions")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig("trig.png", dpi=150)
plt.show()
# Multiple plots (subplot)
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
# Scatter plot
axes[0, 0].scatter(x_data, y_data, alpha=0.6, c=colors, cmap="viridis")
axes[0, 0].set_title("Scatter Plot")
# Bar chart
categories = ["A", "B", "C", "D"]
values = [23, 45, 12, 67]
axes[0, 1].bar(categories, values, color="steelblue", edgecolor="black")
axes[0, 1].set_title("Bar Chart")
# Histogram
data = np.random.randn(1000)
axes[1, 0].hist(data, bins=30, color="skyblue", edgecolor="white")
axes[1, 0].set_title("Histogram")
# Pie chart
sizes = [30, 25, 20, 15, 10]
labels = ["Python", "JavaScript", "Java", "Go", "Rust"]
axes[1, 1].pie(sizes, labels=labels, autopct="%1.1f%%", startangle=90)
axes[1, 1].set_title("Language Market Share")
plt.tight_layout()
plt.show()
Seaborn — Statistical Visualization
import seaborn as sns
import pandas as pd
# Set default theme
sns.set_theme(style="whitegrid", palette="muted")
# Sample datasets
tips = sns.load_dataset("tips") # restaurant tips data
iris = sns.load_dataset("iris") # iris flower data
# 1. boxplot — distribution and outliers
plt.figure(figsize=(10, 5))
sns.boxplot(data=tips, x="day", y="total_bill", hue="sex")
plt.title("Total Bill by Day")
plt.show()
# 2. violinplot — shows distribution shape
sns.violinplot(data=tips, x="day", y="tip", hue="smoker", split=True)
# 3. heatmap — correlation matrix
plt.figure(figsize=(8, 6))
corr = tips.select_dtypes(include="number").corr()
sns.heatmap(corr, annot=True, fmt=".2f", cmap="coolwarm", vmin=-1, vmax=1)
plt.title("Correlation Heatmap")
plt.show()
# 4. pairplot — relationships between all variable pairs
sns.pairplot(iris, hue="species", diag_kind="kde")
plt.show()
# 5. scatterplot + regression line
sns.lmplot(data=tips, x="total_bill", y="tip", hue="smoker",
col="time", height=5)
# 6. barplot (mean + confidence interval)
sns.barplot(data=tips, x="day", y="total_bill", estimator="mean",
errorbar="ci", palette="Blues_d")
# 7. countplot (frequency)
sns.countplot(data=tips, x="day", order=["Thur", "Fri", "Sat", "Sun"])
Plotly — Interactive Charts
import plotly.express as px
import plotly.graph_objects as go
# Sample data
df = px.data.gapminder()
# 1. Scatter (bubble chart)
fig = px.scatter(
df[df.year == 2007],
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
log_x=True,
title="2007 GDP vs Life Expectancy",
)
fig.show()
# 2. Line graph (time series)
korea = df[df.country == "Korea, Rep."]
fig = px.line(korea, x="year", y="gdpPercap", markers=True,
title="South Korea GDP Over Time")
fig.update_traces(line_width=3)
fig.show()
# 3. Bar chart
fig = px.bar(
df[df.year == 2007].nlargest(10, "pop"),
x="country", y="pop",
color="continent",
title="Top 10 Countries by Population",
)
fig.show()
# 4. Animation (change over time)
fig = px.scatter(
df,
x="gdpPercap", y="lifeExp",
size="pop", color="continent",
animation_frame="year",
animation_group="country",
range_x=[100, 100000], range_y=[20, 90],
log_x=True,
title="GDP vs Life Expectancy Over Time",
)
fig.show()
# 5. Graph Objects — fine-grained control
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers", name="Data1"))
fig.add_trace(go.Bar(x=["A", "B", "C"], y=[10, 20, 15], name="Data2"))
fig.update_layout(
title="Combined Chart",
xaxis_title="X Axis",
yaxis_title="Y Axis",
template="plotly_white",
)
fig.show()
Saving Charts
# Matplotlib
plt.savefig("chart.png", dpi=300, bbox_inches="tight")
plt.savefig("chart.pdf") # vector format
# Plotly
fig.write_html("chart.html") # interactive HTML
fig.write_image("chart.png") # static image (requires kaleido)
# pip install kaleido
Library Selection Guide
Matplotlib
✅ When you need complete control
✅ Publication/report quality output
✅ Custom chart implementation
❌ Requires more code
Seaborn
✅ Statistical visualization (distributions, relationships, categories)
✅ Perfect compatibility with pandas DataFrames
✅ Beautiful charts with minimal code
❌ No interactive features
Plotly
✅ Web dashboards, interactive exploration
✅ Animations, 3D charts
✅ Build web apps with Dash integration
❌ Larger file sizes
Summary
| Library | Strength | Key Charts |
|---|---|---|
| Matplotlib | Fine-grained control | line/bar/scatter/pie |
| Seaborn | Statistical visualization | boxplot/heatmap/pairplot |
| Plotly | Interactive | bubble/animation/3D |
Use Seaborn for data exploration, Plotly for final presentations, and Matplotlib for papers and reports.