Python Docs

Data Visualization

Data visualization helps you understand patterns, trends, and distributions in data. Here we use Matplotlib for low-level control and Seaborn for high-level, statistical plots.

Matplotlib

Matplotlib is the core plotting library in Python. You can create line plots, bar charts, scatter plots, histograms, and more. Below is a simple example plotting a sine wave.

Example

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)

plt.plot(x, y, label='sin')
plt.legend()
plt.title('Sine')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

What you see:

A smooth sine curve between 0 and 2π with labeled axes and a legend.

Seaborn

Seaborn is built on top of Matplotlib and is great for statistical visualizations. It works well with pandas DataFrames and comes with built-in datasets. Below we plot a boxplot of restaurant tips data.

Example

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

df = sns.load_dataset('tips')
sns.boxplot(df, x='day', y='total_bill', hue='sex')
sns.despine()
plt.show()

What you see:

A grouped boxplot showing the distribution of total bill amounts across different days of the week, separated by gender (hue = sex). This quickly shows median, spread, and outliers.

Tips

  • Use plt.figure(figsize=(w, h)) to control plot size.
  • Start with Seaborn for quick EDA (Exploratory Data Analysis), then customize details with Matplotlib.
  • Always label axes and add titles/legends for clarity.