0% found this document useful (0 votes)
6 views7 pages

Matplotlib Package

Uploaded by

prathapmaster7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
6 views7 pages

Matplotlib Package

Uploaded by

prathapmaster7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Matplotlib:

Data Visualization is one of the key skills required for data scientists. There are
many tools available for visualizing the data graphically. One such tool for
python developers is matplotlib.
Matplotlib is open-source cross-platform python library used for Data
visualization and Graphical plotting.

Installing Matplotlib:
For installing matplotlib in windows, press win+R and type cmd and click enter.
in the command prompt type, the following command:
pip install matplotlib

For Linux users, press ctrl+alt+T to open the Terminal. And type the following
command:
sudo apt-get install python3-matplotlib

Instead of setting up the environment it is recommended to use Jupyter


notebook for matplotlib.

import the matplotlib library using the following code.


import matplotlib.pyplot as plt(shortcut name)

1. Creating a simple plot using Matplotlib in Python


# plotting the data
import matplotlib.pyplot as plt
plt.plot([10, 20, 30, 40], [20, 30, 40, 50])
plt.show()
Output:
In above code, X-axis and Y-axis are given as arguments in plt.show(). Which
represents list datatype.

2. Creating a basic plot with Matplotlib in Python with axis labels


import matplotlib.pyplot as plt
# plotting
plt.plot([10, 20, 30, 40], [20, 30, 40, 50])
# A title for our plot
plt.title("Sample
Plot") # labels
plt.ylabel("y-axis")
plt.xlabel("x-axis")
plt.show()
Output:
3. Generating a bar plot in Python with the Data Visualization library
import matplotlib.pyplot as plt

# Dataset
courses = ['Python', 'Java', 'C++', 'C']
values = [24, 12, 6, 18]

# Create a bar plot


plt.bar(courses, values, color='orange')

# labels to the axes


plt.xlabel("Courses")
plt.ylabel("Number of students")

# title to the plot


plt.title("Enrollment")

# Displaying the plot


plt.show()

Output:
Scatter plot: Use scatter() method to do scatter plot
4. Generating a scatter plot with Matplotlib in Python
import matplotlib.pyplot as plt

# Dataset
x = [2, 3, 4, 6, 7, 9, 8, 9, 8, 12]
y = [34, 65, 34, 60, 83, 45, 67, 87, 78, 90]

# Creating a scatter plot with color specified using the color parameter
plt.scatter(x, y, color="blue")

# Adding labels and title


plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Scatter Plot")

# Display the plot


plt.show()

Output:
Pie chart: It is plotted using plt.pie().
A pie chart is a visual representation used to show the proportion of different
categories in a dataset. It divides a circle into slices, with each slice
representing a category and its size showing the proportion of that category
relative to the whole. Pie charts are useful for quickly understanding the
relative sizes of different categories in a dataset, making them popular for
displaying categorical data.

5. Creating a pie chart using Matplotlib


import matplotlib.pyplot as plt

# dataset
count = [35, 25, 25, 15]
lang = ["Python", "Java", "C++", "C"]

plt.pie(count, labels=lang, autopct='%1.1f%%') # autopct adds percentage


labels
plt.legend() # mark the
legends plt.show()
Output:

You might also like