Python Bokeh Data visualization with Examples
Introduction of Bokeh in Python
Bokeh is a powerful Python library for creating interactive and visually appealing data visualizations. To effectively use Bokeh, there are several key concepts and skills you should learn:
-
Basic Plotting:
- Familiarize yourself with creating basic plots, such as line plots, scatter plots, and bar charts using Bokeh’s high-level plotting interface.
-
Customizing Visualizations:
- Learn how to customize the appearance of your plots by setting attributes like colors, markers, and labels.
-
Layouts:
- Understand how to arrange multiple plots and widgets on a single page using layout functions like
row()
,column()
, andgridplot()
.
- Understand how to arrange multiple plots and widgets on a single page using layout functions like
-
Widgets:
- Explore Bokeh’s interactive widgets like sliders, buttons, and dropdowns. These widgets allow users to interact with your visualizations dynamically.
-
Interactivity:
- Learn how to add interactivity to your plots by linking widgets to plot data and updating the plot based on user input.
-
Data Sources:
- Understand how to work with different data sources in Bokeh, including Pandas DataFrames, NumPy arrays, and ColumnDataSources.
-
Glyphs:
- Explore Bokeh’s low-level glyph API for creating complex visualizations by specifying the geometric shapes, sizes, and colors of individual data points.
-
Server Applications:
- Learn how to create interactive web applications with Bokeh Server, allowing you to build dynamic dashboards and data-driven applications.
-
Styling and Theming:
- Familiarize yourself with Bokeh’s theming and styling options to customize the overall appearance of your visualizations.
-
Exporting and Embedding:
- Discover how to save Bokeh plots as standalone HTML files or embed them in Jupyter notebooks, web applications, or websites.
-
Handling Geographic Data:
- If you’re working with geospatial data, learn how to create interactive maps and geographic visualizations using Bokeh’s
geopandas
integration.
- If you’re working with geospatial data, learn how to create interactive maps and geographic visualizations using Bokeh’s
-
Advanced Features:
- Explore advanced Bokeh features like linking and brushing, tooltips, and annotations to enhance the interactivity and informativeness of your visualizations.
-
Performance Optimization:
- Understand how to optimize the performance of your Bokeh applications and visualizations, especially when dealing with large datasets.
-
Community Resources:
- Join the Bokeh community, participate in forums, and refer to documentation and tutorials to stay up-to-date with the latest developments and best practices.
-
Integration with Other Libraries:
- Learn how to integrate Bokeh with other Python libraries like Matplotlib, Pandas, and NumPy to leverage their capabilities in conjunction with Bokeh.
-
Real-World Projects:
- Practice your Bokeh skills by working on real-world data visualization projects. This hands-on experience will help solidify your understanding.
Basic Plotting in Bokeh:
-
Import Bokeh: Start by importing the necessary Bokeh modules and functions:
from bokeh.plotting import figure, show
-
Create a Figure: Create a figure object using the
figure()
function. This figure will serve as the canvas for your plot.p = figure(width=400, height=400, title="My First Bokeh Plot")
You can specify various attributes for the figure, such as its dimensions and title.
-
Add Data to the Plot: Add your data to the plot using methods like
line()
,circle()
,square()
, orbar()
, depending on the type of plot you want to create. For example, to create a simple line plot:x = [1, 2, 3, 4, 5] y = [6, 7, 2, 4, 5] p.line(x, y, line_width=2, legend_label="Line Plot")
This code adds a line plot to the figure with the specified data points and line attributes.
-
Customize the Plot: You can customize the plot’s appearance by setting various attributes like colors, markers, and labels. For instance, you can set the axis labels:
p.xaxis.axis_label = "X-axis" p.yaxis.axis_label = "Y-axis"
-
Display the Plot: To display the plot in a Jupyter notebook or a standalone HTML file, use the
show()
function:show(p)
This will render the plot in your chosen environment.
Customizing Visualizations in Bokeh:
-
Change Colors:
- You can change the colors of various elements in your plot, such as lines, markers, and backgrounds. Bokeh allows you to specify colors using CSS color names, hexadecimal color codes, RGB tuples, and other color representations.
p.line(x, y, line_color="blue", line_width=2) p.circle(x, y, fill_color="red", size=8) p.background_fill_color = "#F0F0F0"
-
Modify Markers and Lines:
- Customize markers for data points and lines for line plots. You can specify marker types, sizes, and line properties.
p.circle(x, y, size=10, fill_color="blue", line_color="black", line_width=2) p.line(x, y, line_color="green", line_width=2, line_dash=[4, 4])
-
Add Legends and Annotations:
- Legends help identify different data series in your plot. You can add legends to your plot and customize their location, labels, and other attributes.
p.line(x1, y1, legend_label="Line 1", line_color="blue") p.line(x2, y2, legend_label="Line 2", line_color="green") p.legend.title = "Legend Title"
You can also add annotations like text or arrows to highlight specific points or features in your plot.
-
Adjust Axis Labels and Ticks:
- Customize axis labels, tick marks, and their properties, such as fonts and sizes.
p.xaxis.axis_label = "X-axis" p.yaxis.axis_label = "Y-axis" p.xaxis.major_label_text_color = "red"
-
Set Plot Titles and Backgrounds:
- You can set the title of your plot and customize its appearance.
p.title.text = "Customized Bokeh Plot" p.title.text_font_style = "italic" p.title.text_color = "navy"
Additionally, you can adjust the background color and other visual aspects of the plot canvas.
-
Use Palettes and Color Mapping:
- Bokeh provides built-in color palettes and color mapping tools to assign colors to categories or data ranges.
from bokeh.transform import linear_cmap from bokeh.models import ColorBar mapper = linear_cmap(field_name='values', palette='Viridis256', low=min(values), high=max(values)) p.circle('x', 'y', size=10, source=source, color=mapper) color_bar = ColorBar(color_mapper=mapper['transform'], width=8, location=(0,0)) p.add_layout(color_bar, 'right')
These are some of the key ways you can customize your Bokeh visualizations to make them more visually appealing and informative. The level of customization you apply to your plots will depend on your specific data visualization requirements and design preferences. Bokeh provides extensive flexibility, allowing you to create highly tailored and interactive visualizations.
Certainly! Point 3 refers to arranging multiple plots and widgets on a single page using layout functions in Bokeh. These layout functions help you organize your visualizations and interactive elements into a cohesive display. Here’s an explanation of how to work with layouts in Bokeh:
Layouts in Bokeh:
Bokeh provides several layout functions that allow you to combine multiple plots and widgets into a single layout. This is useful for creating complex dashboards or reports with multiple visualizations. Here are some of the commonly used layout functions:
-
row()
andcolumn()
:row()
andcolumn()
functions allow you to arrange plots and widgets in rows or columns, respectively. You can specify which elements you want to include and their order.
from bokeh.layouts import row, column # Create plots or widgets plot1 = create_plot1() plot2 = create_plot2() widget1 = create_widget1() # Arrange them in a row or column layout_row = row(plot1, plot2) layout_column = column(widget1, layout_row)
-
gridplot()
:gridplot()
is used to arrange multiple plots in a grid layout. You can specify the number of rows and columns, and it will automatically arrange the plots accordingly.
from bokeh.layouts import gridplot # Create plots plot1 = create_plot1() plot2 = create_plot2() plot3 = create_plot3() # Create a grid layout with 2 rows and 2 columns grid = gridplot([[plot1, plot2], [plot3, None]])
In the example above,
None
is used as a placeholder for an empty cell in the grid. -
widgetbox()
:widgetbox()
is used to arrange widgets in a vertical column. This is particularly useful when you want to organize multiple widgets together.
from bokeh.layouts import widgetbox # Create widgets widget1 = create_widget1() widget2 = create_widget2() # Arrange widgets in a vertical column widget_layout = widgetbox(widget1, widget2)
-
Nested Layouts:
- You can nest layout functions to create complex layouts. For example, you can create a layout with rows and columns inside a
column()
orrow()
layout.
from bokeh.layouts import row, column # Create plots and widgets plot1 = create_plot1() plot2 = create_plot2() widget1 = create_widget1() # Create nested layout with rows and columns nested_layout = column( row(plot1, plot2), widget1 )
- You can nest layout functions to create complex layouts. For example, you can create a layout with rows and columns inside a
-
Tabbed Layouts and Panels:
- Bokeh also supports tabbed layouts, where you can organize multiple plots or widgets into separate tabs, allowing users to switch between them.
from bokeh.models.widgets import Tabs, Panel # Create plots and widgets plot1 = create_plot1() plot2 = create_plot2() widget1 = create_widget1() # Create panels for each plot or widget tab1 = Panel(child=plot1, title="Plot 1") tab2 = Panel(child=plot2, title="Plot 2") tab3 = Panel(child=widget1, title="Widget 1") # Create a Tabs layout tabs = Tabs(tabs=[tab1, tab2, tab3])
These layout functions and techniques allow you to create organized and visually pleasing arrangements of plots and widgets in your Bokeh applications and dashboards. The choice of which layout function to use depends on your specific layout requirements and design preferences.
Certainly! Point 4 refers to working with widgets in Bokeh. Widgets are interactive elements that you can add to your Bokeh plots and applications to allow users to interact with and manipulate the data or visualizations. Widgets enable you to create dynamic and user-friendly applications. Here’s an explanation of how to use widgets in Bokeh:
Widgets in Bokeh:
Bokeh provides a wide range of widgets that you can use to add interactivity to your plots and applications. Some common widgets include sliders, buttons, dropdowns, text inputs, and more. Here’s how to work with widgets in Bokeh:
-
Import Widgets:
- Start by importing the necessary widgets from Bokeh:
from bokeh.models import Slider, Button, Select, TextInput
-
Create Widgets:
- Instantiate widgets and specify their properties, such as labels, default values, and available options.
slider = Slider(title="Slider", value=0, start=0, end=100, step=1) button = Button(label="Click Me") select = Select(title="Select", options=["Option 1", "Option 2", "Option 3"]) text_input = TextInput(title="Text Input", value="Default Text")
-
Add Widgets to the Layout:
- Once you’ve created widgets, you can add them to your plot or layout using the
add_layout()
method.
layout = column(slider, button, select, text_input)
In this example, the widgets are arranged in a vertical column.
- Once you’ve created widgets, you can add them to your plot or layout using the
-
Define Callback Functions:
- To make widgets interactive, you can define callback functions that specify what happens when a user interacts with a widget. Callbacks can be triggered by events such as a button click, slider value change, or dropdown selection.
def button_callback(): # Define the action to be performed when the button is clicked # For example, update the plot based on user input pass def slider_callback(attr, old_val, new_val): # Define the action to be performed when the slider value changes # For example, update the plot with the new value pass button.on_click(button_callback) slider.on_change('value', slider_callback)
You can attach callback functions to widgets to specify how the plot or application should respond to user input.
-
Display the Widgets and Plot:
- Finally, you can display the widgets and plot using the
show()
function or by embedding them in a Bokeh application.
from bokeh.io import curdoc curdoc().add_root(layout)
This will render the widgets and plot, making them accessible to users in a web browser.
- Finally, you can display the widgets and plot using the
By following these steps, you can create interactive Bokeh applications and dashboards that allow users to explore and manipulate data through widgets. Widgets enable you to build data-driven and user-friendly visualizations and applications tailored to your specific needs.
Data Sources in Bokeh
-
Pandas DataFrames:
- Bokeh can seamlessly work with Pandas DataFrames, which are widely used for data manipulation and analysis in Python. You can pass a Pandas DataFrame directly to Bokeh plotting functions or create a ColumnDataSource from it.
import pandas as pd from bokeh.plotting import figure, show from bokeh.models import ColumnDataSource # Create a Pandas DataFrame data = pd.DataFrame({'x': [1, 2, 3, 4, 5], 'y': [6, 7, 2, 4, 5]}) # Convert the DataFrame to a ColumnDataSource source = ColumnDataSource(data=data) # Create a plot using the ColumnDataSource p = figure() p.circle('x', 'y', source=source) show(p)
-
NumPy Arrays:
- You can also work with NumPy arrays as your data source. Bokeh accepts NumPy arrays directly in its plotting functions.
import numpy as np from bokeh.plotting import figure, show # Create NumPy arrays x = np.array([1, 2, 3, 4, 5]) y = np.array([6, 7, 2, 4, 5]) # Create a plot using NumPy arrays p = figure() p.circle(x, y) show(p)
-
ColumnDataSource:
ColumnDataSource
is a Bokeh-specific data structure designed to efficiently handle and update data for interactive visualizations. You can use it to manage data that may change dynamically.
from bokeh.plotting import figure, show from bokeh.models import ColumnDataSource # Create a ColumnDataSource directly source = ColumnDataSource(data={'x': [1, 2, 3, 4, 5], 'y': [6, 7, 2, 4, 5]}) # Create a plot using the ColumnDataSource p = figure() p.circle('x', 'y', source=source) show(p)
ColumnDataSource
allows you to update the data source dynamically, making it ideal for building interactive visualizations with widgets.
Working with different data sources in Bokeh gives you the flexibility to adapt to your preferred data structures and seamlessly integrate your data into your visualizations. Whether you have data in Pandas DataFrames, NumPy arrays, or other formats, Bokeh provides the tools to work with them effectively in your data-driven applications and dashboards.