iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Plotting

pandas has a thin .plot() API over matplotlib that turns a DataFrame into a chart in one line. For polished output, drop into matplotlib for fine control or move to seaborn/plotly for statistical chart types. The pandas + matplotlib pair covers ~90% of day-to-day analysis plotting.

Line, bar, hist, scatter, and a small dashboard

EXAMPLE
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Toy dataset: 90 days of sales by region
dates = pd.date_range('2026-03-01', periods=90, freq='D')
rng = np.random.default_rng(7)
df = pd.DataFrame({
    'date':   dates,
    'region': rng.choice(['Sydney','Melbourne','Brisbane','Perth'], 90),
    'orders': rng.poisson(40, 90) + rng.integers(0, 30, 90),
    'aov':    rng.uniform(45, 120, 90),
})
df['revenue'] = df['orders'] * df['aov']

# 1) Line chart of daily revenue, smoothed
ax = df.set_index('date')['revenue'].rolling(7).mean().plot(
    figsize=(9, 3), title='7-day rolling revenue', ylabel='AUD',
)
ax.grid(True, alpha=0.3)
plt.tight_layout(); plt.savefig('rev_line.png'); plt.close()

# 2) Bar of total revenue by region
totals = df.groupby('region', as_index=True)['revenue'].sum().sort_values()
ax = totals.plot.barh(figsize=(8, 3), title='Revenue by region', xlabel='AUD')
plt.tight_layout(); plt.savefig('rev_region.png'); plt.close()

# 3) Histogram of order counts
ax = df['orders'].plot.hist(bins=20, figsize=(8, 3), title='Daily orders distribution')
ax.axvline(df['orders'].mean(), color='red', linestyle='--', label='mean')
ax.legend()
plt.tight_layout(); plt.savefig('orders_hist.png'); plt.close()

# 4) Scatter: orders vs aov, coloured by region (using matplotlib directly)
fig, ax = plt.subplots(figsize=(8, 4))
for region, sub in df.groupby('region'):
    ax.scatter(sub['orders'], sub['aov'], label=region, alpha=0.7)
ax.set_xlabel('orders'); ax.set_ylabel('avg order value (AUD)')
ax.set_title('Orders vs AOV')
ax.legend()
plt.tight_layout(); plt.savefig('orders_aov.png'); plt.close()

# 5) Small multiples — one panel per region
fig, axes = plt.subplots(2, 2, figsize=(10, 6), sharex=True)
for ax, (region, sub) in zip(axes.flatten(), df.groupby('region')):
    sub.set_index('date')['revenue'].rolling(7).mean().plot(ax=ax, title=region)
    ax.grid(True, alpha=0.3)
fig.suptitle('7-day rolling revenue, per region')
plt.tight_layout(); plt.savefig('rev_facets.png'); plt.close()

# 6) Save chart-ready data as a Parquet for the BI team
df.to_parquet('daily_sales.parquet', index=False)

Why it matters

Reach for plotly or altair when interactivity (hover, zoom, click filters) is required — pandas + matplotlib produces static PNG/PDF. For notebooks and quick exploration, default to plt; for production reports, hand the data off to a real visualisation library that the audience can interact with.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
import pandas as pd
import matplotlib.pyplot as plt
df['close'].plot(title='Close price')
df.groupby('country')['amount'].sum().plot.bar()
plt.show()
Try it Yourself »

Discussion

Loading…