Matplotlib Notes
================
What is Matplotlib?
- A comprehensive library for creating static, animated, and interactive visualizations in Python.
- Part of the PyData stack (with NumPy, Pandas, etc.).
- Works well with Jupyter Notebooks, IDEs, and scripts.
Basic Components
- Figure: The overall window or page.
- Axes: A part of the figure where data is plotted (can have multiple).
- Axis: x-axis and y-axis within each Axes.
- Plot: The actual drawing (line, scatter, bar, etc.).
Installation
pip install matplotlib
Importing
import matplotlib.pyplot as plt
Basic Plot Example
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Basic Line Plot")
plt.grid(True)
plt.show()
Types of Plots
Line: plt.plot()
Scatter: plt.scatter()
Bar: plt.bar()
Histogram: plt.hist()
Pie Chart: plt.pie()
Box Plot: plt.boxplot()
Area Plot: plt.stackplot()
Error Bar: plt.errorbar()
Customization Options
Color: 'r', 'g', 'b', '#00ff00', etc.
Line Style: '-', '--', ':', '-.'
Marker: 'o', '^', 's', 'D', '*', etc.
Labels and Legend
plt.xlabel("X Label")
plt.ylabel("Y Label")
plt.title("Title")
plt.legend(["Series 1"])
Subplots
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.subplot(1, 2, 2)
plt.bar(x, y)
plt.show()
Save Plot
plt.savefig("plot.png")
Using with Pandas
import pandas as pd
data = pd.read_csv("data.csv")
data.plot(kind='line')
plt.show()
Tips
- Use %matplotlib inline in Jupyter.
- Use fig, ax = plt.subplots() for more control.
- Customize ticks with plt.xticks(), plt.yticks().
Matplotlib with Pandas - More Examples
======================================
Sample Data
-----------
data = {'Year': [2018, 2019, 2020, 2021, 2022],
'Sales': [100, 120, 130, 115, 140],
'Profit': [20, 25, 23, 18, 30]}
df = pd.DataFrame(data)
1. Line Plot
df.plot(x='Year', y='Sales', kind='line')
2. Multiple Lines
df.plot(x='Year', y=['Sales', 'Profit'], kind='line')
3. Bar Chart
df.plot(x='Year', y='Sales', kind='bar')
4. Grouped Bar
df.plot(x='Year', kind='bar', stacked=False)
5. Stacked Bar
df.plot(x='Year', kind='bar', stacked=True)
6. Histogram
df['Profit'].plot(kind='hist')
7. Box Plot
df[['Sales', 'Profit']].plot(kind='box')
8. Scatter Plot
df.plot(kind='scatter', x='Sales', y='Profit')
9. Pie Chart
df.set_index('Year')['Sales'].plot(kind='pie')
Subplots
df.plot(x='Year', y=['Sales', 'Profit'], subplots=True)
Annotations
ax = df.plot(x='Year', y='Sales', kind='line')
for i, val in enumerate(df['Sales']):
ax.text(df['Year'][i], val + 2, str(val), ha='center')
Custom Graph with Points
=========================
Using scatter + annotate:
x = [1, 2, 3, 4]
y = [10, 15, 13, 17]
labels = ['A', 'B', 'C', 'D']
plt.scatter(x, y)
list(map(lambda x_, y_, label: plt.annotate(label, (x_, y_ + 0.5)), x, y, labels))
Using Pandas + apply:
df.apply(lambda row: plt.annotate(row['label'], (row['x'], row['y'] + 0.3)), axis=1)
Customizing X and Y Axis Gaps
=============================
1. Using plt.xticks() / plt.yticks()
plt.xticks([1,2,3,4,5])
plt.yticks(range(10, 35, 5))
2. Using MultipleLocator from matplotlib.ticker
from matplotlib.ticker import MultipleLocator
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(5))
3. Setting Axis Limits
plt.xlim(0, 6)
plt.ylim(5, 35)
4. Rotating Tick Labels
plt.xticks(rotation=45)