Make their edges look like polygons, don't make them round import pandas as pd import…
- User
Make their edges look like polygons, don't make them round import pandas as pd import numpy as np import matplotlib.pyplot as plt from math import pi
Load the data
data = { 'age': [35, 20, 45, 45, 20], 'height(cm)': [170, 175, 155, 165, 165], 'weight(kg)': [85, 110, 65, 80, 60], 'waist(cm)': [97.0, 110.0, 86.0, 94.0, 81.0], 'eyesight(left)': [0.9, 0.7, 0.9, 0.8, 1.5], 'eyesight(right)': [0.9, 0.9, 0.9, 0.7, 0.1], 'hearing(left)': [1, 1, 1, 1, 1], 'hearing(right)': [1, 1, 1, 1, 1], 'systolic': [118, 119, 110, 158, 109], 'relaxation': [78, 79, 80, 88, 64], 'HDL': [70, 71, 57, 46, 47], 'LDL': [142, 114, 112, 91, 92], 'hemoglobin': [19.8, 15.9, 13.7, 16.9, 14.9], 'Urine protein': [1, 1, 3, 1, 1], 'serum creatinine': [1.0, 1.1, 0.6, 0.9, 1.2], 'AST': [61, 19, 1090, 32, 26], 'ALT': [115, 25, 1400, 36, 28], 'Gtp': [125, 30, 276, 36, 15], 'dental caries': [1, 1, 0, 0, 0], 'smoking': [1, 0, 0, 0, 0] }
df = pd.DataFrame(data)
Select the first observation (index 0)
categories = list(df.columns) values = df.loc[1].values.flatten().tolist() values += values[:1]
Number of variables
num_vars = len(categories)
Compute angle for each category
angles = [n / float(num_vars) * 2 * pi for n in range(num_vars)] angles += angles[:1]
Initialize radar chart
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw=dict(polar=True))
Draw one axe per variable + add labels
plt.xticks(angles[:-1], categories, color='grey', size=12, fontweight='bold')
ylabels
ax.set_rlabel_position(0) plt.yticks([20, 40, 60, 80, 100, 120, 140, 160, 180, 200], ["20", "40", "60", "80", "100", "120", "140", "160", "180", "200"], color="grey", size=10) plt.ylim(0, 200)
Plot data
ax.plot(angles, values, linewidth=2, linestyle='-', color='blue', marker='o', markersize=5)
Fill area
ax.fill(angles, values, 'blue', alpha=0.4)
Title
plt.title('Health Metrics Radar Chart', size=20, color='blue', y=1.1)
Add a grid
ax.grid(color='grey', linestyle='--', linewidth=0.5)
Customize the color of the chart
ax.spines['polar'].set_visible(False)
plt.show()
- Assistant
To change the edges of the radar chart from round to a polygon-like shape, you can use the
path_effectsmodule frommatplotlibto manipulate the appearance of the lines. By setting thesolid_joinstyleandsolid_capstyleproperties tomiter, you can achieve sharp edges.Here is the modified code:
import pandas as pd import numpy as np import matplotlib.pyplot as plt from math import pi import matplotlib.patheffects as path_effects # Load the data data = { 'age': [35, 20, 45, 45, 20], 'height(cm)': [170, 175, 155, 165, 165], 'weight(kg)': [85, 110, 65, 80, 60], 'waist(cm)': [97.0, 110.0, 86.0, 94.0, 81.0], 'eyesight(left)': [0.9, 0.7, 0.9, 0.8, 1.5], 'eyesight(right)': [0.9, 0.9, 0.9, 0.7, 0.1], 'hearing(left)': [1, 1, 1, 1, 1], 'hearing(right)': [1, 1, 1, 1, 1], 'systolic': [118, 119, 110, 158, 109], 'relaxation': [78, 79, 80, 88, 64], 'HDL': [70, 71, 57, 46, 47], 'LDL': [142, 114, 112, 91, 92], 'hemoglobin': [19.8, 15.9, 13.7, 16.9, 14.9], 'Urine protein': [1, 1, 3, 1, 1], 'serum creatinine': [1.0, 1.1, 0.6, 0.9, 1.2], 'AST': [61, 19, 1090, 32, 26], 'ALT': [115, 25, 1400, 36, 28], 'Gtp': [125, 30, 276, 36, 15], 'dental caries': [1, 1, 0, 0, 0], 'smoking': [1, 0, 0, 0, 0] } df = pd.DataFrame(data) # Select the first observation (index 0) categories = list(df.columns) values = df.loc[1].values.flatten().tolist() values += values[:1] # Number of variables num_vars = len(categories) # Compute angle for each category angles = [n / float(num_vars) * 2 * pi for n in range(num_vars)] angles += angles[:1] # Initialize radar chart fig, ax = plt.subplots(figsize=(10, 10), subplot_kw=dict(polar=True)) # Draw one axe per variable + add labels plt.xticks(angles[:-1], categories, color='grey', size=12, fontweight='bold') # ylabels ax.set_rlabel_position(0) plt.yticks([20, 40, 60, 80, 100, 120, 140, 160, 180, 200], ["20", "40", "60", "80", "100", "120", "140", "160", "180", "200"], color="grey", size=10) plt.ylim(0, 200) # Plot data line, = ax.plot(angles, values, linewidth=2, linestyle='-', color='blue', marker='o', markersize=5) line.set_path_effects([path_effects.withStroke(linewidth=5, foreground='blue', alpha=0.4)]) # Fill area ax.fill(angles, values, 'blue', alpha=0.4, path_effects=[path_effects.withStroke(linewidth=5, foreground='blue', alpha=0.4)]) # Title plt.title('Health Metrics Radar Chart', size=20, color='blue', y=1.1) # Add a grid ax.grid(color='grey', linestyle='--', linewidth=0.5) # Customize the color of the chart ax.spines['polar'].set_visible(False) plt.show()With these changes, the edges where the lines meet on the radar chart should appear more angular, resembling polygons rather than curves.