
- 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 - Andrews Curves
Andrews Curves provide a way to visualize multivariate, high-dimensional data using smooth curves. Theses curves are generated based on a Fourier series, where the dataset attributes are treated as coefficients. By assigning different colors to these curves based on class labels, we can easily see clusters and identify patterns in the data. The curves representing similar classes appear closer together, helping us understand how data points relate to one another.
In this tutorial, we will learn how to create Andrews Curves in Python using Pandas library.
Pandas plotting.andrews_curves() Function
Pandas provides a direct function called pandas.plotting.andrews_curves() for generating Andrews curves. This function takes a DataFrame with multivariate data, a column with class labels, and other parameters for customization. This function returns a matplotlib.axes.Axes object representing the plot.
Mathematical Form of Andrews Curves
The function for Andrews Curves is based on Fourier series and represented as −
f(t)=x1/2+x2sin(t)+x3cos(t)+x4sin(2t)+x5cos(2t)+
Where,
x coefficients are the values of different attributes (features) for each data sample.
t is linearly spaced from - to +, which forms a continuous curve.
Syntax
Following is the syntax of the pandas.plotting.andrews_curves() function −
pandas.plotting.andrews_curves(frame, class_column, ax=None, samples=200, color=None, colormap=None, **kwargs)
Where,
frame: A DataFrame containing the data to be plotted.
class_column: The column name containing class labels. Curves are colored based on these labels.
ax: It is a optional parameter, by default it is set to None. It is a Matplotlib axes object where the plot will be drawn.
samples: Number of points for each curve, by default it is set to 200.
color: Specify the colors to use for different classes.
colormap: A colormap for selecting colors based on class labels.
Example
Here is the basic example of plotting the Andrews Curves for iris dataset using the pandas.plotting.andrews_curves() function.
import pandas as pd import matplotlib.pyplot as plt from pandas.plotting import andrews_curves # Load the Iris dataset url = 'https://raw.githubusercontent.com/pandas-dev/pandas/main/pandas/tests/io/data/csv/iris.csv' df = pd.read_csv(url) # Plot Andrews Curves, using the 'Name' column plt.figure(figsize=(7, 3)) andrews_curves(df, 'Name') plt.title("Andrews Curves for Iris Dataset") plt.show()
Following is the output of the above code −

Customizing Colors in Andrews Curves
We can customize the Andrews curves plot by specifying colors for each class or applying a Matplotlib colormap.
Example: Using Custom Colors
The following example demonstrates customize the Andrews curves plot by specifying custom colors to the pandas.plotting.andrews_curves() function.
import pandas as pd import numpy as np import matplotlib.pyplot as plt from pandas.plotting import andrews_curves # Create data df_random = pd.DataFrame(np.random.randn(100, 3), columns=['Feature1', 'Feature2', 'Feature3']) df_random['Category'] = np.random.choice(['Class1', 'Class2', 'Class3'], size=100) # Plot Andrews Curves for the random data plt.figure(figsize=(7, 4)) andrews_curves(df_random, 'Category', color=['red', 'blue', 'green']) plt.title("Andrews Curves with Custom Colors") plt.show()
Following is the output of the above code −

Example: Applying a Colormap
The following example demonstrates customize the Andrews curves plot by specifying custom colors to the pandas.plotting.andrews_curves() function.
import pandas as pd import numpy as np import matplotlib.pyplot as plt from pandas.plotting import andrews_curves # Create data df_random = pd.DataFrame(np.random.randn(100, 3), columns=['Feature1', 'Feature2', 'Feature3']) df_random['Category'] = np.random.choice(['Class1', 'Class2', 'Class3'], size=100) # Plot Andrews Curves for the random data plt.figure(figsize=(7, 4)) andrews_curves(df_random, 'Category', colormap='viridis') plt.title("Andrews Curves with Colormap") plt.show()
Following is the output of the above code −
