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

28 Nov, 2021
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
Python3
import pandas as pdimport numpy as npimport plotly.express as pximport stringimport plotly.graph_objects as go  # create a dataframecols = ['a', 'b', 'c', 'd', 'e']n = 50  df = pd.DataFrame({'Date': pd.date_range('2021-1-1', periods=n)})  # create data with vastly different rangesfor 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 longdfm = df.melt("Date")  # make the plotfig = 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 titlesfor 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 labelsfig.update_yaxes(matches=None, showticklabels=True, visible=True)  fig.show()
Output:
Example 2:
Temperature vs City data
Python3
import pandas as pdimport numpy as npimport plotly.express as pximport stringimport plotly.graph_objects as go  # create a dataframecols = ['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 rangesfor 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 longdfm = df.melt("Date")  # make the plotfig = 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 titlesfor 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 labelsfig.update_yaxes(matches=None, showticklabels=True, visible=True)  fig.show()
Output:
Picked
Python-Plotly
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()