Lesson

Hover effects

Learn Hover effects in SQLPad's Data Science in Action: Interactive Visualization with Plotly and Pandas course with practical examples and guided lessons.

In this lesson, we will learn about hover effects in Plotly. Hover effects are interactive features that display additional information when the user hovers over a data point on the graph. This can be helpful in providing context and insights without cluttering the chart.

Basic Hover Effects

By default, Plotly provides basic hover effects for data points in a chart. Let's create a simple scatter plot using the built-in iris dataset from Plotly.

import plotly.express as px

# Load the iris dataset
data = px.data.iris()

# Create a scatter plot
fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species')

# Show the plot
fig.show()

When you hover over a data point, you can see the x and y coordinates along with the species name. This basic hover effect can be customized to display more information or to change the format of the hover label.

Customizing Hover Labels

To customize the hover labels, use the hover_data parameter in the plotting function. This parameter accepts a list of column names or a dictionary where the keys are the column names and the values are the format to be displayed.

Let's add the petal_width and petal_length columns to the hover data and format them as decimals.

# Create a scatter plot with custom hover data
fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species',
                 hover_data={'petal_width': ':.2f', 'petal_length': ':.2f'})

# Show the plot
fig.show()

Now, when you hover over a data point, the hover label includes the petal width and petal length formatted as decimals.

Customizing Hover Templates

Another way to customize hover effects is by using hover templates. Hover templates allow you to define a custom HTML template for the hover label. You can use variables surrounded by %{} to include dynamic data in the template.

Let's create a scatter plot with a custom hover template that includes the species name, sepal dimensions, and petal dimensions.

# Create a scatter plot with a custom hover template
fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species')

fig.update_traces(hovertemplate='<b>%{hovertext}</b><br>Sepal: %{x:.2f} x %{y:.2f}<br>Petal: %{customdata[0]:.2f} x %{customdata[1]:.2f}',
                  customdata=data[['petal_width', 'petal_length']].values,
                  hovertext=data['species'])

# Show the plot
fig.show()

The custom hover template displays the species name in bold, followed by the sepal dimensions and petal dimensions in separate lines.

Hover Modes

Plotly provides two hover modes: 'x' and 'y'. The 'x' hover mode displays hover labels for all data points with the same x value, while the 'y' hover mode displays hover labels for all data points with the same y value. To change the hover mode, update the layout's hovermode attribute.

Let's create a scatter plot with the 'x' hover mode.

# Create a scatter plot with x hover mode
fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species')

fig.update_layout(hovermode='x')

# Show the plot
fig.show()

Now, when you hover over a data point, the hover labels for all data points with the same x value are displayed.

In this lesson, we have learned about hover effects in Plotly and how to customize them using hover data, hover templates, and hover modes. These interactive features can help you create more insightful and engaging visualizations.

Exercises

1. Hover Effects in Plotly

Instruction

In this exercise, you will create a scatter plot using the iris dataset and customize the hover effects using hover data, hover templates, and hover modes.

  1. Import the plotly.express module as px.
  2. Load the iris dataset using px.data.iris() and store it in a variable called data.
  3. Create a scatter plot using px.scatter() with sepal_width on the x-axis, sepal_length on the y-axis, and species as the color. Add custom hover data for petal_width and petal_length formatted as decimals.
  4. Update the traces to use a custom hover template that includes the species name, sepal dimensions, and petal dimensions.
  5. Update the layout to use the 'x' hover mode.
  6. Show the plot using fig.show().

My Solution

# Your solution goes here

Hint

  1. Use import plotly.express as px to import the module.
  2. Use data = px.data.iris() to load the dataset.
  3. Use fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species', hover_data={'petal_width': ':.2f', 'petal_length': ':.2f'}) to create the scatter plot with custom hover data.
  4. Use fig.update_traces() to update the hover template and custom data.
  5. Use fig.update_layout(hovermode='x') to set the hover mode to 'x'.
  6. Use fig.show() to display the plot.

Solution

import plotly.express as px

data = px.data.iris()

fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species',
                 hover_data={'petal_width': ':.2f', 'petal_length': ':.2f'})

fig.update_traces(hovertemplate='<b>%{hovertext}</b><br>Sepal: %{x:.2f} x %{y:.2f}<br>Petal: %{customdata[0]:.2f} x %{customdata[1]:.2f}',
                  customdata=data[['petal_width', 'petal_length']].values,
                  hovertext=data['species'])

fig.update_layout(hovermode='x')

fig.show()