Creating a polar plot for visualizing clusters with Matplotlib

python matplotlib visualization polar-plot
Apr 22, 202304:12 PM367 views
0

Problem

A [polar plot](https://en.wikipedia.org/wiki/Polar_coordinate_system) can be used for visualizing high-dimensional data. ![polar plot](https://i.imgur.com/dl2kSC5.png) One particularly useful way to use a polar plot is for visualizing clusters. Demonstrate how to make a polar plot in Python using Matplotlib. In particular, use the Iris and Wine data available from Scikit. ```python from sklearn.datasets import load_iris from sklearn.datasets import load_wine def get_wine(): X, y = load_wine(return_X_y=True, as_frame=True) Xy = X.assign(y=y) return Xy def get_iris(): X, y = load_iris(return_X_y=True, as_frame=True) Xy = X.assign(y=y) \ .rename(columns={ 'sepal length (cm)': 's_length', 'sepal width (cm)': 's_width', 'petal length (cm)': 'p_length', 'petal width (cm)': 'p_width' }) return Xy ``` A snippet of the Iris data looks like the following. ``` | | s_length | s_width | p_length | p_width | y | |---:|-----------:|----------:|-----------:|----------:|----:| | 0 | 5.1 | 3.5 | 1.4 | 0.2 | 0 | | 1 | 4.9 | 3 | 1.4 | 0.2 | 0 | | 2 | 4.7 | 3.2 | 1.3 | 0.2 | 0 | | 3 | 4.6 | 3.1 | 1.5 | 0.2 | 0 | | 4 | 5 | 3.6 | 1.4 | 0.2 | 0 | ``` A snippet of the wine data looks like the following. ``` | | alcohol | malic_acid | ash | alcalinity_of_ash | magnesium | total_phenols | flavanoids | nonflavanoid_phenols | |---:|----------:|-------------:|------:|--------------------:|------------:|----------------:|-------------:|-----------------------:| | 0 | 14.23 | 1.71 | 2.43 | 15.6 | 127 | 2.8 | 3.06 | 0.28 | | 1 | 13.2 | 1.78 | 2.14 | 11.2 | 100 | 2.65 | 2.76 | 0.26 | | 2 | 13.16 | 2.36 | 2.67 | 18.6 | 101 | 2.8 | 3.24 | 0.3 | | 3 | 14.37 | 1.95 | 2.5 | 16.8 | 113 | 3.85 | 3.49 | 0.24 | | 4 | 13.24 | 2.59 | 2.87 | 21 | 118 | 2.8 | 2.69 | 0.39 | ```
Menu