Department of Computer Science and Engineering
Subject: Data science and visualization (21CS644)
Module-5 : A Deep Dive into Matplotlib – Exercise programs
Question 1: Write python code to Visualize Water Usage using Pie Chart and explain
various attributes. (use your own sample data)
Question 2: Write python code to Visualize Movie Comparison using a Bar Plot and explain
various attributes. (use your own sample data)
Question 3: Write python code to Visualize Restaurant Performance using Stacked Bar Plot
and explain various attributes. (use your own sample data)
Question 4: Write python code for Comparing Smartphone Sales Units Using a Stacked Area
Chart and explain various attributes. (use your own sample data)
Question 5: Write python code to Visualize Intelligence Quotient Units using a Histogram
and a Box Plot and explain various attributes. (use your own sample data)
Question 6: Write python code to Visualize Correlation between Various Animals Using a
Scatter Plot and explain various attributes. (use your own sample data)
Matplotlib Simple Line Plot
In this example, a simple line chart is generated using NumPy to define data values. The x-values are evenly
spaced points, and the y-values are calculated as twice the corresponding x-values.
# importing the required libraries
import matplotlib.pyplot as plt
import numpy as np
# define data values
x = np.array([1, 2, 3, 4]) # X-axis points
y = x*2 # Y-axis points
plt.plot(x, y) # Plot the chart
plt.show() # display
We can see in the above output image that there is no label on the x-axis and y-axis. Since labeling is
necessary for understanding the chart dimensions. In the following example, we will see how to add labels,
Ident in the charts.
import matplotlib.pyplot as plt
import numpy as np
# Define X and Y variable data
x = np.array([1, 2, 3, 4])
y = x*2
plt.plot(x, y)
plt.xlabel("X-axis") # add X-axis label
plt.ylabel("Y-axis") # add Y-axis label
plt.title("Any suitable title") # add title
plt.show()
Line Chart with Annotations
In this example, a line chart is created using sample data points. Annotations displaying the x and y
coordinates are added to each data point on the line chart for enhanced clarity.
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a line chart
plt.figure(figsize=(8, 6))
plt.plot(x, y, marker='o', linestyle='-')
# Add annotations
for i, (xi, yi) in enumerate(zip(x, y)):
plt.annotate(f'({xi}, {yi})', (xi, yi), textcoords="offset
points", xytext=(0, 10), ha='center')
# Add title and labels
plt.title('Line Chart with Annotations')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
# Display grid
plt.grid(True)
# Show the plot
plt.show()
Multiple Line Charts Using Matplotlib
We can display more than one chart in the same container by using pyplot.figure() function. This
will help us in comparing the different charts and also control the look and feel of charts.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4])
y = x*2
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Any suitable title")
plt.show() # show first chart
# The figure() function helps in creating a
# new figure that can hold a new chart in it.
plt.figure()
x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]
plt.plot(x1, y1, '-.')
# Show another chart with '-' dotted line
plt.show()
Multiple Plots on the Same Axis
Here, we will see how to add 2 plots within the same axis.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4])
y = x*2
# first plot with X and Y data
plt.plot(x, y)
x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]
# second plot with x1 and y1 data
plt.plot(x1, y1, '-.')
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.show()
Fill the Area Between Two Lines
Using the pyplot.fill_between() function we can fill in the region between two line plots in the
same graph. This will help us in understanding the margin of data between two line plots based on
certain conditions.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4])
y = x*2
plt.plot(x, y)
x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]
plt.plot(x, y1, '-.')
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.fill_between(x, y, y1, color='green', alpha=0.5)
plt.show()
Bar Plot in Matplotlib
Creating a bar plot
The syntax of the bar() function to be used with the axes is as follows:-
plt.bar(x, height, width, bottom, align)
Following is a simple example of the bar plot, which represents the number of students
enrolled in different courses of an institute.
import numpy as np
import matplotlib.pyplot as plt
# creating the dataset
data = {'C':20, 'C++':15, 'Java':30,
'Python':35}
courses = list(data.keys())
values = list(data.values())
fig = plt.figure(figsize = (10, 5))
# creating the bar plot
plt.bar(courses, values, color ='maroon',
width = 0.4)
plt.xlabel("Courses offered")
plt.ylabel("No. of students enrolled")
plt.title("Students enrolled in different courses")
plt.show()
Multiple bar plots
Multiple bar plots are used when comparison among the data set is to be done when one variable
is changing. We can easily convert it as a stacked area bar chart, where each subgroup is displayed
by one on top of the others. It can be plotted by varying the thickness and position of the bars.
Following bar plot shows the number of students passed in the engineering branch:
import numpy as np
import matplotlib.pyplot as plt
# set width of bar
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
# set height of bar
IT = [12, 30, 1, 8, 22]
ECE = [28, 6, 16, 5, 10]
CSE = [29, 3, 24, 25, 17]
# Set position of bar on X axis
br1 = np.arange(len(IT))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
# Make the plot
plt.bar(br1, IT, color ='r', width = barWidth,
edgecolor ='grey', label ='IT')
plt.bar(br2, ECE, color ='g', width = barWidth,
edgecolor ='grey', label ='ECE')
plt.bar(br3, CSE, color ='b', width = barWidth,
edgecolor ='grey', label ='CSE')
# Adding Xticks
plt.xlabel('Branch', fontweight ='bold', fontsize = 15)
plt.ylabel('Students passed', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(IT))],
['2015', '2016', '2017', '2018', '2019'])
plt.legend()
plt.show()
Stacked bar plot
Stacked bar plots represent different groups on top of one another. The height of the bar depends
on the resulting height of the combination of the results of the groups. It goes from the bottom
to the value instead of going from zero to value. The following bar plot represents the
contribution of boys and girls in the team.
import numpy as np
import matplotlib.pyplot as plt
N = 5
boys = (20, 35, 30, 35, 27)
girls = (25, 32, 34, 20, 25)
boyStd = (2, 3, 4, 1, 2)
girlStd = (3, 5, 2, 3, 3)
ind = np.arange(N)
width = 0.35
fig = plt.subplots(figsize =(10, 7))
p1 = plt.bar(ind, boys, width, yerr = boyStd)
p2 = plt.bar(ind, girls, width,
bottom = boys, yerr = girlStd)
plt.ylabel('Contribution')
plt.title('Contribution by the teams')
plt.xticks(ind, ('T1', 'T2', 'T3', 'T4', 'T5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('boys', 'girls'))
plt.show()