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:
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: