Create Scatter, Line and Bar Charts using Matplotlib

To create scatter plots, line charts, and bar charts using Matplotlib:

Scatter plot

import matplotlib.pyplot as plt

x_axis = ["value_1", "value_2", "value_3", ...]
y_axis = ["value_1", "value_2", "value_3", ...]

plt.scatter(x_axis, y_axis)
plt.title("title name")
plt.xlabel("x_axis name")
plt.ylabel("y_axis name")
plt.show()

Line chart

import matplotlib.pyplot as plt

x_axis = ["value_1", "value_2", "value_3", ...]
y_axis = ["value_1", "value_2", "value_3", ...]

plt.plot(x_axis, y_axis)
plt.title("title name")
plt.xlabel("x_axis name")
plt.ylabel("y_axis name")
plt.show()

Bar chart

import matplotlib.pyplot as plt

x_axis = ["value_1", "value_2", "value_3", ...]
y_axis = ["value_1", "value_2", "value_3", ...]

plt.bar(x_axis, y_axis)
plt.title("title name")
plt.xlabel("x_axis name")
plt.ylabel("y_axis name")
plt.show()

How to Create a Scatter Plot using Matplotlib

Scatter plots are used to depict a relationship between two variables.

For example, suppose that you want to depict the relationship between:

  • The unemployment_rate; and
  • The index_price

Here is the dataset associated with those two variables:

unemployment_rateindex_price
6.11500
5.81520
5.71525
5.71523
5.81515
5.61540
5.51545
5.31560
5.21555
5.21565

To create the scatter diagram:

import matplotlib.pyplot as plt

unemployment_rate = [6.1, 5.8, 5.7, 5.7, 5.8, 5.6, 5.5, 5.3, 5.2, 5.2]
index_price = [1500, 1520, 1525, 1523, 1515, 1540, 1545, 1560, 1555, 1565]

plt.scatter(unemployment_rate, index_price)
plt.title("Unemployment Rate vs Index Price")
plt.xlabel("Unemployment Rate")
plt.ylabel("Index Price")
plt.grid(True)
plt.show()

Once you run the Python code, you’ll get the scatter plot.

How to Create a Line Chart using Matplotlib

Line charts are often used to display trends overtime.

For example, imagine that you have the following dataset:

yearunemployment_rate
19209.8
193012
19408
19507.2
19606.9
19707
19806.5
19906.2
20005.5
20106.3

To create the line chart:

import matplotlib.pyplot as plt

year = [1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010]
unemployment_rate = [9.8, 12, 8, 7.2, 6.9, 7, 6.5, 6.2, 5.5, 6.3]

plt.plot(year, unemployment_rate)
plt.title("Unemployment Rate vs Year")
plt.xlabel("Year")
plt.ylabel("Unemployment Rate")
plt.show()

How to Create a Bar Chart using Matplotlib

Bar charts are used to display categorical data.

Here is an example of a dataset:

countrygdp_per_capita
A45000
B42000
C52000
D49000
E47000

To create the bar chart:

import matplotlib.pyplot as plt

country = ["A", "B", "C", "D", "E"]
gdp_per_capita = [45000, 42000, 52000, 49000, 47000]

plt.bar(country, gdp_per_capita)
plt.title("Country Vs GDP Per Capita")
plt.xlabel("Country")
plt.ylabel("GDP Per Capita")
plt.show()

Run the code, and you’ll get the bar chart.

You can find additional information about the Matplotlib module by reviewing the Matplotlib documentation.

2 thoughts on “Create Scatter, Line and Bar Charts using Matplotlib”

  1. this tutorial was very helpful

    Reply

Leave a Comment