Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

data science 5

Rating
-
Sold
-
Pages
22
Uploaded on
17-02-2025
Written in
2024/2025

Learn about data science and data analysis 5

Institution
Course

Content preview

UNIT V DATA VISUALIZATION
5.1 Importing Matplotlib

 matplotlib.pyplot is a collection of command style functions that make matplotlib work like
MATLAB.
 Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting
area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.
 In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of
things like the current figure and plotting area, and the plotting functions are directed to the
current axes

import matplotlib.pyplot as plt

 This line of code imports Matplotlib's pyplot module and aliases it as plt, making it easier to
reference its functions and methods.
 After importing, you can use plt to create plots, set labels, plot data, adjust visual properties,
and more.
5.2 Line plots
 Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of matplotlib, is a
collection of functions that helps in creating a variety of charts.
 Line charts are used to represent the relation between two data X and Y on a different axis.
 Creating a simple line plot using Matplotlib in Python involves specifying data points and using
the plt.plot() function.
 The simplest of all plots is the visualization of a single function y=f(x)

Line Colors and Styles:
 set the color of the line by using the color parameter.
 Matplotlib supports various color representations like named colors, hexadecimal codes,
RGB tuples, etc.
 define line styles using the linestyle parameter. Matplotlib offers different line styles such as
solid lines, dashed lines, dash-dot lines, etc.

plt.plot(x, y, color='red', linestyle=':') # Red color, dotted line
Axes Limits:
 set the limits for the axes in a Matplotlib plot using plt.xlim() and plt.ylim() for the x-axis and y-
axis, respectively.
 These functions allow you to specify the range of values displayed on each axis.

Example:
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

,# Plotting the line
plt.plot(x, y)
# Setting limits for the axes
plt.xlim(0, 6) # Set x-axis limits from 0 to 6
plt.ylim(0, 12) # Set y-axis limits from 0 to 12
# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')
plt.plot(x, y, color='red', linestyle=':') # Red color, dotted line
# Display the plot
plt.show()

 create a line plot with x values [1, 2, 3, 4, 5] and corresponding y values [2, 4, 6, 8, 10].
 Matplotlib's plt.plot() function takes the x and y values as arguments and generates a line plot.
 In this example, plt.xlim() and plt.ylim() define the limits for the x-axis and y-axis, respectively.
 The limits are set to display values between 0 and 6 for the x-axis and between 0 and 12 for the
y-axis.
 Adjust the x and y data to visualize different datasets or customize the appearance of the plot
using additional functions provided by Matplotlib.

Output:




Example 2:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x));
plt.plot(x, np.sin(x - 0), color='blue') # specify color by name
plt.plot(x, np.sin(x - 1), color='g') # short color code (rgbcmyk)
plt.plot(x, np.sin(x - 2), color='0.75') # Grayscale between 0 and 1
plt.plot(x, np.sin(x - 3), color='#FFDD44') # Hex code (RRGGBB from 00 to FF)
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB tuple, values 0 and 1
plt.plot(x, np.sin(x - 5), color='chartreuse');# all HTML color names supported
plt.show()

, Output:




5.3 Scatter plots:
 A scatter plot is a visual representation of how two variables relate to each other. You can use
scatter plots to explore the relationship between two variables
 Creating a scatter plot using Matplotlib involves using the plt.scatter() function.
 A scatter plot is a diagram where each value in the data set is represented by a dot.
 The Matplotlib module has a method for drawing scatter plots, it needs two arrays of the same
length, one for the values of the x-axis, and one for the values of the y-axis
 The x array represents the age of each car. The y array represents the speed of each car.
 Use the scatter() method to draw a scatter plot diagram:
Example:
import matplotlib.pyplot as plt
# Sample data for the scatter plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating a scatter plot
plt.scatter(x, y)
# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Scatter Plot')
# Displaying the plot
plt.show()
Output:

Written for

Institution
Course

Document information

Uploaded on
February 17, 2025
Number of pages
22
Written in
2024/2025
Type
Class notes
Professor(s)
Ambika
Contains
All classes

Subjects

$4.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
asseesahamed

Also available in package deal

Get to know the seller

Seller avatar
asseesahamed Kamala niketan
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions