🧠 AI Computer Institute
Content is AI-generated for educational purposes. Verify critical information independently. A bharath.ai initiative.

Data Visualization Cheat Sheet

dataGrades 9-125 sections

Chart Types & When to Use

Chart TypeUse CaseAxesExample Data
Line PlotTrends over timeTime vs ValueStock price history
Scatter PlotRelationshipsX vs Y continuousHeight vs Weight
Bar ChartCategorical comparisonCategory vs ValueSales by country
HistogramDistributionValue rangesAge distribution
Box PlotQuartiles & outliersCategory vs ValueSalary by job level
Heatmap2D correlationsX category vs Y categoryCorrelation matrix
Pie ChartPart-to-whole (caution!)Categories & proportionsMarket share ₹ 40% vs 60%
Area ChartStacked trendsTime vs Stacked valuesTraffic by source
Violin PlotDistribution shapeCategory vs ValueIncome by gender
KDE PlotProbability densityValue (continuous)Income distribution

Matplotlib Basics

import matplotlib.pyplot as plt
import numpy as np

// Basic line plot
plt.plot([1, 2, 3], [1, 4, 9])
plt.xlabel('X label')
plt.ylabel('Y label')
plt.title('Title')
plt.show()

// Figure & Axes (object-oriented)
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot([1, 2, 3], [1, 4, 9], label='Line')
ax.scatter([1, 2, 3], [1, 4, 9], label='Points')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
plt.show()

// Multiple subplots
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
axes[0, 0].plot([1, 2, 3], [1, 4, 9])
axes[0, 1].scatter([1, 2, 3], [1, 4, 9])
axes[1, 0].bar(['A', 'B', 'C'], [10, 20, 15])

// Styling
plt.plot(x, y, 'r--', label='Red dashed')  # 'r'=red, '--'=dashed
plt.plot(x, y, 'b-o', linewidth=2, markersize=8)  # blue, solid, circles

// Line styles: '-', '--', '-.', ':'
// Colors: 'r', 'b', 'g', 'k', 'c', 'm', 'y'
// Markers: 'o', 's', '^', 'x', '+', '*'

// Legends & annotations
plt.legend(loc='best')  # 'upper left', 'lower right', etc.
plt.annotate('Max point', xy=(3, 9), xytext=(2.5, 8),
            arrowprops=dict(arrowstyle='->', color='red'))

// Limits & grids
ax.set_xlim(0, 10)
ax.set_ylim(0, 100)
ax.grid(True, alpha=0.3)

// Save
plt.savefig('plot.png', dpi=300, bbox_inches='tight')

Seaborn for Statistical Graphics

import seaborn as sns
import pandas as pd

// Styling
sns.set_style("whitegrid")  # white, dark, darkgrid, ticks
sns.set_palette("husl")      # Color palette

// Distributions
sns.histplot(data=df, x='age', kde=True, bins=30)
sns.kdeplot(data=df, x='age', hue='gender')
sns.distplot(df['age'])  # Deprecated, use histplot

// Categorical plots
sns.barplot(data=df, x='country', y='sales')
sns.boxplot(data=df, x='country', y='salary')
sns.violinplot(data=df, x='gender', y='income')
sns.stripplot(data=df, x='category', y='value')
sns.swarmplot(data=df, x='category', y='value')  # No overlap

// Relationship plots
sns.scatterplot(data=df, x='age', y='income', hue='gender')
sns.lineplot(data=df, x='date', y='sales', hue='product')
sns.regplot(data=df, x='age', y='income')  # With regression line
sns.lmplot(data=df, x='age', y='income', hue='gender')  # Faceted

// Correlation heatmap
sns.heatmap(df.corr(), annot=True, cmap='coolwarm', center=0)

// Joint plots (bivariate + marginal)
sns.jointplot(data=df, x='age', y='income', kind='scatter')
# kind: 'scatter', 'kde', 'hex', 'hist', 'reg'

// Pair plots (all columns)
sns.pairplot(df, hue='gender')

// FacetGrid (multi-panel)
g = sns.FacetGrid(df, col='country', row='gender', height=4)
g.map(sns.scatterplot, 'age', 'income')

Plotly for Interactive Plots

import plotly.express as px
import plotly.graph_objects as go

// Quick plots (Express)
fig = px.line(df, x='date', y='sales', title='Sales Over Time')
fig.show()

fig = px.scatter(df, x='age', y='income', color='gender', size='expense')
fig.show()

fig = px.bar(df, x='country', y='sales', color='product')
fig.show()

fig = px.box(df, x='country', y='salary')
fig.show()

// 3D scatter
fig = px.scatter_3d(df, x='age', y='income', z='expense', color='gender')

// Interactive line
fig = px.line(df, x='date', y='value', hover_name='name')

// Sunburst (hierarchical)
fig = px.sunburst(df, ids='id', labels='label', parents='parent', values='value')

// Graph objects (advanced control)
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 4, 9], mode='lines+markers'))
fig.update_layout(title='My Plot', xaxis_title='X', yaxis_title='Y')
fig.show()

// Annotations
fig.add_annotation(text='Important', x=2, y=4, showarrow=True)

// Subplots
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=2)
fig.add_trace(go.Scatter(x=[1,2], y=[1,4]), row=1, col=1)
fig.add_trace(go.Bar(x=['A','B'], y=[10,20]), row=1, col=2)

// Styling
fig.update_xaxes(title_text='X Label')
fig.update_yaxes(title_text='Y Label')
fig.update_layout(hovermode='x unified', template='plotly_white')

Styling & Best Practices

// Color palettes
Categorical: Set1, Set2, tab10, husl
Sequential: Blues, Greens, YlOrRd
Diverging: RdBu, RdYlGn, coolwarm

Avoid: Jet (bad for colorblind)
Use: Viridis, Blues, Reds (good for colorblind)

// Design tips
Title: Clear, descriptive
Labels: Readable, with units (₹ thousands, %)
Legend: Positioned to avoid data
Gridlines: Light, not distracting
Aspect ratio: Typically 16:9 or 4:3
Font size: Readable (12pt min)

// Colors by audience
Professional: Blues, grays
Marketing: Bold, vibrant
Scientific: Colorblind-friendly (viridis)

// Annotations
Important: Highlight key points
Outliers: Label unusual values
Comparisons: Add reference lines

// Common mistakes
❌ Dual y-axes (misleading scaling)
❌ Pie charts (hard to compare)
❌ 3D effects (distort perception)
❌ Too many lines on one plot (confusing)
✓ Small multiples (clear comparisons)
✓ Log scale when needed (wide range)
✓ Consistent color schemes

// Code example (professional style)
fig, ax = plt.subplots(figsize=(12, 6))
ax.plot(df['date'], df['value'], linewidth=2, color='#1f77b4')
ax.fill_between(df['date'], df['value'], alpha=0.3)
ax.set_xlabel('Date', fontsize=12)
ax.set_ylabel('Revenue (₹ thousands)', fontsize=12)
ax.set_title('Monthly Revenue Trend', fontsize=14, fontweight='bold')
ax.grid(True, alpha=0.3, linestyle='--')
plt.tight_layout()
plt.savefig('revenue.png', dpi=300)

More Cheat Sheets