Parallel coordinates chart
Parallel coordinates chart is a graph used to plot individual data points parallel to different performance measures. It allows plotting high-dimensional data into 2-dimensional plots. The data lines are plotted parallel to the x-axis to show other trends in data.
pandas.plotting.parallel_coordinates()
This method is used for parallel coordinates plotting in pandas. It takes different argument values and returns matplotlib.axis.Axes
object.
Syntax
#Signature
pandas.plotting.parallel_coordinates(frame,
class_column,
cols=None,
ax=None,
color=None,
use_columns=False,
xticks=None,
colormap=None,
axvlines=True,
axvlines_kwds=None,
sort_labels=False,
**kwargs)
Parameters
It takes multiple argument values but here are listed some mostly used argument values.
frame
: DataFrame as an argument.class_column
: A string that shows the column name which contains class names.cols
: Pandas list which contains column names. The default value is None.ax
: It shows thematplotlib axis
instance or object.color
: It can be a list, tuple, or None. It shows the colors which can be used to differentiate classes.use_columns
: If it’s set to true the column can be used asxticks
.xticks
: It shows a list or a tuple of values forxticks
.
Return value
it returns matplotlib.axis.Axes
object.
Explanation
In this section, we’ll implement pandas.plotting.parallel_coordinates()
function to plot a parallel coordinate chart.
main.py
# load basic libraries
import pandas as pd
import matplotlib.pyplot as plt
# load employee.csv file as DataFrame
df = pd.read_csv("employee.csv")
# invoking method for plot
pd.plotting.parallel_coordinates( df, 'Name', color=('#556270', '#4ECDC4', '#C7F464'))
# save output as PNG file in output directory
plt.savefig("output/graph.png")
- Line#2-3: Import pandas and matplotlib.pyplot libraries in the program.
- Line#5:
pd.read_csv("employee.csv")
will read and employee.csv file data and return it as a Python DataFrame. - Line#7:
pd.plotting.parallel_coordinates( df, 'Name', color=('#556270', '#4ECDC4', '#C7F464'))
will draw a parallel coordinate plot on runtime. - Line#9:
plt.savefig("output/graph.png")
will save the graph as a PNG file in the output directory.
employee.csv
Here, we have a CSV file named employee.csv
record including name, medical expenses, bonus, and total.
Name,Medical Expenses,Bonus,TOTAL
Raj,1250,13100,14350
Sharad,1250,2300,3550
Danish,1250,0,1250
Pawan,1250,0,1250
Rijo Paul,1250,2300,3550
Joseph,1250,2300,3550
Aakash,1200,0,1200
Ganesh,1200,0,1200
Vinudas,1250,1500,2750
Divya,800,0,800
Joseph,774,0,774
Sindhu,800,0,800
Deepthi,749,0,749
Lijin,1000,2000,3000
Output
