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_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 |
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:
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 |
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:
country | gdp_per_capita |
A | 45000 |
B | 42000 |
C | 52000 |
D | 49000 |
E | 47000 |
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.
this tutorial was very helpful
Thanks!