Data visualization also makes it easier to detect patterns, and trends in the data, through which some kind of inference can be made.
Use Matplotlib to create basic plots, such as line plots, bar charts, histograms, and scatter plots.
Customize the appearance of plots by changing the title, labels, axes limits, line styles, and colors.
Appreciate the significance of plotting the data in the python programming using Google Colab
Introduction to Matplotlib - Visualization with Python
Importing Matplotlib
Simple single-line and multiple-line plots
Subplots in Matplotlib
Bar chart (Single and Multiple)
Pie charts
Scatter Chart
Contour Plot
Three-dimensional plotting
Introduction to Matplotlib : Visualization with Python
Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays. It can be used in Python and IPython shells, Jupyter Notebook and web application servers. Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It is one of the most popular Python libraries for data visualization and is used by scientists, engineers, and data scientists all over the world.
Matplotlib has a procedural interface named the Pylab, which is designed to resemble MATLAB, a proprietary programming language developed by MathWorks. Matplotlib along with NumPy can be considered as the open source equivalent of MATLAB. Matplotlib was originally written by John D. Hunter in 2003.
Matplotlib can be used to create a wide variety of plots such as,
Line plots
Scatter plots
Bar charts
Histograms
Pie charts
Heatmaps
3D plots
Matplotlib also provides features for customizing the appearance of plots with
Axis labels and titles
Legends
Gridlines
Markers and line styles
Colors and fonts
Matplotlib can be used to create plots that are both informative and visually appealing. It is a powerful tool for data visualization that can help you to communicate your findings to others in a clear and concise way.
To visualize data using Matplotlib, we need to import the library. We can import the library, by typing the following code at the beginning of your Python script:
import matplotlib.pyplot as plt
Once we have imported Matplotlib, we can start creating plots.
For example, to create a line plot using the following code:
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Line Plot")
plt.show()
This code will create a line plot with the X-axis labelled "X Axis" and the Y-axis labelled "Y-Axis". The title of the plot will be "Line Plot Example".
Line plots are one of the most common types of plots used for data visualization. They are used to show the relationship between two variables, typically an independent variable (x-axis) and a dependent variable (y-axis).
To create a line plot using Matplotlib plt.plot() function is used, this function takes two arguments: the x-axis values and the y-axis values.
The appearance of the line plot can be improvised using the additional arguments to the plt.plot() function, such as
to change the line color to red, you can use the color='r' argument.
to change the line style to a dashed line, you can use the linestyle='--' argument.
Key points to be noted for visualizing data:
Choose the right plot type for your data.
Use clear and concise labels and titles.
Use appropriate colours and line styles.
Use legends to identify different data series.
Customize the plot to meet your specific needs.
Linspace is a function in the NumPy library that is used to generate a sequence of evenly spaced numbers within a specified interval. The function takes three arguments: the start point of the interval, the end point of the interval, and the number of points to generate.
For example, the following code will generate a sequence of 10 evenly spaced numbers between 0 and 10:
import numpy as np
x = np.linspace(0, 10, 11)
print(x)
import matplotlib.pyplot as plt
import numpy as np
# Create the x-axis data
x = np.linspace(0, 10, 100)
# Create the y-axis data for the first line
y1 = np.sin(x)
# Create the y-axis data for the second line
y2 = np.cos(x)
# Plot the first line in blue
plt.plot(x, y1, color='b', label = "sin x")
# Plot the second line in red
plt.plot(x, y2, color='r', label = "cos x")
# Set the x-axis and y-axis labels
plt.xlabel('X')
plt.ylabel('Y')
# Set the title of the
plot plt.title('Multiple Line Plot')
# Show the plot
plt.show()
Subplots in Matplotlib are a way to create multiple plots within a single figure. This can be useful for comparing different data sets or for visualizing different aspects of the same data set. Subplots are a powerful tool for creating complex and informative data visualizations. By using subplots, you can create plots that compare different data sets, show different aspects of the same data set, and share axes.
To create subplots in Matplotlib, you can use the subplots() function. This function takes two arguments: the number of rows of subplots and the number of columns of subplots. The subplots() function returns a tuple of two objects: a figure object and an axes object. The figure object represents the entire figure, and the axes object represents each subplot.
import matplotlib.pyplot as plt
import numpy as np
# Create the x-axis data
x = np.linspace(1, 2, 101)
fig, axes = plt.subplots(2, 2)
axes[0, 0].plot(x,x, label='x',color='r')
axes[0, 0].legend()
axes[0, 1].plot(x,x**2, label='x^2',color='b')
axes[0, 1].legend()
axes[1, 0].plot(x,x**3, label='x^3',color='g')
axes[1, 0].legend()
axes[1, 1].plot(x,x**4, label='x^4',color='m')
axes[1, 1].legend()
plt.show()
The acceleration due to gravity, or g, decreases with increasing height above the Earth's surface, because the force of gravity is inversely proportional to the square of the distance from the center of the Earth. As we move higher away from the center of the Earth, the gravitational force decreases, and therefore the acceleration due to gravity also decreases.
import matplotlib.pyplot as plt
import numpy as np
g0 = 9.81 # Acceleration due to gravity at surface of earth in m/s^2
RE = 6371 # Radius of Earth in km
# Create the x-axis data
h = np.linspace(0, 500, 500) # Height above the surface of earth in km
# Create the y-axis data
gh = g0*(1 - 2*(h/RE))
# Ploting the variation of acceleration due to gravity with height
plt.plot(h, gh, color='b')
# Set the x-axis and y-axis labels
plt.xlabel('Altitude above the surface of earth in km')
plt.ylabel('Acceleration due to gravity in m/s^2')
# Set the title of the plot
plt.title('Acceleration due to gravity variation with altitude')
# Marking a point on a graph
h1 = [400]
gh1 = [8.575]
plt.plot(h1, gh1, marker="o", markersize=10, markeredgecolor="red", markerfacecolor="green")
plt.text(400, 8.6, 'Average Altitude of ISS', fontsize = 12)
# Show the plot
plt.show()
A bar plot or bar chart is a graph that represents the category of data with rectangular bars with lengths and heights that is proportional to the values which they represent. The bar plots can be plotted horizontally or vertically. A bar chart describes the comparisons between the discrete categories. One of the axis of the plot represents the specific categories being compared, while the other axis represents the measured values corresponding to those categories.
import matplotlib.pyplot as plt
import numpy as np
# Create the x-axis data
x = np.array(['AA', 'AB', 'BB', 'CC', 'CD', 'DD', 'FF', 'XX'])
# Create the height data
y1 = np.array([3, 6, 9, 10, 7, 6, 3, 3])
# Create the bar chart
plt.bar(x, y1)
# Set the labels and title of the plot
plt.ylabel('Number of Students')
plt.title('Grade Distribution Chart')
# Show the plot
plt.show()
Multiple Bar Chart is required for comparing several quantities and when changing one variable. Multiple bar chart can be plotted by playing with thickness and the position of the bars
import numpy as np
import matplotlib.pyplot as plt
# Creating the Data Sets for Plots
data = [[3, 6, 9, 10, 7, 6, 3, 3],
[2, 7, 8, 11, 8, 5, 4, 2],
[4, 5, 8, 12, 5, 8, 3, 3],[1, 9, 9, 12, 5, 6, 3, 3]]
# Based on Number of Data
X = np.arange(8)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(X + 0.00, data[0], color = 'b', width = 0.15)
ax.bar(X + 0.15, data[1], color = 'g', width = 0.15)
ax.bar(X + 0.30, data[2], color = 'r', width = 0.15)
ax.bar(X + 0.45, data[3], color = 'm', width = 0.15)
# Set the labels and title of the plot
ax.set_xticks([0.25,1.25,2.25,3.25,4.25,5.25,6.25,7.25])
ax.set_xticklabels(['AA','AB','BB','BC','CC','CD','DD','FF'])
ax.legend(labels=['2AEBS101','2AEBS102','2AEES103','2AEES104'])
ax.set_title('Grade Distribution Chart')
ax.set_ylabel('Number of Students',fontsize = 16)
plt.show()
Pie charts are often used in engineering to represent data that is expressed as a percentage of a whole. For example, a pie chart could be used to show the distribution of different types of materials used in a product, the percentage of time spent on different tasks in a project, or the breakdown of costs for a particular engineering project.
A Pie Chart can only display one series of data. Pie charts show the size of items (called wedge) in one data series, proportional to the sum of the items. The data points in a pie chart are shown as a percentage of the whole pie.
import matplotlib.pyplot as plt
# Data to plot
data = [150000, 60000, 90000, 90000, 90000, 120000]
# Labels for each slice
labels = ['Labour', 'Timber', 'Supervision', 'Steel','Bricks', 'Cement']
# Colors for each slice
colors = ['red', 'orange', 'yellow', 'green','blue','brown']
# Plot the pie chart
plt.pie(data, labels=labels, colors=colors, autopct='%1.1f%%')
# Add a title to the plot
plt.title('Cost of Construction of House')
# Show the plot
plt.show()
A scatter plot in Matplotlib is a type of plot that is used to visualize the relationship between two variables. It is created by plotting a point for each data point in the two variables. The size and color of the points can be used to represent additional information about the data, such as the density of the data or the value of a third variable.
To create a scatter plot in Matplotlib, you can use the scatter() function. This function takes four arguments:
x: An array of x-axis values.
y: An array of y-axis values.
s: The size of the markers. This can be a scalar or an array of the same size as x and y.
c: The color of the markers. This can be a single color or an array of colors of the same size as x and y.
import numpy as np
import matplotlib.pyplot as plt
# Load the aircraft data
aircraft_data = np.loadtxt('aircraft_data.csv', delimiter=',')
# Extract the altitude and fuel consumption data
MTOW = aircraft_data[:, 0]
AR = aircraft_data[:, 1]
# Create the scatter plot
plt.scatter(MTOW, AR)
# Set the plot title and labels
plt.title('Maximum Takeoff Weight v/s Aspect Ratio')
plt.xlabel('Maximum Takeoff Weight (MTOW) in kg')
plt.ylabel('Aspect Ratio')
# Show the plot
plt.show()
Contour plots (sometimes called Level Plots) are a way to show a three-dimensional surface on a two-dimensional plane. It graphs two predictor variables X Y on the y-axis and a response variable Z as contours. These contours are sometimes called the z-slices or the iso-response values. A contour plot is appropriate if you want to see how value Z changes as a function of two inputs X and Y, such that Z = f(X,Y). A contour line or isoline of a function of two variables is a curve along which the function has a constant value.
The independent variables x and y are usually restricted to a regular grid called meshgrid. The numpy.meshgrid creates a rectangular grid out of an array of x values and an array of y values.
To create a contour plot in Python, you can use the matplotlib.pyplot.contour() function. This function takes three arguments:
X: A 2D array of the x-axis values.
Y: A 2D array of the y-axis values.
Z: A 2D array of the z-axis values.
import numpy as np
import matplotlib.pyplot as plt
# Define the parameters of the problem
T_c = 100 # Temperature at the center of the flat plate
T_0 = 15 # Temperature difference between the center of the flat plate and the ambient temperature
L = 1 # Characteristic length of the flat plate
# Create a array of x-axis values
xlist = np.linspace(0, L, 100)
# Create a array of y-axis values
ylist = np.linspace(0, L, 100)
# Create grid of x-axis and y-axis values
X, Y = np.meshgrid(xlist, ylist)
# Create a 2D array of z-axis values (heat distribution)
Z = T_c + T_0 * np.exp(-(X - 0.5)**2 - (Y - 0.5)**2)
# Plot the contour plot
plt.contourf(X, Y, Z, levels=20)
# Set the plot title and labels
plt.title('Heat Distribution in a Rectangular Plate')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.colorbar()
# Show the plot
plt.show()
Even though Matplotlib was initially designed with only two-dimensional plotting in mind, some three-dimensional plotting utilities were built on top of Matplotlib's two-dimensional display in later versions, to provide a set of tools for three-dimensional data visualization. Three-dimensional plots are enabled by importing the mplot3d toolkit, included with the Matplotlib package.
A three-dimensional axes can be created by passing the keyword projection='3d' to any of the normal axes creation routines.
Three-dimensional plotting in Matplotlib is supported by the mplot3d toolkit. To use this toolkit, you need to import it at the beginning of your code:
from mpl_toolkits import mplot3d
import numpy as np
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
# Define the parameters of the problem
T_c = 100 # Temperature at the center of the flat plate
T_0 = 15 # Temperature difference between the center of the flat plate and the ambient temperature
L = 1 # Characteristic length of the flat plate
# Create a array of x-axis values
xlist = np.linspace(0, L, 100)
# Create a array of y-axis values
ylist = np.linspace(0, L, 100)
# Create grid of x-axis and y-axis values
X, Y = np.meshgrid(xlist, ylist)
# Create a 2D array of z-axis values (heat distribution)
Z = T_c + T_0 * np.exp(-(X - 0.5)**2 - (Y - 0.5)**2)
# Plot the 3D surface
fig = plt.figure(figsize=(9, 9))
ax = fig.add_subplot(projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
# Set the plot title and labels
ax.set_title('Temperature Distribution in a Rectangular Plate')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
# Show the plot
plt.show()
Introduction to Matplotlib - Visualization with Python
Importing Matplotlib
Simple single-line and multiple-line plots
Subplots in Matplotlib
Bar chart (Single and Multiple)
Pie charts
Scatter Chart
Contour Plot
Three-dimensional plotting
Create a line plot of the function y = exp(x) for the range of x-values from 0 to 10.
Plot a suitable graph for visualizing the variation of hydrostatic pressure depth in the oceans from surface to 05 km deep.
A hydrogen balloon of volume 5 cu.m is ascending in the atmosphere, plot the graph to show the variation of buoyancy force experienced by the balloon from the sea level to 2000 m altitude. Also, plot a variation of buoyancy of the volume as the volume increases from 1 cu.m to 5 cu.m.
An electrical engineer is working on a new design for a solar panel. They need to compare the efficiency of different solar cell materials, and plot a bar chart to compare the efficiency of solar cell materials. (Silicon - 20 %, Gallium arsenide - 25 %, Perovskite - 30%)
Plot a bar chart to show the average tensile strength of the following materials Mild Steel, Medium Carbon Steel, 450 High Carbon Steel, 600 Alloy Steel.
Make a Scatter plot of the Mass of the bird and wingspan of different species of birds, and comment on your observation.