An interactive chart in Python Processing

Environment:
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.

Lesson 15 of 20 · Python and Processing

In the previous lesson we made a simple bar chart. Now we will improve it so that it reacts to the movement of the mouse.

When the user moves the mouse over a bar, the chart will show the exact value of that data.

Interactive charts are often used in modern web applications, dashboard systems and data analysis.

Example data

We will still store the data in a Python list.


data = [12, 25, 18, 30, 22, 15]

Each value represents the height of one bar on the chart.


The complete program


data = [12, 25, 18, 30, 22, 15]

def setup():
    size(600, 400)

def draw():
    background(240)

    bar_width = width / len(data)

    for i in range(len(data)):

        x = i * bar_width
        h = data[i] * 8
        y = height - h

        # check whether the mouse is over the bar
        if mouseX > x and mouseX < x + bar_width and mouseY > y:
            fill(255, 100, 100)
            rect(x, y, bar_width - 5, h)

            fill(0)
            textAlign(CENTER)
            text(data[i], x + bar_width/2, y - 10)
        else:
            fill(100, 180, 255)
            rect(x, y, bar_width - 5, h)

How does this program work?

  • the list data stores the values
  • each value becomes one bar on the chart
  • the program checks whether the mouse is over a particular bar
  • if it is, the bar changes colour and shows its value
Checking the position of the mouse lets us make interactive graphic elements.

Mini project: highlighting the largest value

We can improve the chart further so that the largest bar has a special colour.


max_value = max(data)

for i in range(len(data)):

    if data[i] == max_value:
        fill(255,150,0)
    else:
        fill(100,180,255)

    rect(i*bar_width, height - data[i]*8,
         bar_width - 5, data[i]*8)

What does this code do?

  • the max() function finds the largest value
  • that bar is drawn in a different colour
  • this makes the chart more informative

Where are interactive charts used?

  • analysing financial data
  • visualising surveys
  • sports statistics
  • dashboard applications
  • scientific research

Related lessons


Frequently asked questions

What is an interactive chart?

An interactive chart is a chart that reacts to the user's actions, such as moving the mouse or clicking.

Why are interactive charts useful?

They make it easier for users to explore data and get extra information about the values on the chart.

How does Processing know where the mouse is?

Processing uses the variables mouseX and mouseY, which hold the current position of the cursor.

Can we make more complex charts?

Yes. By combining lists, animation and interaction we can make very advanced data visualisations.


Check your knowledge

Open the quiz

Mini quiz: an interactive chart

1. How does the program know that the mouse is over a bar?