Home > Enterprise >  Create a radar chart for every index position in df
Create a radar chart for every index position in df

Time:11-27

my dataframe looks similia to this:

df = pd.DataFrame({"Name": ["A", "B", "C", "D", "E","F", "G"],
                    "Tldiffto3": [2.0, -3.0, 2.0,17.0,7.0, 
                      12.0,2.0],
                    "LZdiffto3": [0.94, -5.0, 7.0,29.0,4.0, 
                      10.0,2.5],
                    "SZdiffto3": [0.94, -5.0, 7.0,29.0,4.0, 
                      10.0,2.5],
                    "KZdiffto3": [0.8, -9.0, 10.0,8.0,3.0, 
                      11.0,4.5]})

The following code creates a single radar chart:

# number of variables
categories=list(df)[0:]

N = len(categories)
# create angle by dividing the plot / number of variable
angles = [n / float(N) * 2 * pi for n in range(N)]
angles  = angles[:1]

values = 
df.loc[df.index[0]].values.flatten().tolist()
values  = values[:1]

fig, ax = plt.subplots(figsize=(6, 6), 
          subplot_kw=dict(polar=True), dpi = 100)

   # Draw the outline of the data.
  ax.plot(angles, values, color='#1aaf6c', linewidth=1, 
        )
  # add the fill in  
  ax.fill(angles, values, color='#1aaf6c', alpha=0.25) 

# Fix axis to go in the right order and start at 12 o'clock.
ax.set_theta_offset(np.pi / 2)
ax.set_theta_direction(-1)

# axis per variable   add labels
plt.xticks(angles[:-1], categories)

# Go through labels   adjust alignment based position
for label, angle in zip(ax.get_xticklabels(), angles):
  if angle in (0, np.pi):
    label.set_horizontalalignment('center')
  elif 0 < angle < np.pi:
    label.set_horizontalalignment('left')
  else:
    label.set_horizontalalignment('right')

# Set position of y-labels to center
# of the first two axes.
ax.set_rlabel_position(180 / N)

# Add  styling.
# color tick labels.
ax.tick_params(colors='#222222')
#  y-axis  labels smaller.
ax.tick_params(axis='y', labelsize=8)
#color of the circular gridlines.
ax.grid(color='#AAAAAA')
# color of the spine
ax.spines['polar'].set_color('#222222')
# background color inside the circle 
ax.set_facecolor('#FAFAFA')

Now I want to loop through the index and create a new subplot with a radar chart for each index i.E the name:

for i in range(df.index):
  ax.plot(angles, values, color='#1aaf6c', linewidth=1) 
  ax.fill(angles, values, color='#1aaf6c', alpha=0.25) 

this did not work, not sure where the problem ist, would appreciate an explanation :)

CodePudding user response:

The issue is it overwrites the plots each time unless you create new subplots. I've also used fig.tight_layout() to avoid overlapping. You can adjust nrows or ncolumns for a better view:

fig, ax = plt.subplots(nrows=len(df), ncols=1, figsize=(20, 12), 
              subplot_kw=dict(polar=True), dpi = 100)
fig.tight_layout()

for i in range(len(df)):
    values =  df.loc[df.index[i]].values.flatten().tolist()
    values  = values[:1]

     # Draw the outline of the data.
    ax[i].plot(angles, values, color='#1aaf6c', linewidth=1, 
          )
    # add the fill in  
    ax[i].fill(angles, values, color='#1aaf6c', alpha=0.25) 

    # Fix axis to go in the right order and start at 12 o'clock.
    ax[i].set_theta_offset(np.pi / 2)
    ax[i].set_theta_direction(-1)

Output:

radial_plots

  • Related