Open In App

How to hide axis titles in plotly express figure with facets in Python?

In this article, we will learn how to hide axis titles in a plotly express figure with facets in Python. 

We can hide the axis by setting the axis title as blank by iterating through for loop. We are hiding the axis only for X-axis and Y-axis so we have to compare this condition in each iteration



Syntax: 

for axis in fig.layout:
   if type(fig.layout[axis]) == go.layout.YAxis:
       fig.layout[axis].title.text = ''  
   if type(fig.layout[axis]) == go.layout.XAxis:
       fig.layout[axis].title.text = ''

Example 1:



Date vs Value data




import pandas as pd
import numpy as np
import plotly.express as px
import string
import plotly.graph_objects as go
 
# create a dataframe
cols = ['a', 'b', 'c', 'd', 'e']
n = 50
 
df = pd.DataFrame({'Date': pd.date_range('2021-1-1', periods=n)})
 
# create data with vastly different ranges
for col in cols:
    start = np.random.choice([1, 10, 100, 1000, 100000])
    s = np.random.normal(loc=0, scale=0.01*start, size=n)
    df[col] = start + s.cumsum()
 
# melt data columns from wide to long
dfm = df.melt("Date")
 
# make the plot
fig = px.line(
    data_frame=dfm,
    x='Date',
    y='value',
    facet_col='variable',
    facet_col_wrap=6,
 
    height=500,
    width=1000,
    title='Geeksforgeeks',
    labels={
        'Date': 'Date',
        'value': 'Value',
        'variable': 'Plot no.'
    }
)
 
# hide subplot y-axis titles and x-axis titles
for axis in fig.layout:
    if type(fig.layout[axis]) == go.layout.YAxis:
        fig.layout[axis].title.text = ''
    if type(fig.layout[axis]) == go.layout.XAxis:
        fig.layout[axis].title.text = ''
 
# ensure that each chart has its own y range and tick labels
fig.update_yaxes(matches=None, showticklabels=True, visible=True)
 
fig.show()

Output:

Example 2:

Temperature vs City data




import pandas as pd
import numpy as np
import plotly.express as px
import string
import plotly.graph_objects as go
 
# create a dataframe
cols = ['city-A', 'city-B', 'city-C', 'city-D']
n = 50
 
df = pd.DataFrame({'Date': pd.date_range('2021-6-1', periods=n)})
 
# create data with vastly different ranges
for col in cols:
    start = np.random.choice([1, 10, 100, 1000, 100000])
    s = np.random.normal(loc=0, scale=0.01*start, size=n)
    df[col] = start + s.cumsum()
 
# melt data columns from wide to long
dfm = df.melt("Date")
 
# make the plot
fig = px.line(
    data_frame=dfm,
    x='Date',
    y='value',
    facet_col='variable',
    facet_col_wrap=6,
 
    height=500,
    width=1300,
    title='Geeksforgeeks',
    labels={
        'Date': 'Date',
        'value': 'Value',
        'variable': 'CITY'
    }
)
 
# hide subplot y-axis titles and x-axis titles
for axis in fig.layout:
    if type(fig.layout[axis]) == go.layout.YAxis:
        fig.layout[axis].title.text = ''
    if type(fig.layout[axis]) == go.layout.XAxis:
        fig.layout[axis].title.text = ''
 
# ensure that each chart has its own y rage and tick labels
fig.update_yaxes(matches=None, showticklabels=True, visible=True)
 
fig.show()

Output:


Article Tags :