Open In App

How Do I Remove Grid Lines from a Bokeh Plot?

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Bokeh is a powerful Python library for creating interactive and visually appealing visualizations for web browsers. While grid lines can help in interpreting the data in a plot, there are scenarios where you might want to remove them for a cleaner, more focused visualization. This article will guide you through the steps to remove grid lines from a Bokeh plot.

Understanding Bokeh Grids

Before diving into the removal of grid lines, it's essential to understand how Bokeh handles grids. Bokeh plots include both major and minor grid lines, which are controlled by various properties. These properties allow for extensive customization, including the ability to hide grid lines entirely.

Why Remove Grid Lines?

Grid lines are often included in plots to help viewers align data points with the axes. However, in some cases, they can add unnecessary clutter, especially when the data points are dense or when the grid lines do not add significant value to the interpretation of the data.

Removing grid lines can lead to a cleaner and more aesthetically pleasing visualization, allowing the audience to focus on the data itself.

Steps to Remove Grid Lines in Bokeh

xgrid.grid_line_color and ygrid.grid_line_color: These attributes control the color of the grid lines on the x-axis and y-axis, respectively. Setting them to None effectively hides the grid lines.

Step 1: Install and Import Bokeh

First, ensure you have Bokeh installed. If not, you can install it using pip:

Python
pip install bokeh

Then, import the necessary modules:

Python
from bokeh.plotting import figure, show
from bokeh.io import output_notebook

# Display plots inline in Jupyter Notebook
output_notebook()

Step 2: Create a Sample Plot with Grid Lines

Here’s how you can create a basic plot with grid lines:

Python
# Create a new plot
p = figure(width=400, height=400, title="Plot with Grid Lines")

# Add a line renderer
p.line(x=[1, 2, 3, 4, 5], y=[6, 7, 2, 4, 5], line_width=2)

# Show the plot with grid lines
show(p)

Output:

Screenshot-2024-08-28-224031
Plot with Grid Lines

Step 3: Remove Grid Lines

To remove grid lines, you need to set the grid line properties to None. You can do this for both the x-axis and y-axis grids:

Python
# Create a new plot without grid lines
p_no_grid = figure(width=400, height=400, title="Plot Without Grid Lines")

# Add a line renderer
p_no_grid.line(x=[1, 2, 3, 4, 5], y=[6, 7, 2, 4, 5], line_width=2)

# Remove grid lines
p_no_grid.xgrid.grid_line_color = None
p_no_grid.ygrid.grid_line_color = None

# Show the plot without grid lines
show(p_no_grid)

Output:

Screenshot-2024-08-28-224043
Remove Grid Lines

Removing Grid lines in Bokeh plots : Practical Examples

Removing grid lines in Bokeh plots can help create cleaner visualizations and make the plot easier to interpret. Here are two use cases where removing grid lines can enhance the clarity and aesthetic of your Bokeh plots:

Use Case 1: Cleaner Scatter Plot for Presentation

When preparing a scatter plot for a presentation or report, you might want to remove the grid lines to make the data points stand out more clearly. This can help the audience focus on the data itself without the distraction of grid lines.

Python
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
import numpy as np

# Display plots directly in the notebook
output_notebook()

# Generate larger sample data
np.random.seed(42)  # For reproducibility
x = np.random.randint(1, 100, size=50)  # Generate 50 random x values between 1 and 100
y = np.random.randint(1, 100, size=50)  # Generate 50 random y values between 1 and 100

# Create a figure with specified width and height
p = figure(title="Scatter Plot without Grid Lines", x_axis_label='X-Axis', y_axis_label='Y-Axis', width=800, height=400)

# Add scatter plot
p.circle(x, y, size=10, color="navy", alpha=0.5)

# Remove grid lines
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None

# Show plot
show(p)

Output:

bokeh_plot
Remove Grid Lines from a Bokeh Plot

Use Case 2: Minimalistic Bar Chart for Publication

For publication in a scientific journal or a minimalist design for a dashboard, you might want to remove grid lines to align with the aesthetic requirements. Removing grid lines can make the bar chart appear cleaner and more professional.

Python
from bokeh.plotting import figure, show
from bokeh.io import output_notebook

# Display plots directly in the notebook
output_notebook()

# Expanded sample data
categories = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
values = [15, 30, 45, 10, 25, 50, 35, 20, 40, 55]

# Create a figure with categorical x-axis
p = figure(x_range=categories, title="Bar Chart without Grid Lines", 
           x_axis_label='Category', y_axis_label='Values', width=800, height=400)

# Add bar chart
p.vbar(x=categories, top=values, width=0.5, color="firebrick")

# Remove grid lines
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None

# Show plot
show(p)

Output:

bokeh_plot
Remove Grid Lines from a Bokeh Plot

Conclusion

Removing grid lines in a Bokeh plot is straightforward and can help you create a cleaner and more focused visualization. By setting grid_line_color to None, you can easily remove grid lines, or you can customize their appearance to better suit your needs.


Next Article

Similar Reads