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 [Link] as plt
Basic Plot Example
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
[Link](x, y)
[Link]("X-axis")
[Link]("Y-axis")
[Link]("Basic Line Plot")
[Link](True)
[Link]()
Types of Plots
Line: [Link]()
Scatter: [Link]()
Bar: [Link]()
Histogram: [Link]()
Pie Chart: [Link]()
Box Plot: [Link]()
Area Plot: [Link]()
Error Bar: [Link]()
Customization Options
Color: 'r', 'g', 'b', '#00ff00', etc.
Line Style: '-', '--', ':', '-.'
Marker: 'o', '^', 's', 'D', '*', etc.
Labels and Legend
[Link]("X Label")
[Link]("Y Label")
[Link]("Title")
[Link](["Series 1"])
Subplots
[Link](1, 2, 1)
[Link](x, y)
[Link](1, 2, 2)
[Link](x, y)
[Link]()
Save Plot
[Link]("[Link]")
Using with Pandas
import pandas as pd
data = pd.read_csv("[Link]")
[Link](kind='line')
[Link]()
Tips
- Use %matplotlib inline in Jupyter.
- Use fig, ax = [Link]() for more control.
- Customize ticks with [Link](), [Link]().
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 = [Link](data)
1. Line Plot
[Link](x='Year', y='Sales', kind='line')
2. Multiple Lines
[Link](x='Year', y=['Sales', 'Profit'], kind='line')
3. Bar Chart
[Link](x='Year', y='Sales', kind='bar')
4. Grouped Bar
[Link](x='Year', kind='bar', stacked=False)
5. Stacked Bar
[Link](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
[Link](kind='scatter', x='Sales', y='Profit')
9. Pie Chart
df.set_index('Year')['Sales'].plot(kind='pie')
Subplots
[Link](x='Year', y=['Sales', 'Profit'], subplots=True)
Annotations
ax = [Link](x='Year', y='Sales', kind='line')
for i, val in enumerate(df['Sales']):
[Link](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']
[Link](x, y)
list(map(lambda x_, y_, label: [Link](label, (x_, y_ + 0.5)), x, y, labels))
Using Pandas + apply:
[Link](lambda row: [Link](row['label'], (row['x'], row['y'] + 0.3)), axis=1)
Customizing X and Y Axis Gaps
=============================
1. Using [Link]() / [Link]()
[Link]([1,2,3,4,5])
[Link](range(10, 35, 5))
2. Using MultipleLocator from [Link]
from [Link] import MultipleLocator
[Link].set_major_locator(MultipleLocator(1))
[Link].set_major_locator(MultipleLocator(5))
3. Setting Axis Limits
[Link](0, 6)
[Link](5, 35)
4. Rotating Tick Labels
[Link](rotation=45)