What is Bqplot? Example of data visualization using Bqplot library. Introduction to Bqplot package in Python with examples.
Bqplot is a Python plotting library based on the interactive visualization library, Plotly. It provides a high-level API for creating interactive and customizable visualizations in Jupyter notebooks and web applications. Bqplot allows us to create rich and interactive visualizations using the Plotly.js library.
Bqplot offers a wide range of plot types, including line plots, scatter plots, bar charts, histograms, heatmaps, and more. It also provides support for interactivity, such as panning, zooming, and brushing. You can create complex visualizations by combining multiple plots and widgets together.
One of the key features of Bqplot is its integration with Jupyter notebooks. It allows you to create interactive plots directly in the notebook environment, enabling you to explore and analyze data more effectively. Bqplot also supports the Jupyter interactive widgets framework, which means you can link plots and widgets together to create dynamic visualizations that respond to user interactions.
The library was initially developed by Bloomberg LP and was first released in 2016. The motivation behind the creation of Bqplot was to provide a high-level plotting library that leverages the power of Jupyter notebooks and allows users to create interactive visualizations directly in the notebook environment.
Bqplot was inspired by the success and popularity of the D3.js library, which provides powerful tools for creating data visualizations on the web. The developers of Bqplot aimed to bring similar interactivity and flexibility to Python users, with a focus on integration with Jupyter notebooks.
The library is built on top of Plotly, which is a widely-used open-source library for creating interactive visualizations. By using Plotly as a backend, Bqplot is able to generate interactive visualizations that can be displayed and manipulated directly within Jupyter notebooks.
Bqplot provides a range of plot types, including line plots, scatter plots, bar charts, histograms, heatmaps, and more. It also supports interactivity features like panning, zooming, and brushing, allowing users to explore and analyze data more effectively.
Since its initial release, Bqplot has gained popularity among data scientists, researchers, and developers who work with Jupyter notebooks. The library has a growing community and continues to receive updates and improvements.
Using pip:
pip install bqplot
Using conda:
conda install -c conda-forge bqplot
If you are using JupyterLab <= 2:
jupyter labextension install @jupyter-widgets/jupyterlab-manager bqplot
Line plot
import bqplot.pyplot as plt
import numpy as np# create data vectors x and y to plot using a Lines mark
x = np.linspace(-10, 10, 100)
y = np.sin(x)
# 1. Create the figure object
fig = plt.figure(title="Line Chart")
# 2. By default axes are created with basic defaults. If you want to customize the axes create
# a dict and pass it to axes_options argument in the marks
axes_opts = {"x": {"label": "X"}, "y": {"label": "Y"}}
# 3. Create a Lines mark by calling plt.plot function
line = plt.plot(
x=x, y=y, axes_options=axes_opts
) # note that custom axes options are passed to the mark function
# 4. Render the figure using plt.show() (displays toolbar as well)
plt.show()
Bar Chart
# first, create data vectors x and y to plot a bar chart
x = list("ABCDE")
y = np.random.rand(5)# 1. Create the figure object
fig = plt.figure(title="Bar Chart")
# 2. Customize the axes options
axes_opts = {
"x": {"label": "X", "grid_lines": "none"},
"y": {"label": "Y", "tick_format": ".0%"},
}
# 3. Create a Bars mark by calling plt.bar function
bar = plt.bar(x=x, y=y, padding=0.5, axes_options=axes_opts)
# 4. directly display the figure object created in step 1 (note that the toolbar no longer shows up)
fig
Scatter Plot
# first, let's create two vectors x and y
x = np.linspace(-10, 10, 25)
y = 3 * x + 5
y_noise = y + 10 * np.random.randn(25) # add some random noise to y# 1. Create the figure object
fig = plt.figure(title="Scatter and Line")
# 3. Create line and scatter marks
# additional attributes (stroke_width, colors etc.) can be passed as attributes
# to the mark objects as needed
line = plt.plot(x=x, y=y, colors=["green"], stroke_width=3)
scatter = plt.scatter(x=x, y=y_noise, colors=["red"], stroke="black")
# setting x and y axis labels using pyplot functions. Note that these functions
# should be called only after creating the marks
plt.xlabel("X")
plt.ylabel("Y")
# 4. render the figure
fig
World Map
import bqplot.pyplot as pltfig = plt.figure(title="World Map")
plt.geo(map_data="WorldMap",
colors={"default_color": "steelblue"})
fig
Choropleth
fig = plt.figure(title="Choropleth")plt.scales(scales={"color": bq.ColorScale(scheme="Greens")})
chloro_map = plt.geo(
map_data="WorldMap",
color={643: 105, 4: 21, 398: 23, 156: 42, 124: 78, 76: 98},
colors={"default_color": "Grey"},
)
fig
Advanced Projections
import bqplot as bqfig = plt.figure(title="Advanced World Map")
geo_scale = bq.Orthographic(scale_factor=375, center=[0, 25], rotate=(-50, 0))
plt.scales(scales={"projection": geo_scale})
map_mark = plt.geo(
map_data="WorldMap",
colors={682: "green", 356: "red", 643: "blue", "default_color": "orange"},
)
fig
All other details and more examples can be found here
— — —
Why did the Bqplot library become a magician?
Because it could magically turn data into captivating visual illusions!
🙂🙂🙂