Select Items with an 'IF' Statement

Learn how to use “if” statements to select items using the scatterplot example.

We'll cover the following

The ‘if’ statement

The Filtering lesson is a good way to adapt what you see on a graph, but so is a more familiar friend: the ‘if’ statement.

An if statement will act to carry out a task in a particular way, depending on a condition that you specify.

Here’s an example: what if we wanted to show our scatter plot as normal, but all those with a “close” value less than 400 should be colored red. Sound familiar? Yes, I know it’s similar to the example above with the subtle difference that it is leaving the circles above 400 in place (more on that to follow).

Starting with the simple scatter plot example, all we have to do is include the if statement in the block of code that draws the circles. Here’s the entire block with the additions highlighted:

Get hands-on with 1200+ tech skills courses.