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
How to Hide Axis Titles in Plotly Express Figure with Facets in Python?
Plotly Express is a powerful data visualization library in Python that creates interactive plots with ease. When working with faceted plots (subplots), you may want to hide axis titles to create cleaner visualizations. This article explores different methods to hide axis titles in Plotly Express figures with facets.
Syntax
Here's the basic syntax for creating faceted plots in Plotly Express ?
import plotly.express as px
fig = px.scatter(data_frame, x="x_column", y="y_column",
facet_row="row_column", facet_col="col_column")
Sample Data Setup
Let's create sample data to demonstrate the techniques ?
import pandas as pd
import plotly.express as px
# Create sample dataset
data = {
'x_values': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'y_values': [10, 15, 12, 18, 14, 20, 16, 22, 18, 25, 21, 28],
'category': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'],
'region': ['North', 'North', 'South', 'South', 'North', 'North',
'South', 'South', 'North', 'North', 'South', 'South']
}
df = pd.DataFrame(data)
print(df.head())
x_values y_values category region 0 1 10 A North 1 2 15 B North 2 3 12 A South 3 4 18 B South 4 5 14 A North
Method 1: Using update_layout()
The simplest approach is to modify the figure's layout to hide axis titles ?
import pandas as pd
import plotly.express as px
# Create sample data
data = {
'x_values': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'y_values': [10, 15, 12, 18, 14, 20, 16, 22, 18, 25, 21, 28],
'category': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'],
'region': ['North', 'North', 'South', 'South', 'North', 'North',
'South', 'South', 'North', 'North', 'South', 'South']
}
df = pd.DataFrame(data)
# Create faceted scatter plot
fig = px.scatter(df, x="x_values", y="y_values",
facet_col="category", facet_row="region")
# Hide axis titles
fig.update_layout(
xaxis_title="",
yaxis_title=""
)
print("Axis titles hidden using update_layout method")
fig.show()
Axis titles hidden using update_layout method
Method 2: Iterating Through All Axes
For more control over individual subplot axes, iterate through all axis objects ?
import pandas as pd
import plotly.express as px
# Create sample data
data = {
'x_values': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'y_values': [10, 15, 12, 18, 14, 20, 16, 22, 18, 25, 21, 28],
'category': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'],
'region': ['North', 'North', 'South', 'South', 'North', 'North',
'South', 'South', 'North', 'North', 'South', 'South']
}
df = pd.DataFrame(data)
# Create faceted scatter plot
fig = px.scatter(df, x="x_values", y="y_values",
facet_col="category", facet_row="region")
# Hide axis titles for all subplots
for axis_name in fig.layout:
if axis_name.startswith("xaxis") or axis_name.startswith("yaxis"):
fig.layout[axis_name].title = ""
print("All subplot axis titles hidden")
fig.show()
All subplot axis titles hidden
Method 3: Using update_xaxes() and update_yaxes()
A more explicit approach using dedicated axis update methods ?
import pandas as pd
import plotly.express as px
# Create sample data
data = {
'x_values': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'y_values': [10, 15, 12, 18, 14, 20, 16, 22, 18, 25, 21, 28],
'category': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'],
'region': ['North', 'North', 'South', 'South', 'North', 'North',
'South', 'South', 'North', 'North', 'South', 'South']
}
df = pd.DataFrame(data)
# Create faceted scatter plot
fig = px.scatter(df, x="x_values", y="y_values",
facet_col="category", facet_row="region")
# Hide axis titles using update methods
fig.update_xaxes(title_text="")
fig.update_yaxes(title_text="")
print("Axis titles hidden using update_xaxes/update_yaxes methods")
fig.show()
Axis titles hidden using update_xaxes/update_yaxes methods
Comparison
| Method | Scope | Best For |
|---|---|---|
update_layout() |
Main axes only | Simple plots with basic faceting |
| Iterating through layout | All subplot axes | Complex faceted plots with many subplots |
update_xaxes()/update_yaxes() |
All matching axes | Clean, explicit axis modifications |
Conclusion
Use update_xaxes() and update_yaxes() with title_text="" for the cleanest approach to hide axis titles in faceted Plotly Express plots. For complex layouts, iterate through the layout object to target specific axes individually.
