How to Hide Axis in Bokeh Plot
Last Updated :
23 Jul, 2025
Bokeh is a powerful visualization library in Python that enables the creation of interactive plots and dashboards. One of the customization features it offers is the ability to hide axes, which can be useful for creating cleaner visualizations or when the axes are not needed for understanding the data. This article will guide you through the steps to hide axes in Bokeh, along with some practical examples and tips.
Understanding Bokeh's Plotting Interface
Before diving into hiding axes, it's essential to understand the basic structure of a Bokeh plot. Bokeh uses a figure object as the central element for creating plots. This object allows you to add various elements like lines, circles, and axes to your plot. The figure object also provides methods to customize these elements, including visibility settings.
Why Hide Axes from Bokeh Plot?
There are several reasons why you might want to hide axes in a Bokeh plot:
- Aesthetic Purposes: To create a minimalist design where axes are not necessary.
- Focus on Data: To emphasize the data points or other elements without the distraction of axes.
- Custom Layouts: In dashboards or multi-plot layouts where axes might be redundant.
Hiding Axes in Bokeh Plot
Hiding axes in Bokeh can be achieved through several methods, each allowing for different levels of customization.
1. Hiding Both X and Y Axes
To hide both the x and y axes, you can set the visible attribute of the axes to False. Here is how you can do it:
Python
from bokeh.plotting import figure, show, output_notebook
# Create a figure object
p = figure(title="Plot without Axes", width=600, height=300)
# Add scatter data to the figure
p.scatter([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, color="navy", alpha=0.5)
# Hide both x and y axes
p.xaxis.visible = False
p.yaxis.visible = False
output_notebook()
show(p)
Output:
Hiding Both X and Y Axes2. Hiding Individual Axes
If you only need to hide one of the axes, you can set the visible attribute for either the x-axis or the y-axis individually.
Python
from bokeh.plotting import figure, show, output_notebook
# Create a figure object
p = figure(title="Plot without X Axis", width=600, height=300)
# Add scatter data to the figure
p.scatter([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, color="orange", alpha=0.5)
# Hide the x-axis
p.xaxis.visible = False
# Add some data to the plot
output_notebook()
show(p)
Output:
Hiding X AxesAlternatively, you can hide the y-axis:
Python
from bokeh.plotting import figure, show, output_notebook
# Create a figure object
p = figure(title="Plot without X Axis", width=600, height=300)
# Add scatter data to the figure
p.scatter([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, color="orange", alpha=0.5)
# Hide the x-axis
p.yaxis.visible = False
# Add some data to the plot
output_notebook()
show(p)
Output:
Hiding Y Axes3. Hiding Grid Lines
In addition to hiding the axes, you might also want to hide the grid lines for a cleaner appearance. This can be done by setting the visible attribute for the grid lines to False.
Python
from bokeh.plotting import figure, show, output_notebook
# Create a figure object
p = figure(title="Plot without Grid lines and Axis", width=600, height=300)
# Add scatter data to the figure
p.scatter([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, color="pink", alpha=0.5)
# Hide both axis
p.xaxis.visible = False
p.yaxis.visible = False
p.xgrid.visible = False
p.ygrid.visible = False
# Add some data to the plot
output_notebook()
show(p)
Output:
Hiding Grid LinesHiding Axis for better Visualization : Practical Examples
1. Hiding Categorical Minor Axes in Bar Charts
In some bar charts, particularly those with nested categories, you may want to hide minor axes to simplify the visualization. For example, in a chart displaying fruit counts by year, you might want to hide the year labels and only display the fruit categories.
In this example, the minor x-axis labels (years) are hidden by setting the major_label_text_alpha to 0.0 and reducing the major_label_text_font_size
Python
from bokeh.io import output_file, show, output_notebook
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.transform import factor_cmap
from bokeh.plotting import figure
output_file("bars.html")
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']
data = {'fruits': fruits,
'2015': [2, 1, 4, 3, 2, 4],
'2016': [5, 3, 3, 2, 4, 6],
'2017': [3, 2, 4, 4, 5, 3]}
x = [(fruit, year) for fruit in fruits for year in years]
counts = sum(zip(data['2015'], data['2016'], data['2017']), ())
source = ColumnDataSource(data=dict(x=x, counts=counts))
p = figure(x_range=FactorRange(*x), height=250, title="Fruit counts by year",
toolbar_location=None, tools="")
palette = ["#c9d9d3", "#718dbf", "#e84d60"]
p.vbar(x='x', top='counts', width=0.9, source=source, line_color="white",
fill_color=factor_cmap('x', palette=palette, factors=years, start=1, end=2))
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
p.xaxis.major_label_text_alpha = 0.0
p.xaxis.major_label_text_font_size = '1px'
output_notebook()
show(p)
Output:
Hiding Categorical Minor Axes in Bar ChartsAdditionally, refer to the link for output: Chart
You can create interactive plots where the visibility of axes changes based on user input, such as toggle buttons or dropdown menus.
In this example, a Toggle button is used to switch the x-axis visibility on and off. The on_change method is used to bind the toggle button's state to the axis visibility
Python
from bokeh.io import show
from bokeh.layouts import layout
from bokeh.models import Toggle
from bokeh.plotting import figure
p = figure(width=600, height=200, tools='')
p.line([1, 2, 3], [1, 2, 1], line_color="#0072B2")
toggle = Toggle(label="Toggle X-Axis", button_type="success", active=True)
def toggle_x_axis(attr, old, new):
p.xaxis.visible = new
toggle.on_change('active', toggle_x_axis)
show(layout([p], [toggle]))
Output:
Conditional Axis Visibility Based on User InputAdditionally, refer to the link for output: Chart
Conclusion
Hiding axes in Bokeh can significantly enhance the aesthetics and focus of your visualizations. Whether you're aiming for a minimalist design or creating a complex dashboard, understanding how to manipulate axis visibility is a valuable skill. By following the steps outlined in this article, you can effectively control the appearance of axes in your Bokeh plots, tailoring them to suit your specific needs.
Similar Reads
Python - Data visualization tutorial Data visualization is a crucial aspect of data analysis, helping to transform analyzed data into meaningful insights through graphical representations. This comprehensive tutorial will guide you through the fundamentals of data visualization using Python. We'll explore various libraries, including M
7 min read
What is Data Visualization and Why is It Important? Data visualization uses charts, graphs and maps to present information clearly and simply. It turns complex data into visuals that are easy to understand.With large amounts of data in every industry, visualization helps spot patterns and trends quickly, leading to faster and smarter decisions.Common
4 min read
Data Visualization using Matplotlib in Python Matplotlib is a widely-used Python library used for creating static, animated and interactive data visualizations. It is built on the top of NumPy and it can easily handles large datasets for creating various types of plots such as line charts, bar charts, scatter plots, etc. Visualizing Data with P
11 min read
Data Visualization with Seaborn - Python Seaborn is a popular Python library for creating attractive statistical visualizations. Built on Matplotlib and integrated with Pandas, it simplifies complex plots like line charts, heatmaps and violin plots with minimal code.Creating Plots with SeabornSeaborn makes it easy to create clear and infor
9 min read
Data Visualization with Pandas Pandas is a powerful open-source data analysis and manipulation library for Python. The library is particularly well-suited for handling labeled data such as tables with rows and columns. Pandas allows to create various graphs directly from your data using built-in functions. This tutorial covers Pa
6 min read
Plotly for Data Visualization in Python Plotly is an open-source Python library designed to create interactive, visually appealing charts and graphs. It helps users to explore data through features like zooming, additional details and clicking for deeper insights. It handles the interactivity with JavaScript behind the scenes so that we c
12 min read
Data Visualization using Plotnine and ggplot2 in Python Plotnine is a Python data visualization library built on the principles of the Grammar of Graphics, the same philosophy that powers ggplot2 in R. It allows users to create complex plots by layering components such as data, aesthetics and geometric objects.Installing Plotnine in PythonThe plotnine is
6 min read
Introduction to Altair in Python Altair is a declarative statistical visualization library in Python, designed to make it easy to create clear and informative graphics with minimal code. Built on top of Vega-Lite, Altair focuses on simplicity, readability and efficiency, making it a favorite among data scientists and analysts.Why U
4 min read
Python - Data visualization using Bokeh Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots. Bokeh output can be obtained in various mediums like notebook, html and server. It is possible to embed bokeh plots in Django and flask apps. Bokeh provides two visualization interfaces to us
4 min read
Pygal Introduction Python has become one of the most popular programming languages for data science because of its vast collection of libraries. In data science, data visualization plays a crucial role that helps us to make it easier to identify trends, patterns, and outliers in large data sets. Pygal is best suited f
5 min read