A Python project – Data Visualisation
In this project we will learn how to show data with charts. Data visualisation is very important because it makes information easier to understand.
We will use the matplotlib library to draw charts.
- lists
- working with data
- the Python library matplotlib
- the basics of visualisation
Installing the library
If you do not have matplotlib installed, run:
pip install matplotlib
Example data
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
temperature = [20, 22, 19, 24, 23]
This data represents the temperature on each day.
The program code (a line chart)
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
temperature = [20, 22, 19, 24, 23]
plt.plot(days, temperature)
plt.title("Temperature during the week")
plt.xlabel("Days")
plt.ylabel("Temperature (°C)")
plt.show()
How this code works
- plt.plot() draws the chart
- title() sets the title
- xlabel() / ylabel() label the axes
- show() shows the chart
A bar chart
plt.bar(days, temperature)
plt.title("Temperature by day")
plt.show()
A bar chart is excellent for comparing values.
A pie chart
subjects = ["Maths", "Computer science", "Physics", "Biology"]
votes = [8, 12, 4, 6]
plt.pie(votes, labels=subjects, autopct="%1.1f%%")
plt.title("Favourite subject in the class")
plt.show()
A pie chart shows percentages – how big a part of the whole (here: of all the votes) belongs to each value.
Possible improvements to the project
- load the data from a CSV file
- make interactive charts
- combine several charts
- make a dashboard (several charts together)
Related lessons
Frequently asked questions
What is matplotlib?
Matplotlib is a Python library for drawing charts and visualising data.
Which chart should I use?
A line chart for changes over time, a bar chart for comparison, a pie chart for percentages.
Is this used in practice?
Yes — it is used in data analysis, science, finance and machine learning.
Is this part of Data Science?
Yes — visualisation is one of the key parts of Data Science.
Next project
1) first run the given code without changes, 2) change one thing and predict what will happen, 3) add at least one feature of your own, 4) write down in a notebook what you changed and why. A work log like that is the basis of a creative programming paper.
