How Do I Remove Grid Lines from a Bokeh Plot?
Last Updated :
03 Sep, 2024
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
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:
Plot with Grid LinesStep 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:
Remove Grid LinesRemoving 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:
Remove Grid Lines from a Bokeh PlotUse 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:
Remove Grid Lines from a Bokeh PlotConclusion
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.
Similar Reads
How to Hide Axis in Bokeh Plot
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 da
5 min read
How to Remove Grid lines in Excel
When you open the Excel sheet, you see a cross-section of rows and columns with grey-coloured lines that separate cells. Those gray-colored lines are known as gridlines in MS Excel. The grey color is the default color offered by Microsoft and one can change the color of the gridlines. These Guidelin
10 min read
How to Remove Lines in Google Docs
Have you ever encountered unwanted lines in Google Docs that disrupt the flow of your document? Whether itâs a table border, a horizontal line created by typing hyphens or underscores, or paragraph borders, these lines can sometimes make your document look cluttered. The good news is, removing them
8 min read
Remove grid and background from plot using ggplot2 in R
A plot by default is produced with a grid background and grayish colored background. In this article we will see, how a gird can be removed from a plot. We will use a line plot, for demonstration but the same can be employed for any other Visualization.To understand the difference better let us firs
1 min read
How to add color-picker in Bokeh?
In this article, we are going to see how to add a color picker widget in bokeh. Widgets are the best way to make charts more interactive. We can also add widgets in the Bokeh application to provide the best front-end user visualization. Using widgets we can do many things like update charts, connec
1 min read
How to add a grid on a figure in Matplotlib ?
Matplotlib library is widely used for plotting graphs. In many graphs, we require to have a grid to improve readability. Grids are created by using grid() function in the Pyplot sublibrary. In this article, we will see how to add grid in Matplotlb. Add a Grid on a Figure in MatplotlibBelow are the w
3 min read
Python Bokeh - Plotting a Line Graph
Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot a line graph. Plotting a line gra
4 min read
How to use Color Palettes in Python-Bokeh?
Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh provides us with multiple color palettes in the bokeh
7 min read
Create a two line legend in a Bokeh Plot
In this article, we will be learning about how to create a two line legend in the bokeh plot. Legends are one of the most essential parts of a bokeh plot. They help us to differentiate between different glyphs used in a plot. Other than that if we need to plot different lines of different colors in
7 min read
How to Add Color Bars in Bokeh?
Bokeh is one of the promising libraries of Python in recent times. It provides high performance and efficiency in data visualizations. One of the big advantages of bokeh is that we can get the output file in various formats such as HTML, notebooks etc. In this article, we will be learning about how
7 min read