Matplotlib - Filled Plots



A filled plot, also known as a filled area plot or an area chart, is a type of graph used to represent data visually. In a filled plot, the area between the data line and either the x-axis or another reference line is filled with color or pattern.

Imagine you are tracking the daily temperature fluctuations in your city over a month. You can create a filled plot to visualize the range of temperatures each day. On the x-axis, you have the days of the month, and on the y-axis, you have the temperature in degrees Celsius. Each day's temperature range is represented by a filled area, with the area between the highest and lowest temperatures filled with color −

Filled Plots

Filled Plots in Matplotlib

A filled plot in Matplotlib is like coloring between the lines on a graph. Instead of just showing points or lines, it fills in the area between those points or lines with color. This can help you see the shape of the data more clearly.

We can use the fill_between() function from the 'pyplot' module to create filled plots in Matplotlib. This function accepts the X and Y coordinates as arrays and fills a specific color in the area enclosed by the curves in a 2D space.

Lets start by drawing a basic filled plot.

Basic Filled Plot

A basic filled plot in Matplotlib is a visualization where the area under a line connecting data points is filled with color. This type of plot helps to visually interpret data and identify patterns or trends more easily.

Example

In the following example, we are creating a filled line plot. We generate the x-axis values ranging from 0 to 10 and then calculate the corresponding y-axis values by taking the sine of each x-axis value. Next, we fill the area under the line and above the x-axis with color, resulting in a filled line plot −

import matplotlib.pyplot as plt
import numpy as np

# Creating sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Creating a filled line plot
plt.fill_between(x, y, color='skyblue', alpha=0.4, label='Filled Line')

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Filled Plot')

# Displaying the plot
plt.show()

Output

Following is the output of the above code −

Basic Filled Plot

Filled Plot between Two Lines

A filled plot between two lines in Matplotlib refers to coloring the area between two lines on a graph. The two lines traverse the XY plane, and wherever they form an enclosed region, it is filled with color.

Example

In here, we are creating a filled plot between two lines in the XY plane. The two lines 'Y1' and 'Y2' are calculated by taking the sine and cosine values of the x-axis values, respectively. We then fill the area enclosed by the two lines to create the resultant filled plot between two lines −

import matplotlib.pyplot as plt
import numpy as np

# Creating sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Creating a filled plot between two lines
plt.fill_between(x, y1, y2, color='lightgreen', alpha=0.4, label='Filled Between Lines')

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Filled Plot between Two Lines')

# Displaying the plot
plt.show()

Output

Output of the above code is as follows −

Filled Plot between Two Lines

Example

In here, we are creating a filled plot between two vertical lines. We use the axvspan() function to add vertical span(rectangle) across the axes. This rectangle spans from xmin to xmax horizontally, and, by default, the whole Y-axis vertically −

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

line1 = 3 # vertical x = 3
line2 = 5 # vertical x = 5

ax.axvspan(line1, line2, alpha=.5, color='green')

plt.show()

Output

We get the output as shown below −

Filled Plot between Two Vertical Lines

Filled Polar Area Plot

In Matplotlib, a filled polar area plot represents a visualization where the area enclosed by a curve is filled with color. The curve is created in the polar coordinate system, meaning that the position of the data points is determined by the radius and angle instead of X and Y coordinates.

Example

The following example generates a filled polar area plot. The coordinates of the data points are defined by the angle (theta), ranging from 0 to 2, and the radius (r) of curve, which varies with the cosine of the angle. The area under the curve is filled with color. In the resultant plot, the polar area under the curve is filled −

import matplotlib.pyplot as plt
import numpy as np

# Creating sample data for a polar plot
theta = np.linspace(0, 2 * np.pi, 100)
r = 3 + 2 * np.cos(6 * theta)

# Creating a filled polar area plot
plt.fill_between(theta, r, color='purple', alpha=0.5, label='Filled Polar Area Plot')

# Adding title
plt.title('Filled Polar Area Plot')

# Displaying the plot
plt.show()

Output

After executing the above code, we get the following output −

Filled Polar Area Plot

Filled Plot from Irregular Data

In Matplotlib, a filled plot from irregular data is a way to represent a colored region on a graph, where the data points are irregularly spaced. The area enclosed by these points and the x-axis is filled with color.

Example

Now, we are filling the plot under a curve created using irregularly spaced data points on a graph. We define the data points using arrays for the x-axis and the y-axis. The region enclosed by these points is filled with color −

import matplotlib.pyplot as plt
import numpy as np

# Creating irregular data points
x = np.array([1, 2, 3, 4, 5, 4, 3, 2, 1])
y = np.array([0, 1, 0, 1, 0, -1, 0, -1, 0])

# Plotting the irregular data points
plt.plot(x, y, color='blue', label='Irregular Data Points', marker='o')

# Creating a polygon to fill between the curve and the x-axis
plt.fill_between(x, y, color='lightgreen', alpha=0.5, label='Filled Plot from Irregular Data')

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Filled Plot from Irregular Data')

# Displaying the plot
plt.show()

Output

The output obtained is as shown below −

Filled Plot from Irregular Data
Advertisements