Computer Science
Grade 5
20 min
6. Interactive Visualizations: Using Plotly and Bokeh
Learn how to create interactive visualizations using libraries like Plotly and Bokeh, allowing users to explore data in more detail.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Explain what makes a visualization 'interactive'.
Identify the purpose of the Plotly and Bokeh libraries.
Create a simple interactive bar chart using Plotly Express.
Create a simple interactive line chart using Bokeh.
Add titles and labels to their charts.
Use a hover tool to inspect data points on their charts.
Have you ever seen a map or chart online that changes when you move your mouse over it? 🗺️✨ Let's learn how to build those magical, moving pictures with code!
In this lesson, we will explore two special toolkits, Plotly and Bokeh, that help us turn boring data into exciting, interactive charts. You'll learn how to make graphs that you can zoom into, get more information from, and bring your data stories to life.
Real-World A...
2
Key Concepts & Vocabulary
TermDefinitionExample
Interactive VisualizationA chart or graph that you can interact with. Instead of just looking at it, you can click, zoom, or hover your mouse to see more details.A map of the USA where hovering over a state shows its population.
Library (or Toolkit)A collection of pre-written code that helps you do cool things without having to write everything from scratch. It's like having a box of special Lego pieces for a specific project.Plotly and Bokeh are libraries specifically for making charts and graphs.
PlotlyA popular library for making beautiful, web-ready interactive charts with very few lines of code.Using `plotly.express` to quickly make a bar chart of favorite ice cream flavors.
BokehAnother powerful library for creating interactive visualizations, especially u...
3
Core Syntax & Patterns
Plotly Express Bar Chart Pattern
import plotly.express as px
fig = px.bar(x=[...], y=[...])
fig.show()
This is the fastest way to make a bar chart with Plotly. You 'import' the library, then use the `px.bar()` function. You give it your data for the x-axis (the labels at the bottom) and the y-axis (the numbers for the bar heights), and then `.show()` displays it.
Bokeh Line Chart Pattern
from bokeh.plotting import figure, show
p = figure()
p.line(x=[...], y=[...])
show(p)
This is a basic pattern for a Bokeh line chart. First, you import `figure` and `show`. You create a blank figure (like a canvas), then you tell it to draw a line using your x and y data points. Finally, `show(p)` displays your finished chart.
Adding Titles and Labels
fig.update_layout(title_...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You want to show the path of a robot on a grid. A loop runs 10 times, and in each step, the robot's (x, y) position changes. To show the robot's entire path using a line chart, what must your program do inside the loop?
A.Only save the robot's very last position.
B.Draw a new, separate chart for every step.
C.Take a screenshot at every step.
D.Add the robot's new (x, y) position to two lists (one for x, one for y) at the end of each loop.
Challenging
A program uses a binary system to decide if a city on a map should be a large circle or a small dot. It has a list of populations and a loop. Inside the loop, what `if` statement could be used to set a binary 'is_large_city' variable to 1 (true) or 0 (false)?
A.`if population > 1000000: is_large_city = 1 else: is_large_city = 0`
B.`if city_name == 'New York': is_large_city = 1`
C.`is_large_city = population / 1000000`
D.`is_large_city = 1` for all cities.
Challenging
An interactive chart has two sliders. Slider A, named 'count', controls the number of dots (1-50). Slider B, named 'size', controls the size of the dots (5-20). If you set 'count' to 10 and 'size' to 18, what happens, and what two variables in the code are being updated?
A.You see 18 dots, each with a size of 10. The variables are `num_dots` and `dot_color`.
B.You see 10 dots, each with a size of 18. The variables are likely `num_dots` and `dot_size`.
C.You see 28 dots (10 + 18). The variables are `x_position` and `y_position`.
D.The chart shows one dot at position (10, 18). The variables are `x` and `y`.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free