0% found this document useful (0 votes)
6 views

15octmatplotlib 2024

Marolotlib
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

15octmatplotlib 2024

Marolotlib
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 1: Introduction to Matplotlib

- Overview of Matplotlib: What is Matplotlib, its history, and importance in data


visualization.
- Installation: Installing Matplotlib using pip or conda.
- Basic Structure:
- pyplot module.
- Creating a simple plot (line, bar, scatter, etc.).
- Plot Display Options: Inline plots (e.g., in Jupyter notebooks) and standalone
plots.

1. Overview of Matplotlib
- What is Matplotlib?
Matplotlib is a Python library used to create graphs and charts. It's widely
used for
data visualization.
- History:
Matplotlib was created by John D. Hunter in 2003 to make easy, flexible plots.
- Importance in Data Visualization:
Data visualization helps to understand complex data easily by turning numbers
into visual
graphs like line plots, bar charts, etc. Matplotlib is one of the best tools
for this in
Python.

2. Installation
- To use Matplotlib, you need to install it first:
- Using pip (the Python package manager):
bash
pip install matplotlib

- Using conda (if you're using Anaconda):


bash
conda install matplotlib

3. Basic Structure
- pyplot Module:
Matplotlib has a module called pyplot that makes it easy to create plots.
You can think of pyplot as a set of commands that help you make different
types of plots.
- Creating a Simple Plot:
- Line plot example:
python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4]) Simple line plot
plt.show() Display the plot

- Bar plot example:


python
plt.bar([1, 2, 3], [4, 5, 6]) Bar chart
plt.show() Display the plot

- Scatter plot example:


python
plt.scatter([1, 2, 3], [4, 5, 6]) Scatter plot
plt.show() Display the plot

4. Plot Display Options


- Inline Plots (in Jupyter notebooks):
In Jupyter notebooks, you can show plots directly in the notebook by typing:
python
%matplotlib inline

This makes the plot appear right after the code in the notebook.
- Standalone Plots:
If you are using a normal Python script (not in Jupyter), the plot will appear
in
a new window when you use plt.show().

Chapter 2: Basic Plotting with Matplotlib


- Plotting Simple Graphs:
- Line plots, bar plots, scatter plots, and histograms.
- Plot titles and axis labels.
- Legends and grids.
- Customizing colors, markers, and line styles.
- Multiple Plots:
- Creating subplots with subplot() and subplots().
- Sharing axes and customizing plot layouts.

1. Plotting Simple Graphs


- Line Plots:
A line plot shows data points connected by lines. It's useful to visualize trends
over time.
- Bar Plots:
A bar plot uses bars to represent data. It's good for comparing different
categories.
- Scatter Plots:
A scatter plot shows individual data points on the graph. Each point represents
one value
on the X-axis and one on the Y-axis.
- Histograms:
A histogram groups data into ranges (bins) and shows how often each range
appears. It's great
for understanding data distribution.

2. Plot Titles and Axis Labels


- Plot Titles:
You can give your plot a title using plt.title("Title") to explain what the plot
is about.
- Axis Labels:
Use plt.xlabel("X-axis label") and plt.ylabel("Y-axis label") to label the X and
Y axes.

3. Legends and Grids


- Legends:
A legend helps you know what each line, bar, or point represents. You add legends
using plt.legend().
- Grids:
Grids are horizontal and vertical lines that make it easier to see the data
points. You can turn them on with plt.grid(True).
4. Customizing Colors, Markers, and Line Styles
- Colors:
You can change the color of lines or bars by passing a color name or code like
color='red'.
- Markers:
Markers show individual data points (e.g., circles, squares) on line plots. Use
marker='o' for circles.
- Line Styles:
You can customize how lines look. For example, linestyle='--' makes a dashed
line.

5. Multiple Plots
- Subplots:
You can put multiple plots in the same figure using subplot(). This is useful
for comparing different data side by side.
- plt.subplot(2, 1, 1) means 2 rows, 1 column, first plot.
- Sharing Axes:
You can make multiple plots share the same X-axis or Y-axis using plt.subplots()
with the option sharex=True or sharey=True.
- Customizing Layouts:
You can adjust the layout of multiple plots with plt.tight_layout() so they don't
overlap.

1. Simple Line Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

2. Simple Bar Plot


python
x = ['A', 'B', 'C', 'D']
y = [5, 7, 8, 6]

plt.bar(x, y, color='blue')
plt.title('Simple Bar Plot')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.show()

3. Simple Scatter Plot


python
x = [1, 2, 3, 4]
y = [2, 3, 5, 7]

plt.scatter(x, y, color='green', marker='o')


plt.title('Simple Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

4. Simple Histogram
python
data = [22, 30, 35, 36, 40, 42, 42, 45, 48, 49, 50, 52]

plt.hist(data, bins=5, color='purple')


plt.title('Simple Histogram')
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.show()

5. Customizing Line Plot with Color and Marker


python
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y, color='red', marker='o', linestyle='--')


plt.title('Line Plot with Color and Marker')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

6. Bar Plot with Grid


python
x = ['Apples', 'Bananas', 'Cherries']
y = [12, 30, 18]

plt.bar(x, y, color='orange')
plt.title('Bar Plot with Grid')
plt.xlabel('Fruits')
plt.ylabel('Quantity')
plt.grid(True)
plt.show()

7. Adding Legend to a Line Plot


python
x = [1, 2, 3, 4]
y1 = [1, 4, 9, 16]
y2 = [2, 4, 6, 8]

plt.plot(x, y1, label='Squared', color='blue')


plt.plot(x, y2, label='Doubled', color='red')
plt.title('Line Plot with Legend')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend() Adding legend
plt.show()

You might also like