Create Scatter, Line and Bar Charts using Matplotlib

Here is the syntax to create scatter, line 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()

Let’s now review the steps to create a scatter plot.

How to Create a Scatter Plot using Matplotlib

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

For example, let’s say 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_rate index_price
6.1 1500
5.8 1520
5.7 1525
5.7 1523
5.8 1515
5.6 1540
5.5 1545
5.3 1560
5.2 1555
5.2 1565

You can then capture the above data using lists:

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]

print(unemployment_rate)
print(index_price)

If you run the above code in Python, you’ll get the following lists:

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

Finally, apply the syntax below in order to plot 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.

Next, you’ll see how to create a line chart.

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:

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

You may capture the data as lists:

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]

print(year)
print(unemployment_rate)

The output:

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

And here is the complete code to depict 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()

Finally, you’ll see how to create a bar chart using matplotlib.

How to Create a Bar Chart using Matplotlib

Bar charts are used to display categorical data.

Here is an example of a dataset to be used for the bar chart:

country gdp_per_capita
A 45000
B 42000
C 52000
D 49000
E 47000

First, capture the date using lists:

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

print(country)
print(gdp_per_capita)

Here is the dataset:

['A', 'B', 'C', 'D', 'E']
[45000, 42000, 52000, 49000, 47000]

Finally, apply the code below 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.