parallel coordinates 图做法
Parallel coordinates are a common way of visualizing high-dimensional geometry and analyzing multivariate data.
To show a set of points in an n-dimensional space, a backdrop is drawn consisting of n parallel lines, typically vertical and equally spaced. A point in n-dimensional space is represented as a polyline with vertices on the parallel axes; the position of the vertex on the i-th axis corresponds to the i-th coordinate of the point.
举例:三种方法
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import warnings
import numpy as np
warnings.filterwarnings('ignore')
sns.set(style="ticks")
pandas.plotting.parallel_coordinates
iris = sns.load_dataset("iris")
print(iris.head())
pd.plotting.parallel_coordinates(iris,'species',color=sns.color_palette("Set2"),alpha=0.3)
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
<matplotlib.axes._subplots.AxesSubplot at 0x1a5c6345808>

matplotlib.pyplot
fig,ax=plt.subplots()
labels=dict(zip(iris['species'].unique(),sns.color_palette("Set2")[0:3]))
for i in iris.index:
ax=plt.plot(iris.iloc[i,:-1],c=labels.get(iris.iloc[i,-1]),alpha=0.3,)
plt.legend(labels)
<matplotlib.legend.Legend at 0x1a5c92af1c8>

seaborn.lineplot
grouped=iris.groupby('species')
res=[]
for i in grouped:
i[1].loc[:,'unit']=np.arange(i[1].shape[0])
res.append(i[1])
iris_df=pd.concat([i for i in res],axis=0 )
iris_df = pd.melt(iris_df, id_vars=["species","unit"],var_name="measurement")
iris_df
species | unit | measurement | value | |
---|---|---|---|---|
0 | setosa | 0 | sepal_length | 5.1 |
1 | setosa | 1 | sepal_length | 4.9 |
2 | setosa | 2 | sepal_length | 4.7 |
3 | setosa | 3 | sepal_length | 4.6 |
4 | setosa | 4 | sepal_length | 5.0 |
… | … | … | … | … |
595 | virginica | 45 | petal_width | 2.3 |
596 | virginica | 46 | petal_width | 1.9 |
597 | virginica | 47 | petal_width | 2.0 |
598 | virginica | 48 | petal_width | 2.3 |
599 | virginica | 49 | petal_width | 1.8 |
600 rows × 4 columns
ax = sns.lineplot(x="measurement", y="value", hue="species",ci=None,units='unit',estimator=None,alpha=0.3,
data=iris_df)

0 Comments