Data Visualisation — Seeing Data Clearly

Good visualisations turn data into intuitive graphics so humans can quickly see patterns, compare values and spot anomalies.

Choose the Right Chart

  • Comparison — bar charts.
  • Composition — stacked bars, treemaps.
  • Distribution — histograms, box plots, violin plots.
  • Relationship — scatter plots, bubble charts.
  • Trend over time — line charts.

Tools

matplotlib, seaborn, Plotly, D3.js, Tableau and Power BI.

Code Examples: Data Visualization (5 runnable snippets)

Copy any block into a file or notebook and run it end-to-end — each example stands alone.

Example 1: Annotated bar chart with ranked categories

# Example 1: Annotated bar chart with ranked categories -- Data Visualization
import matplotlib.pyplot as plt
import numpy as np

products = np.array(["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot"])
revenue  = np.array([ 42_000, 61_500, 28_300, 74_800, 19_200, 54_100])

order    = np.argsort(revenue)[::-1]
products = products[order]; revenue = revenue[order]

fig, ax = plt.subplots(figsize=(8, 4.5))
bars    = ax.barh(products, revenue, color="#4c78a8")
ax.invert_yaxis()
ax.set_xlabel("Revenue (USD)")
ax.set_title("Q2 revenue by product")
for bar, v in zip(bars, revenue):
    ax.text(v + 1_000, bar.get_y() + bar.get_height()/2,
            f"${v:,.0f}", va="center", fontsize=10)
ax.spines[["top", "right"]].set_visible(False)
fig.tight_layout(); fig.savefig("revenue.png", dpi=120)

Example 2: Correlation heatmap with annotations

# Example 2: Correlation heatmap with annotations -- Data Visualization
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

rng = np.random.default_rng(0)
df  = pd.DataFrame(rng.standard_normal((400, 6)),
                   columns=list("ABCDEF"))
df["B"] += 0.7 * df["A"]
df["D"] -= 0.5 * df["C"]

corr = df.corr().round(2)
fig, ax = plt.subplots(figsize=(6, 5))
sns.heatmap(corr, annot=True, cmap="coolwarm", center=0,
            vmin=-1, vmax=1, square=True,
            cbar_kws={"shrink": 0.7}, ax=ax)
ax.set_title("Feature correlations")
fig.tight_layout(); fig.savefig("corr.png", dpi=120)

Example 3: Matplotlib figure with dual y-axes

# Example 3: Matplotlib figure with dual y-axes -- Data Visualization
import numpy as np
import matplotlib.pyplot as plt

rng   = np.random.default_rng(0)
days  = np.arange(1, 31)
rev   = 100 + 3 * days + rng.normal(0, 8, 30)
conv  = 0.025 + 0.0008 * days + rng.normal(0, 0.003, 30)

fig, ax1 = plt.subplots(figsize=(8, 4))
ax1.plot(days, rev,  color="#1f77b4", marker="o", label="Revenue")
ax1.set_xlabel("Day")
ax1.set_ylabel("Revenue (USD)", color="#1f77b4")

ax2 = ax1.twinx()
ax2.plot(days, conv, color="#d62728", marker="s", label="Conversion")
ax2.set_ylabel("Conversion rate", color="#d62728")

ax1.set_title("Revenue and conversion over 30 days")
fig.tight_layout()
fig.savefig("metrics.png", dpi=120)

Example 4: Seaborn KDE grid with per-group densities

# Example 4: Seaborn KDE grid with per-group densities -- Data Visualization
import seaborn as sns
import numpy as np
import pandas as pd

rng = np.random.default_rng(0)
df  = pd.DataFrame({
    "group": np.repeat(["A", "B", "C"], 400),
    "value": np.concatenate([
        rng.normal(50, 10, 400),
        rng.normal(58, 12, 400),
        rng.normal(63,  9, 400),
    ]),
})

sns.set_theme(style="whitegrid")
g = sns.displot(
    df, x="value", hue="group",
    kind="kde", fill=True, common_norm=False,
    palette="deep", height=4, aspect=1.6,
)
g.set_axis_labels("value", "density")
g.fig.suptitle("KDE by group", y=1.02)
g.fig.savefig("kde.png", dpi=120)

Example 5: Interactive Plotly bubble chart export

# Example 5: Interactive Plotly bubble chart export -- Data Visualization
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, template="plotly_white",
    title="Life expectancy vs GDP per capita (2007)",
)
fig.update_layout(legend_title_text="Continent")
fig.write_html("gapminder.html")
fig.write_image("gapminder.png", scale=2)