GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Python Pandas - Area Plot

Python Pandas Tutorial
  • Python Pandas - Home
  • Python Pandas - Introduction
  • Python Pandas - Environment Setup
  • Python Pandas - Basics
  • Python Pandas - Introduction to Data Structures
  • Python Pandas - Index Objects
  • Python Pandas - Panel
  • Python Pandas - Basic Functionality
  • Python Pandas - Indexing & Selecting Data
  • Python Pandas - Series
  • Python Pandas - Series
  • Python Pandas - Slicing a Series Object
  • Python Pandas - Attributes of a Series Object
  • Python Pandas - Arithmetic Operations on Series Object
  • Python Pandas - Converting Series to Other Objects
  • Python Pandas - DataFrame
  • Python Pandas - DataFrame
  • Python Pandas - Accessing DataFrame
  • Python Pandas - Slicing a DataFrame Object
  • Python Pandas - Modifying DataFrame
  • Python Pandas - Removing Rows from a DataFrame
  • Python Pandas - Arithmetic Operations on DataFrame
  • Python Pandas - IO Tools
  • Python Pandas - IO Tools
  • Python Pandas - Working with CSV Format
  • Python Pandas - Reading & Writing JSON Files
  • Python Pandas - Reading Data from an Excel File
  • Python Pandas - Writing Data to Excel Files
  • Python Pandas - Working with HTML Data
  • Python Pandas - Clipboard
  • Python Pandas - Working with HDF5 Format
  • Python Pandas - Comparison with SQL
  • Python Pandas - Data Handling
  • Python Pandas - Sorting
  • Python Pandas - Reindexing
  • Python Pandas - Iteration
  • Python Pandas - Concatenation
  • Python Pandas - Statistical Functions
  • Python Pandas - Descriptive Statistics
  • Python Pandas - Working with Text Data
  • Python Pandas - Function Application
  • Python Pandas - Options & Customization
  • Python Pandas - Window Functions
  • Python Pandas - Aggregations
  • Python Pandas - Merging/Joining
  • Python Pandas - MultiIndex
  • Python Pandas - Basics of MultiIndex
  • Python Pandas - Indexing with MultiIndex
  • Python Pandas - Advanced Reindexing with MultiIndex
  • Python Pandas - Renaming MultiIndex Labels
  • Python Pandas - Sorting a MultiIndex
  • Python Pandas - Binary Operations
  • Python Pandas - Binary Comparison Operations
  • Python Pandas - Boolean Indexing
  • Python Pandas - Boolean Masking
  • Python Pandas - Data Reshaping & Pivoting
  • Python Pandas - Pivoting
  • Python Pandas - Stacking & Unstacking
  • Python Pandas - Melting
  • Python Pandas - Computing Dummy Variables
  • Python Pandas - Categorical Data
  • Python Pandas - Categorical Data
  • Python Pandas - Ordering & Sorting Categorical Data
  • Python Pandas - Comparing Categorical Data
  • Python Pandas - Handling Missing Data
  • Python Pandas - Missing Data
  • Python Pandas - Filling Missing Data
  • Python Pandas - Interpolation of Missing Values
  • Python Pandas - Dropping Missing Data
  • Python Pandas - Calculations with Missing Data
  • Python Pandas - Handling Duplicates
  • Python Pandas - Duplicated Data
  • Python Pandas - Counting & Retrieving Unique Elements
  • Python Pandas - Duplicated Labels
  • Python Pandas - Grouping & Aggregation
  • Python Pandas - GroupBy
  • Python Pandas - Time-series Data
  • Python Pandas - Date Functionality
  • Python Pandas - Timedelta
  • Python Pandas - Sparse Data Structures
  • Python Pandas - Sparse Data
  • Python Pandas - Visualization
  • Python Pandas - Visualization
  • Python Pandas - Additional Concepts
  • Python Pandas - Caveats & Gotchas
  • Python Pandas Useful Resources
  • Python Pandas - Quick Guide
  • Python Pandas - Cheatsheet
  • Python Pandas - Useful Resources
  • Python Pandas - Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Python Pandas - Area Plot



Previous
Quiz
Next

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()

Output

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()

Output

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()

Output

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()

Output

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

Customized Area Plot
python_pandas_visualization.htm
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.