Drawing charts in Python Processing
the examples in this lesson are written for Processing Python Mode. If you work in the recommended Thonny + py5 environment, most of the code stays the same, and names with a capital letter in the middle are written in lower case with an underscore:
mouseX → mouse_x, noStroke() → no_stroke(). See the table of differences.In this lesson we learn how to use Python lists to draw charts in Processing. Data visualisation is very important in programming because it lets us show large amounts of information in a clear and easy-to-read way.
Charts are used in science, statistics, economics, the analysis of sports results and many other areas.
1. Data as a list
The simplest way to store data for a chart is to use a list.
values = [10, 25, 15, 30, 20]
Each element of the list represents one value we want to show.
2. A simple bar chart
Now we can draw a bar chart using a for loop.
values = [10, 25, 15, 30, 20]
def setup():
size(600, 400)
def draw():
background(240)
bar_width = width / len(values)
for i in range(len(values)):
x = i * bar_width
h = values[i] * 5
fill(100,150,255)
rect(x, height - h, bar_width - 5, h)
In this program:
- each element of the list becomes one bar
- the height of a bar depends on the value in the list
- the loop draws all the bars one after another
3. Adding labels
Charts are easier to read if we show the values above the bars.
textAlign(CENTER)
fill(0)
for i in range(len(values)):
x = i * bar_width + bar_width/2
text(values[i], x, height - values[i]*5 - 10)
Now each bar shows its numerical value.
4. Dynamic data (animation)
In Processing we can change the data while the program is running.
for i in range(len(values)):
values[i] = int(random(5,40))
If we put this code in the draw() function, the chart will keep changing.
Mini project: an interactive chart
Let us make a small project where a mouse click generates new data for the chart.
values = [10,20,30,15,25]
def setup():
size(600,400)
def draw():
background(240)
bar_width = width/len(values)
for i in range(len(values)):
h = values[i]*5
x = i*bar_width
fill(100,180,255)
rect(x,height-h,bar_width-5,h)
def mousePressed():
for i in range(len(values)):
values[i] = int(random(5,40))
How does this program work?
- the list values stores the data
- draw() draws the bar chart
- a mouse click generates new values
- the chart is updated straight away
Where are charts used?
- showing survey results
- analysing sports results
- visualising temperature
- statistics projects
- school projects in mathematics and computer science
Types of charts
There are several kinds of charts we can make in programming. In this lesson we made a bar chart, but there are other types too:
- Bar chart
Used to compare different values. Each value is represented by one bar. - Line chart
Used to show changes over time, for example the temperature during the day. - Pie chart
Used to show how data is divided into percentages. - Scatter chart
Used to show the relationship between two variables.
Related lessons
Frequently asked questions
What is data visualisation?
Data visualisation is presenting information with charts, diagrams and other visual elements.
Why use charts in programming?
Charts make it easier to understand large amounts of data and are often used in statistics, science and the analysis of results.
Can Processing make charts?
Yes. Processing can draw all kinds of graphic elements, so it can also be used for data visualisation.
What type of chart did we make in this lesson?
We made a bar chart, where each bar represents one value from the list.
Check your knowledge
Open the quiz
Mini quiz: charts
1. What decides the height of a bar in a bar chart?
