How to Make an Area Plot in Python using Bokeh

To create an area plot in Python using Bokeh, you can follow these steps:

  1. Import the necessary libraries: Bokeh, NumPy, and Pandas.
import bokeh.io
import bokeh.plotting
import numpy as np
import pandas as pd
  1. Create a Pandas DataFrame with the data you want to plot.
data = {'x': [1, 2, 3, 4, 5],
        'y1': [2, 5, 3, 4, 6],
        'y2': [1, 4, 2, 3, 5],
        'y3': [0, 2, 1, 2, 3]}

df = pd.DataFrame(data)
  1. Create a figure object and specify the x and y variables.
fig = bokeh.plotting.figure(x_axis_label='X-axis',
                            y_axis_label='Y-axis')
  1. Add the area glyphs to the figure using the varea_stack() function.
fig.varea_stack(['y1', 'y2', 'y3'], x='x', color=('red', 'green', 'blue'), source=df)

Here, varea_stack() creates a stacked area plot, where the areas for each variable are stacked on top of each other.

  1. Show the plot using the show() function.
bokeh.io.show(fig)

The complete code to create an area plot using Bokeh is:

import bokeh.io
import bokeh.plotting
import numpy as np
import pandas as pd

data = {'x': [1, 2, 3, 4, 5],
        'y1': [2, 5, 3, 4, 6],
        'y2': [1, 4, 2, 3, 5],
        'y3': [0, 2, 1, 2, 3]}

df = pd.DataFrame(data)

fig = bokeh.plotting.figure(x_axis_label='X-axis',
                            y_axis_label='Y-axis')

fig.varea_stack(['y1', 'y2', 'y3'], x='x', color=('red', 'green', 'blue'), source=df)

bokeh.io.show(fig)

This will create an area plot with three stacked areas in different colors, representing the three variables y1, y2, and y3.

Plotting the Area Plots:

To plot an area chart using Python and Matplotlib, you can follow these steps:

  1. Import the necessary libraries: Matplotlib and NumPy.
import matplotlib.pyplot as plt
import numpy as np
  1. Create a NumPy array with the x and y values you want to plot.
x = np.array([1, 2, 3, 4, 5])
y1 = np.array([2, 5, 3, 4, 6])
y2 = np.array([1, 4, 2, 3, 5])
y3 = np.array([0, 2, 1, 2, 3])
  1. Create a figure object using plt.subplots(). This will return a tuple containing the figure object and the axes object.
fig, ax = plt.subplots()
  1. Plot the stacked areas using the fill_between() function.
ax.fill_between(x, y1, color='red', alpha=0.5)
ax.fill_between(x, y1, y2, color='green', alpha=0.5)
ax.fill_between(x, y2, y3, color='blue', alpha=0.5)

Here, fill_between() creates the stacked area plot, where the areas for each variable are stacked on top of each other.

  1. Add labels and legend to the plot.
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.legend(['y1', 'y2', 'y3'])
  1. Show the plot using the plt.show() function.
plt.show()

The complete code to create an area plot using Matplotlib is:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4, 5])
y1 = np.array([2, 5, 3, 4, 6])
y2 = np.array([1, 4, 2, 3, 5])
y3 = np.array([0, 2, 1, 2, 3])

fig, ax = plt.subplots()

ax.fill_between(x, y1, color='red', alpha=0.5)
ax.fill_between(x, y1, y2, color='green', alpha=0.5)
ax.fill_between(x, y2, y3, color='blue', alpha=0.5)

ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.legend(['y1', 'y2', 'y3'])

plt.show()

This will create an area plot with three stacked areas in different colors, representing the three variables y1, y2, and y3.

1. varea() Function:

varea() is a function in Bokeh that creates a vertical area plot with filled regions between a central line and a lower line. It is similar to the harea() function, but it plots the data vertically instead of horizontally.

The varea() function takes several arguments:

  • x: The x-coordinates of the data points.
  • y1: The y-coordinates of the central line.
  • y2: The y-coordinates of the lower line.
  • fill_alpha (optional): The opacity of the filled regions. Default is 0.1.
  • line_alpha (optional): The opacity of the central line and the lower line. Default is 1.0.
  • line_color (optional): The color of the central line and the lower line. Default is ‘black’.
  • fill_color (optional): The color of the filled regions. Default is ‘blue’.
  • legend_label (optional): The label for the plot in the legend. Default is None.
  • source (optional): The source of data for the plot. Default is None.

Here’s an example of how to use varea() to create a vertical area plot:

import bokeh.plotting as bp
import numpy as np

x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

fig = bp.figure(title='Vertical Area Plot', x_axis_label='x', y_axis_label='y')
fig.varea(x=x, y1=y1, y2=y2, fill_color='green', line_color='black', line_alpha=0.8, fill_alpha=0.2, legend_label='sin(x) vs cos(x)')

bp.show(fig)

This code creates a vertical area plot of the sine and cosine functions with a legend label. The x values are generated using np.linspace(), and the y1 and y2 values are calculated using np.sin() and np.cos(), respectively. The fill_color and line_color are set to green and black, respectively, and the fill_alpha and line_alpha are set to 0.2 and 0.8, respectively. The plot is displayed using the show() function.

2. harea() Function:

harea() is a function in Bokeh that creates a horizontal area plot with filled regions between a central line and a lower line. It is similar to the varea() function, but it plots the data horizontally instead of vertically.

The harea() function takes several arguments:

  • y: The y-coordinates of the data points.
  • x1: The x-coordinates of the central line.
  • x2: The x-coordinates of the lower line.
  • fill_alpha (optional): The opacity of the filled regions. Default is 0.1.
  • line_alpha (optional): The opacity of the central line and the lower line. Default is 1.0.
  • line_color (optional): The color of the central line and the lower line. Default is ‘black’.
  • fill_color (optional): The color of the filled regions. Default is ‘blue’.
  • legend_label (optional): The label for the plot in the legend. Default is None.
  • source (optional): The source of data for the plot. Default is None.

Here’s an example of how to use harea() to create a horizontal area plot:

import bokeh.plotting as bp
import numpy as np

y = np.linspace(0, 2*np.pi, 100)
x1 = np.sin(y)
x2 = np.cos(y)

fig = bp.figure(title='Horizontal Area Plot', x_axis_label='x', y_axis_label='y')
fig.harea(y=y, x1=x1, x2=x2, fill_color='green', line_color='black', line_alpha=0.8, fill_alpha=0.2, legend_label='sin(y) vs cos(y)')

bp.show(fig)

This code creates a horizontal area plot of the sine and cosine functions with a legend label. The y values are generated using np.linspace(), and the x1 and x2 values are calculated using np.sin() and np.cos(), respectively. The fill_color and line_color are set to green and black, respectively, and the fill_alpha and line_alpha are set to 0.2 and 0.8, respectively. The plot is displayed using the show() function.

Conclusion:

In conclusion, Bokeh provides two functions varea() and harea() to create area plots in Python. varea() creates a vertical area plot with filled regions between a central line and a lower line, while harea() creates a horizontal area plot with filled regions between a central line and a lower line. Both functions take similar arguments, such as the data points, the color of the lines and the filled regions, and the opacity of the lines and the filled regions. These functions are useful for visualizing the area between two lines and can be used to compare two datasets.