Trusted answers to developer questions

What is matplotlib?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

The matplotlib Python library, developed by John Hunter and many other contributors, is used to create high-quality graphs, charts, and figures. The library is extensive and capable of changing very minute details of a figure. Some basic concepts and functions provided in matplotlib are:

1. Figure and axes

The entire illustration is called a figure and each plot on it is an axes (do not confuse Axes with Axis). The figure can be thought of as a canvas on which several plots can be drawn. We obtain the figure and the axes using the subplots() function:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

2. Plotting

The very first thing required to plot a graph is data. A dictionary of key-value pairs can be declared, with keys and values as the x and y values. After that, ​scatter(), bar(), and pie(), along with tons of other functions, can be used to create the plot:

# Create data:
data = {
"France": 65.4,
"Germany": 82.4,
"Italy": 59.2,
"UK": 66.9
}
# Use keys and values as x and y axis values:
x_axis_data = data.keys()
y_axis_data = data.values()
# Plotting a bar graph:
ax.bar(x_axis_data, y_axis_data)

3. Axis

The figure and axes obtained ​using subplots() can be used for modification. Properties of the x-axis and y-axis (labels, minimum and maximum values, etc.) can be changed using Axes.set():

# Setting properties of axes:
ax.set(ylim=[0, 100],
ylabel='Population (in million)',
xlabel='Country',
title='European Countries by Population')

See the properties of a graph in the illustration provided below:

Some properties of a plot

Lastly, pyplot.show() is used to display the graph.

Code

The following code demonstrates all of the points discussed above:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# Create data:
data = {
"France": 65.4,
"Germany": 82.4,
"Italy": 59.2,
"UK": 66.9
}
# Use keys and values as x and y axis values:
x_axis_data = data.keys()
y_axis_data = data.values()
# Plotting a bar graph:
ax.bar(x_axis_data, y_axis_data)
# Setting properties of axes:
ax.set(ylim=[0, 100],
ylabel='Population (in million)',
xlabel='Country',
title='European Countries by Population')
# Displaying the graph:
plt.show()

Explanation

  • Line 1: We import the matplotlib.pyplot module as plt.

  • Line 3: This creates a figure (fig) and an axes (ax) using the subplots() function from matplotlib. The figure is the entire window or page where the plot will be drawn, and the axes are the actual plots within that figure.

  • Line 6-11: We define a dictionary called data where keys are countries and values are corresponding population values in million.

  • Line 14-15: We assign the keys of the data dictionary (countries) to x_axis_data and assign the values of the data dictionary (population) to y_axis_data .

  • Line 18: We plot a bar graph using the bar method of the axes (ax). It uses the country names (x_axis_data) on the x-axis and their corresponding population values (y_axis_data) on the y-axis.

  • Line 21-24: We set various properties of the axes, including the y-axis limits (ylim), y-axis label (ylabel), x-axis label (xlabel), and the title of the plot (title).

  • Line 27: We plot the graph using plt.show().

RELATED TAGS

matplotlib
python
graph
figure
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?