Python Pandas - RadViz Plot



A RadViz plot is a visualization technique that projects high-dimensional data into a 2D space. It works based on a spring-tension minimization algorithm to determine the position of data points. This plot is particularly useful for visualizing relationships between multiple variables and understanding the clusters or groups of data categories.

The RadViz plot in Pandas is a powerful tool for visualizing high-dimensional data in a two-dimensional space. By using its customization options, you can create clear and visually attractive plots to analyze relationships and patterns in multi-variate datasets. In this tutorial, we will learn how to use Python's Pandas library to create RadViz plot and customize it for effective visual analysis.

RadViz Plot in Pandas

Pandas provides a direct function called radviz() function within the plotting module for generating RadViz plot. This function takes a DataFrame with multi-variate data, a column with class labels, and other parameters for customization. And this function returns a matplotlib.axes.Axes object representing the plot.

Each attribute (or column or Series) in the DataFrame is represented as a evenly distributed on a unit circle. Each data point is rendered in the circle according to the normalized values of their attributes (nothing but all numerical columns in the DataFrame are normalized to unit [0,1] interval), resulting in a balanced visualization.

Syntax

Following is the syntax of the plotting.radviz() function −

pandas.plotting.radviz(frame, class_column, ax=None, color=None, colormap=None, **kwds)

Where,

  • frame: The DataFrame containing the data you want to visualize.

  • class_column: The column name that contains the categorical labels or categories for each data point.

  • ax

    : A Matplotlib Axes object where the plot will be drawn. If not specified, a new plot will be created.
  • color: A list or tuple of colors to assign to each category.

  • colormap: A string or a Matplotlib colormap object used to color the plot points.

  • **kwds: Additional arguments passed to the Matplotlib scatter method.

Creating a Simple RadViz Plot

To create a basic RadViz plot, you can use a DataFrame with several columns and one column as the target category (for coloring).

Example

Here is a simple example of creating a RadViz plot using the plotting.radviz() function.

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

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

# Sample DataFrame
data = {'Company': ['IBM', 'Intel', 'Microsoft', 'Apple', 'Google', 
                'IBM', 'Intel', 'Microsoft', 'Apple', 'Google'],
'Revenue': np.random.rand(10) * 100,
'Profit': np.random.rand(10) * 50,
'Market_Cap': np.random.rand(10) * 200,
'Employees': np.random.rand(10) * 10
}

df = pd.DataFrame(data)

# Create a RadViz plot
pd.plotting.radviz(df, 'Company')

# Show the plot
plt.title('Basic RadViz Plot')
plt.show()

Following is the output of the above code −

Basic RadViz Plot

Customizing the RadViz Plot

You can enhance the appearance of the RadViz plot by changing the colors using the color and colormap parameters.

Example: Assigning Custom Colors to the RadViz Plot

This example applies the custom colors to the RadViz plot by using the color parameter of the plotting.radviz() function.

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

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

# Sample DataFrame
data = {'Company': ['IBM', 'Intel', 'Microsoft', 'Apple', 'Google', 
                'IBM', 'Intel', 'Microsoft', 'Apple', 'Google'],
'Revenue': np.random.rand(10) * 100,
'Profit': np.random.rand(10) * 50,
'Market_Cap': np.random.rand(10) * 200,
'Employees': np.random.rand(10) * 10
}

df = pd.DataFrame(data)

# Create a RadViz plot
pd.plotting.radviz(df, 'Company', color=['red', 'green', 'blue'])

# Show the plot
plt.title('RadViz Plot with Custom Colors')
plt.show()

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

RadViz Plot with Custom Colors

Example: RadViz Plot using a Colormap

This example customizes the Radviz plot by assigning a colormap name to the colormap parameter of the plotting.radviz() function.

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

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

# Sample DataFrame
data = {'Company': ['IBM', 'Intel', 'Microsoft', 'Apple', 'Google', 
                'IBM', 'Intel', 'Microsoft', 'Apple', 'Google'],
'Revenue': np.random.rand(10) * 100,
'Profit': np.random.rand(10) * 50,
'Market_Cap': np.random.rand(10) * 200,
'Employees': np.random.rand(10) * 10
}

df = pd.DataFrame(data)

# Create a RadViz plot
pd.plotting.radviz(df, 'Company', colormap='viridis')
plt.title('RadViz with Colormap')
plt.show()

Following is the output of the above code −

RadViz Plot with Colormap
Advertisements