Open In App

How to Hide Axis in Bokeh Plot

Last Updated : 23 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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:

bokeh_plot
Hiding Both X and Y Axes

2. 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:

bokeh_plot_2
Hiding X Axes

Alternatively, 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:

bokeh_plot_2_y
Hiding Y Axes

3. 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:

bokeh_plot_pink
Hiding Grid Lines

Hiding 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:

example1
Hiding Categorical Minor Axes in Bar Charts

Additionally, refer to the link for output: Chart

2. Conditional Axis Visibility Based on User Input

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:

toggle
Conditional Axis Visibility Based on User Input

Additionally, 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