5 Best Ways to Visualize Data in Python using Bokeh

Rate this post

πŸ’‘ Problem Formulation: Data visualization is crucial in data analysis, allowing for the detection of patterns, trends, and outliers in datasets. Bokeh, a powerful Python library, enables the creation of interactive and appealing visualizations. Assuming we have a dataset containing sales numbers by month, the goal is to leverage Bokeh to depict this information graphically, enhancing decision-making and insight generation.

Method 1: Basic Line Chart

The line chart is a fundamental tool in data visualization, effectively representing data trends over time. Bokeh’s line chart plotting capabilities are rich and easily customizable. The method involves creating a figure instance and using the line() function, which takes x and y coordinates as arguments, to draw the chart.

Here’s an example:

from bokeh.plotting import figure, output_file, show

# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [100, 120, 150, 170, 160]

# Output static HTML file
output_file("line_chart.html")

# Create a new plot with a title and axis labels
p = figure(title="Monthly Sales", x_axis_label='Month', y_axis_label='Sales')

# Add a line renderer with legend and line thickness
p.line(months, sales, legend_label="Sales Data", line_width=2)

# Show the results
show(p)

Output: An interactive HTML file named “line_chart.html” is generated, displaying a line graph illustrating the sales trend across five months.

In the example above, the figure() function initializes a new plot, where parameters define the title and axis labels. The line() method plots the data, specifying both the x and y axes data, the legend, and line appearance. The output_file() function sets the output target, and show() renders the plot in the file.

Method 2: Bar Chart

Bar charts are efficient for comparing different groups of data. Bokeh simplifies creating bar charts with the vbar() method, providing parameters to control the width, color, and alignment of the bars.

Here’s an example:

from bokeh.plotting import figure, output_file, show

# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [100, 120, 150, 170, 160]

# Output static HTML file
output_file("bar_chart.html")

# Create a new plot
p = figure(x_range=months, title="Monthly Sales",
           x_axis_label='Month', y_axis_label='Sales')

# Add a bar renderer
p.vbar(x=months, top=sales, width=0.5)

# Show the results
show(p)

Output: An interactive HTML file named “bar_chart.html” with a bar chart representing sales for each month.

This code snippet illustrates how to create a vertical bar chart using Bokeh. After setting sample data and output target, a figure object is created. The vbar() function is then used, accepting the month names as the x-coordinates and the sales numbers as the top of the bars. The result is visualized with the show() function.

Method 3: Scatter Plot

Scatter plots are another fundamental visualization tool utilized to identify relationships or patterns between two data variables. Bokeh’s scatter() method makes it easy to create colorful and distinct scatter plots.

Here’s an example:

from bokeh.plotting import figure, output_file, show

# Sample data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# Output to static HTML file
output_file("scatter_plot.html")

# Create a new plot
p = figure(title="Simple Scatter Plot", x_axis_label='X-Value', y_axis_label='Y-Value')

# Add a scatter renderer
p.scatter(x, y, size=8, color="navy", fill_alpha=0.5)

# Show the results
show(p)

Output: An interactive HTML file named “scatter_plot.html” displaying a scatter plot with points.

The code provided demonstrates how to craft a simple scatter plot using Bokeh. The figure object defines the plot’s initial properties. The scatter() function plots the data points, with additional parameters to control point size, color, and opacity. The final chart is displayed in an HTML page using the show() function.

Method 4: Interactive Widgets

Bokeh excels in creating interactive visualizations by allowing users to incorporate widgets. One can create a range of input controls, such as sliders or selection boxes, which permit real-time interaction with the data visualization.

Here’s an example:

from bokeh.layouts import column
from bokeh.models import Slider
from bokeh.plotting import figure, output_file, show

# Sample data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# Output static HTML file
output_file("interactive_plot.html")

# Create a new plot
p = figure(title="Interactive Slider Example")

# Add a line renderer
line = p.line(x, y, line_width=3)

# Add a slider widget
slider = Slider(start=0, end=10, value=1, step=.1, title="Slider")

# Define a callback function
def callback(attr, old, new):
    line.glyph.line_width = slider.value

slider.on_change('value', callback)

# Layout the widgets and the plot
layout = column(slider, p)

# Show the results
show(layout)

Output: An interactive HTML file named “interactive_plot.html” containing a line plot and a slider to adjust the line width.

In this example, an interactive visualization is created using a slider widget alongside a line plot. The Slider() widget enables users to interactively modify the line width in real-time. The on_change() function binds the callback to the slider’s value, defining the reaction to user input. The layout is then arranged vertically using the column() function, and rendered using show().

Bonus One-Liner Method 5: Simple Pie Chart

Although Bokeh does not have a built-in pie chart function, it can be emulated using wedges, which can be implemented in a concise one-liner approach inside a figure definition.

Here’s an example:

from bokeh.plotting import figure, show

# Define data
data = {'fruits': ['Apples', 'Pears', 'Nectarines', 'Plums'],
        'counts': [10, 20, 30, 40]}

p = figure(title="Fruit Counts", toolbar_location=None, tools="hover", tooltips="@fruits: @counts")

p.wedge(x=[0]*4, y=[0]*4, radius=0.8, start_angle=[0, 0.5, 1.0, 1.5], end_angle=[0.5, 1.0, 1.5, 2],
         color=["red", "green", "blue", "orange"], legend_field="fruits")

show(p)

Output: A basic pie chart representing fruit counts with hover tool for details.

This succinct code snippet demonstrates how to employ the wedge() functions in Bokeh to create a simple pie chart representation. Various parameters define the position, size, and slices of the pie, providing a faux pie chart effect. The legend and tooltips enhance the interactivity of the visualization.

Summary/Discussion

  • Method 1: Basic Line Chart. Ideal for illustrating trends over time. Might not be suitable for datasets where relationships between multiple variables are to be explored.
  • Method 2: Bar Chart. Effective for categorical data comparison. Not recommended for continuous data or to show trends.
  • Method 3: Scatter Plot. Best for visualizing relationships between two numerical variables. Can become cluttered if too many points are present.
  • Method 4: Interactive Widgets. Adds interactivity to visualizations, enhancing user engagement. Requires some JavaScript knowledge for more advanced interactions.
  • Method 5: Simple Pie Chart. Good for showing proportions for categorical data. Not a native Bokeh functionality, which may mean it has limitations compared to other libraries focused on this type of chart.