Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Adding Interactivity: Hover, Zoom, and Selection | Section
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Interactive Plotting with Plotly

bookAdding Interactivity: Hover, Zoom, and Selection

Interactive features are a core strength of Plotly charts, making data exploration more intuitive and engaging. With Plotly, you can add hover tooltips to reveal details about each point, zoom in to examine specific data regions, and select subsets of data directly on the chart. These interactive elements are especially useful when you need to explore complex datasets, identify trends, or share insights with others who can interact with your visualizations. By default, Plotly Express charts include many interactive capabilities, but you can further customize them to highlight the information most relevant to your analysis.

1234567891011121314151617181920212223242526272829
import plotly.express as px import pandas as pd from IPython.display import display, HTML # Sample data df = pd.DataFrame({ "city": ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"], "population": [8398748, 3990456, 2705994, 2325502, 1660272], "area": [783.8, 1214.9, 589.6, 1651.1, 1340.6] }) # Create scatter plot with custom hover data fig = px.scatter( df, x="area", y="population", text="city", hover_data={ "city": True, "population": ":,", "area": ":.1f" }, labels={"area": "City Area (sq km)", "population": "Population"}, title="City Population vs. Area" ) fig.update_traces(marker=dict(size=14, color='skyblue'), textposition="top center") html = fig.to_html(full_html=False, include_plotlyjs="cdn") display(HTML(html))
copy

In this example, you see how to create a scatter plot that displays custom information in the hover tooltip. The hover_data parameter allows you to specify exactly which columns appear in the tooltip and how they are formatted. Here, the city name, population (with thousands separators), and area (with one decimal place) are shown. You can also use the text parameter to display labels directly on the chart points, making it easier to identify each city at a glance. This level of customization helps you present the most relevant details to your audience without cluttering the chart.

1234567891011121314151617181920212223242526
import plotly.express as px import pandas as pd from IPython.display import display, HTML # Sample data df = pd.DataFrame({ "category": ["A", "B", "C", "D", "E"], "value1": [10, 15, 13, 17, 12], "value2": [23, 11, 18, 10, 15] }) # Create a scatter plot to demonstrate zoom and selection fig = px.scatter( df, x="value1", y="value2", color="category", title="Zoom and Selection Example" ) # By default, Plotly Express enables zoom and selection tools # You can configure the dragmode (e.g., 'zoom', 'select', 'lasso') as needed fig.update_layout(dragmode='select') # Try 'zoom', 'pan', or 'lasso' for different behaviors html = fig.to_html(full_html=False, include_plotlyjs="cdn") display(HTML(html))
copy

Interactivity transforms static charts into powerful data exploration tools. With Plotly Express, features like hover tooltips, zooming, and selection are enabled by default, letting you and your audience investigate data more deeply. Customizing hover information makes it easy to surface key details, while zoom and selection controls help you focus on specific patterns or outliers. These interactive features not only enhance your analysis but also make your visualizations more engaging and informative for others.

question mark

Which statement best describes the default interactive features of Plotly Express charts?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookAdding Interactivity: Hover, Zoom, and Selection

Swipe to show menu

Interactive features are a core strength of Plotly charts, making data exploration more intuitive and engaging. With Plotly, you can add hover tooltips to reveal details about each point, zoom in to examine specific data regions, and select subsets of data directly on the chart. These interactive elements are especially useful when you need to explore complex datasets, identify trends, or share insights with others who can interact with your visualizations. By default, Plotly Express charts include many interactive capabilities, but you can further customize them to highlight the information most relevant to your analysis.

1234567891011121314151617181920212223242526272829
import plotly.express as px import pandas as pd from IPython.display import display, HTML # Sample data df = pd.DataFrame({ "city": ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"], "population": [8398748, 3990456, 2705994, 2325502, 1660272], "area": [783.8, 1214.9, 589.6, 1651.1, 1340.6] }) # Create scatter plot with custom hover data fig = px.scatter( df, x="area", y="population", text="city", hover_data={ "city": True, "population": ":,", "area": ":.1f" }, labels={"area": "City Area (sq km)", "population": "Population"}, title="City Population vs. Area" ) fig.update_traces(marker=dict(size=14, color='skyblue'), textposition="top center") html = fig.to_html(full_html=False, include_plotlyjs="cdn") display(HTML(html))
copy

In this example, you see how to create a scatter plot that displays custom information in the hover tooltip. The hover_data parameter allows you to specify exactly which columns appear in the tooltip and how they are formatted. Here, the city name, population (with thousands separators), and area (with one decimal place) are shown. You can also use the text parameter to display labels directly on the chart points, making it easier to identify each city at a glance. This level of customization helps you present the most relevant details to your audience without cluttering the chart.

1234567891011121314151617181920212223242526
import plotly.express as px import pandas as pd from IPython.display import display, HTML # Sample data df = pd.DataFrame({ "category": ["A", "B", "C", "D", "E"], "value1": [10, 15, 13, 17, 12], "value2": [23, 11, 18, 10, 15] }) # Create a scatter plot to demonstrate zoom and selection fig = px.scatter( df, x="value1", y="value2", color="category", title="Zoom and Selection Example" ) # By default, Plotly Express enables zoom and selection tools # You can configure the dragmode (e.g., 'zoom', 'select', 'lasso') as needed fig.update_layout(dragmode='select') # Try 'zoom', 'pan', or 'lasso' for different behaviors html = fig.to_html(full_html=False, include_plotlyjs="cdn") display(HTML(html))
copy

Interactivity transforms static charts into powerful data exploration tools. With Plotly Express, features like hover tooltips, zooming, and selection are enabled by default, letting you and your audience investigate data more deeply. Customizing hover information makes it easy to surface key details, while zoom and selection controls help you focus on specific patterns or outliers. These interactive features not only enhance your analysis but also make your visualizations more engaging and informative for others.

question mark

Which statement best describes the default interactive features of Plotly Express charts?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6
some-alt