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

Matplotlib Guide With Code

The document is a guide to using Matplotlib for creating various types of plots, including line plots, bar plots, scatter plots, histograms, pie charts, and subplots. It provides code examples and explanations for each plot type, demonstrating how to visualize data effectively. Additionally, it introduces an object-oriented interface for more flexible plot creation.

Uploaded by

Aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Matplotlib Guide With Code

The document is a guide to using Matplotlib for creating various types of plots, including line plots, bar plots, scatter plots, histograms, pie charts, and subplots. It provides code examples and explanations for each plot type, demonstrating how to visualize data effectively. Additionally, it introduces an object-oriented interface for more flexible plot creation.

Uploaded by

Aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Matplotlib Guide with Code and Explanations

Basic Line Plot

This is a simple line plot showing y values against x.

# Basic Line Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

y = [10, 12, 5, 8, 7]

plt.plot(x, y)

plt.title('Simple Line Plot')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.grid(True)

plt.show()

Bar Plot

Bar plots are used to represent categorical data with rectangular bars.

# Bar Plot

x = ['A', 'B', 'C', 'D']

y = [10, 15, 7, 12]

plt.bar(x, y, color='skyblue')

plt.title('Bar Chart')

plt.show()

Scatter Plot

Scatter plots show the relationship between two variables.

# Scatter Plot

x = [1, 2, 3, 4, 5]

y = [2, 3, 5, 7, 11]

plt.scatter(x, y, color='red')

plt.title('Scatter Plot')
Matplotlib Guide with Code and Explanations

plt.show()

Histogram

Histograms are used to represent the distribution of a dataset.

# Histogram

import numpy as np

data = np.random.randn(1000)

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

plt.title('Histogram')

plt.show()

Pie Chart

Pie charts show percentages of a whole.

# Pie Chart

labels = ['A', 'B', 'C', 'D']

sizes = [25, 35, 20, 20]

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)

plt.axis('equal')

plt.title('Pie Chart')

plt.show()

Subplots

Use subplots to create multiple plots in a single figure.

# Subplots

fig, axs = plt.subplots(2, 2)

x = [1, 2, 3, 4, 5]

y = [10, 12, 5, 8, 7]

axs[0, 0].plot(x, y)

axs[0, 1].bar(x, y)

axs[1, 0].scatter(x, y)
Matplotlib Guide with Code and Explanations

axs[1, 1].hist(np.random.randn(100), bins=20)

plt.tight_layout()

plt.show()

Object-Oriented Interface

This is a more flexible way to build plots using Axes and Figure objects.

# OO Interface

fig, ax = plt.subplots()

ax.plot(x, y)

ax.set_title('Using OO Interface')

ax.set_xlabel('X')

ax.set_ylabel('Y')

plt.show()

You might also like