Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Plotly – How to hide legend entries in a Plotly figure?
Plotly is a powerful open-source plotting library in Python that creates interactive web-based visualizations. Sometimes you may want to hide specific legend entries to reduce clutter or highlight only certain data series in your plots.
In this tutorial, we will demonstrate different methods to hide legend entries in Plotly figures using the showlegend parameter.
Method 1: Using showlegend Parameter
The most direct way to hide legend entries is by setting showlegend=False when creating traces ?
import plotly.graph_objects as go import plotly.offline as py # Create sample data x = [1, 2, 3, 4, 5] y1 = [1, 4, 2, 3, 5] y2 = [2, 3, 4, 5, 6] y3 = [3, 2, 5, 4, 1] # Create traces with showlegend parameter trace1 = go.Scatter(x=x, y=y1, name='Series 1', showlegend=True) trace2 = go.Scatter(x=x, y=y2, name='Series 2', showlegend=False) # Hidden trace3 = go.Scatter(x=x, y=y3, name='Series 3', showlegend=True) fig = go.Figure(data=[trace1, trace2, trace3]) fig.show()
Method 2: Hiding Legend After Figure Creation
You can also hide legend entries by modifying the figure data after creation ?
import pandas as pd
import plotly.express as px
# Create sample DataFrame
data = {
'x': [1, 2, 3, 4, 5],
'series_1': [1, 4, 2, 3, 5],
'series_2': [2, 3, 4, 5, 6],
'series_3': [3, 2, 5, 4, 1]
}
df = pd.DataFrame(data)
# Create scatter plot
fig = px.scatter(df, x='x', y=['series_1', 'series_2', 'series_3'])
# Hide specific legend entries
for trace in fig.data:
if trace.name in ['series_1', 'series_3']:
trace.showlegend = False
fig.show()
Method 3: Using Plotly Graph Objects with Conditional Logic
For more complex scenarios, you can use conditional logic to selectively hide legends ?
import plotly.graph_objects as go
import pandas as pd
# Create dataset
data = {
'one': [1, 2, 3, 4, 5],
'two': [5, 6, 7, 8, 9],
'three': [3, 4, 5, 6, 7]
}
df = pd.DataFrame(data)
# Create figure
fig = go.Figure()
# Add traces with conditional legend display
for column in df.columns:
show_legend = column == 'two' # Only show legend for 'two'
fig.add_trace(go.Scatter(
x=df.index,
y=df[column],
name=column,
showlegend=show_legend
))
fig.update_layout(title="Scatter Plot with Selective Legend")
fig.show()
Method 4: Hiding All Legends
To hide the entire legend from the figure ?
import plotly.graph_objects as go # Create sample data x = [1, 2, 3, 4, 5] y = [2, 4, 3, 5, 1] fig = go.Figure() fig.add_trace(go.Scatter(x=x, y=y, name='Data Series')) # Hide entire legend fig.update_layout(showlegend=False) fig.show()
Key Parameters
| Parameter | Level | Description |
|---|---|---|
showlegend=False |
Trace | Hide legend for specific trace |
showlegend=False |
Layout | Hide entire legend |
trace.showlegend |
Post-creation | Modify legend visibility after creation |
Conclusion
Use showlegend=False at the trace level to hide specific legend entries, or at the layout level to hide the entire legend. You can also modify legend visibility after figure creation by accessing the showlegend property of individual traces.
