Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Bokeh be used to create step line plot in Python?
Bokeh is a Python package that helps in data visualization. It is an open source project. Bokeh renders its plots using HTML and JavaScript, making it ideal for web-based dashboards and interactive applications.
Bokeh can be easily used in conjunction with NumPy, Pandas, and other Python packages. It can be used to produce interactive plots, dashboards, and real-time data visualizations.
Installation
You can install Bokeh using pip or conda ?
pip3 install bokeh
Or using Anaconda ?
conda install bokeh
Creating Step Line Plots
The step() function in Bokeh is used to create step line plots, which are useful for displaying discrete data points where values change abruptly rather than continuously.
Basic Step Line Plot
Here's how to create a simple step line plot ?
from bokeh.plotting import figure, show
from bokeh.io import output_file
# Create output file
output_file("stepLine.html")
# Create figure
p = figure(plot_width=500, plot_height=300, title="Step Line Plot Example")
# Create step plot
p.step([2, 5, 3, 6, 7, 9], [6, 3, 2, 1, 0, 5], line_width=2, mode="center")
# Display the plot
show(p)
Step Plot Modes
Bokeh provides different modes for step plots to control how the steps are positioned ?
from bokeh.plotting import figure, show
from bokeh.layouts import column
from bokeh.io import output_file
output_file("step_modes.html")
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 8, 6]
# Before mode
p1 = figure(plot_width=400, plot_height=200, title="Mode: before")
p1.step(x, y, line_width=2, mode="before", color="red")
# After mode
p2 = figure(plot_width=400, plot_height=200, title="Mode: after")
p2.step(x, y, line_width=2, mode="after", color="green")
# Center mode
p3 = figure(plot_width=400, plot_height=200, title="Mode: center")
p3.step(x, y, line_width=2, mode="center", color="blue")
show(column(p1, p2, p3))
Customizing Step Plots
You can customize step plots with various styling options ?
from bokeh.plotting import figure, show
from bokeh.io import output_file
output_file("custom_step.html")
p = figure(plot_width=600, plot_height=400, title="Customized Step Plot")
x = [1, 2, 3, 4, 5, 6]
y = [10, 15, 12, 18, 14, 20]
# Customized step plot
p.step(x, y,
line_width=3,
line_color="navy",
line_alpha=0.8,
mode="center")
# Add circle markers at data points
p.circle(x, y, size=8, color="red", alpha=0.6)
# Customize axes
p.xaxis.axis_label = "X Values"
p.yaxis.axis_label = "Y Values"
show(p)
Mode Comparison
| Mode | Description | Use Case |
|---|---|---|
before |
Step occurs before the data point | Right-continuous data |
after |
Step occurs after the data point | Left-continuous data |
center |
Step is centered on the data point | Symmetric representation |
Conclusion
Bokeh's step() function provides an effective way to visualize discrete data changes. Use different modes (before, after, center) based on your data's continuity requirements and customize with colors, line width, and markers for better presentation.
