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>
png

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>
png

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
speciesunitmeasurementvalue
0setosa0sepal_length5.1
1setosa1sepal_length4.9
2setosa2sepal_length4.7
3setosa3sepal_length4.6
4setosa4sepal_length5.0
595virginica45petal_width2.3
596virginica46petal_width1.9
597virginica47petal_width2.0
598virginica48petal_width2.3
599virginica49petal_width1.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)
png

Categories: Python

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *