Login
📚 Introduction to Python Programming
Chapters ▾
⇩ Download ▾

15.5 Data visualization

Learning objectives

By the end of this section you should be able to

  • Explain why visualization has an important role in data science.
  • Choose appropriate visualization for a given task.
  • Use Python visualization libraries to create data visualization.

Why visualization?

Data visualization has a crucial role in data science for understanding the data. Data visualization can be used in all steps of the data science life cycle to facilitate data exploration, identify anomalies, understand relationships and trends, and produce reports. Several visualization types are commonly used:

Table 15.7 Common visualization types.
Visualization typeDescriptionBenefits/common usage
Bar plotRectangular barsCompare values across different categories.
Line plotA series of data points connected by line segmentsVisualize trends and changes.
Scatter plotIndividual data points representing the relationship between two variablesIdentify correlations, clusters, and outliers.
Histogram plotRectangular bars representing the distribution of a continuous variable by dividing the variable's range into bins and representing the frequency or count of data within each binSummarizing the distribution of the data.
Box plotRectangular box with whiskers that summarize the distribution of a continuous variable, including the median, quartiles, and outliersSummarizing the distribution of the data and comparing different variables.

Data visualization tools

Many Python data visualization libraries exist that offer a range of capabilities and features to create different plot types. Some of the most commonly used frameworks are Matplotlib, Plotly, and Seaborn. Here, some useful functionalities of Matplotlib are summarized.

Table 15.8 Matplotlib functionalities.
Plot typeMethod
Bar plotThe plt.bar(x, height) function takes in two inputs, x and height, and plots bars for each x value with the height given in the height variable.
ExampleOutput
import matplotlib.pyplot as plt # Data categories = ["Course A", "Course B", "Course C"] values = [25, 40, 30] # Create the bar chart fig = plt.bar(categories, values) # Customize the chart plt.title("Number of students in each course') plt.xlabel("Courses") plt.ylabel("Number of students") # Display the chart plt.show
Bar chart example
Table 15.9 Matplotlib functionalities.
Plot typeMethod
Line plotThe plt.plot(x, y) function takes in two inputs, x and y, and plots lines connecting pairs of (x, y) values.
ExampleOutput
import matplotlib.pyplot as plt # Data month = ["Jan", "Feb", "Mar", "Apr", "May"] inflation = [6.41, 6.04, 4.99, 4.93, 4.05] # Create the line chart plt.plot(month, inflation, marker="o", linestyle="-", color="blue") # Customize the chart plt.title("Inflation trend in 2023") plt.xlabel("Month") plt.ylabel("Inflation") # Display the chart plt.show
Line plot example
Table 15.10 Matplotlib functionalities.
Plot typeMethod
Scatter plotThe plt.scatter(x, y) function takes in two inputs, x and y, and plots points representing (x, y) pairs.
ExampleOutput
import matplotlib.pyplot as plt # Data x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y = [10, 8, 6, 4, 2, 5, 7, 9, 3, 1] # Create the scatter plot plt.scatter(x, y, marker="o", color="blue") # Customize the chart plt.title("Scatter Plot Example") plt.xlabel("X") plt.ylabel("Y") # Display the chart plt.show
Scatter plot example
Table 15.11 Matplotlib functionalities.
Plot typeMethod
Histogram plotThe plt.hist(x) function takes in one input, x, and plots a histogram of values in x to show distribution or trend.
ExampleOutput
import matplotlib.pyplot as plt import numpy as np # Data: random 1000 samples data = np.random.randn(1000) # Create the histogram plt.hist(data, bins=30, edgecolor="black") # Customize the chart plt.title("Histogram of random values") plt.xlabel("Values") plt.ylabel("Frequency") # Display the chart plt.show
Histogram plot example
Table 15.12 Matplotlib functionalities.
Plot typeMethod
Box plotThe plt.boxplot(x) function takes in one input, x, and represents minimum, maximum, first, second, and third quartiles, as well as outliers in x.
ExampleOutput
import matplotlib.pyplot as plt import numpy as np # Data: random 100 samples data = [np.random.normal(0, 5, 100)] # Create the box plot plt.boxplot(data) # Customize the chart plt.title("Box Plot of random values") plt.xlabel("Data Distribution") plt.ylabel("Values") # Display the chart plt.show
A box plot titled 'Box Plot of Random Values' illustrates data distribution. The median is near -1, the box covers values from -4 to 2, whiskers extend from -12 to 7, and two outliers are visible above 10.

Programming practice with Google

Use the Google Colaboratory document below to practice a visualization task on a given dataset.

Google Colaboratory document