Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python Pandas - Area Plot



An Area Plot, also known as an area chart or area graph, is a visualization tool that graphically represents quantitative data. It is an extension of a line chart where the area between the line and the axis is filled with colors, textures, or patterns. Area plots are commonly used to compare two or more quantities visually.

Pandas provides an easy way to create stacked and unstacked area plots using the area() method. In this tutorial, we will learn about how to create and customize area plots using the Pandas library, with multiple examples demonstrating different plotting options.

Area Plot in Pandas

In Pandas, area plots can be created using the area() method for both the Series and DataFrames objects. This method resulting an area plot matplotlib.axes.Axes or an array numpy.ndarray of plots if subplots are enabled. It wraps the Matplotlib area() function.

Syntax

Following is the syntax of the area() method −

DataFrame.plot.area(x=None, y=None, stacked=True, **kwargs)

Where,

  • x: Specifies the X-axis coordinates. By defaults it is uses the index.

  • y: Specifies the column to plot. Defaults to all columns.

  • stacked: It takes a boolean value. If True, creates a stacked area plot. Set to False for unstacked plots.

  • **kwargs: Additional arguments to customize the plot.

Stacked Area Plot

In Pandas Area plots are stacked by default. To produce stacked area plot correctly, all column values must either be entirely positive or entirely negative.

Example

This example creates a stacked area plot with the sum of all columns plotted at each x-coordinate using the DataFrame.plot.area() method.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7, 4]

# Create a DataFrame with random data
df = pd.DataFrame(np.random.rand(10, 3), columns=["a", "b", "c"])

# Generate a stacked area plot
df.plot.area()
plt.title("Stacked Area Plot")
plt.show()

Following is the output of the above code −

Stacked Area Plot

Unstacked Area Plot

To create an unstacked area plot, set the stacked parameter to False. Which is useful for comparing the columns independently.

Example

This example plots the unstacked area plot by using the DataFrame.plot.area() method. Here, the alpha parameter is used to adjust the transparency of the areas.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7, 4]

# Create a DataFrame with random data
df = pd.DataFrame(np.random.rand(10, 3), columns=["a", "b", "c"])

# Generate an unstacked area plot
df.plot.area(stacked=False, alpha=0.5)
plt.title("Unstacked Area Plot")
plt.show()

On executing the above code we will get the following output −

Unstacked Area Plot

Handling Missing Data

If your data contains NaN values, Pandas will automatically fill these with 0. To handle missing data differently, use methods like fillna() or dropna() before plotting.

Example

This example demonstrates handling the missing data before plotting the area plot.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Create a DataFrame with random data
df = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])

# Add some NaN values
df.iloc[2:4, 1] = np.nan

# Fill NaN with a custom value
df.fillna(0).plot.area(stacked=True)
plt.title("Area Plot with NaN Values Filled")
plt.show()

Following is the output of the above code −

Area Plot with Missing Data

Customizing Area Plots

You can customize area plots using the various parameters available in Matplotlib, such as colors, colormap, labels, and gridlines.

Example

This example customizes the area plot in Pandas using the additional keyword arguments.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7, 4]

# Create a DataFrame with random data
df = pd.DataFrame(np.random.rand(10, 3), columns=["a", "b", "c"])

# Customize area plot
df.plot.area(colormap="Greens", figsize=(10, 6))
plt.title("Customized Area Plot")
plt.xlabel("Index")
plt.ylabel("Values")
plt.show()

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

Customized Area Plot
Advertisements