Jonathan
Jonathan

Reputation: 1936

Reversing the r-axis on a radar chart matplotlib

I am trying to make a radar chart using this code :

import matplotlib.pyplot as plt
import numpy as np
import sys
import pandas as pd
from math import pi
import seaborn as sns
import cv2


tab1 = np.random.rand(21)

#Experiment
tab1 =[(-10)*x for x in tab1]

# ------- PART 1: Create background

# number of variable
cell_lines = range(0,22)
N = len(range)
# What will be the angle of each axis in the plot? (we divide the plot / number of variable)
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:0]

# Initialise the spider plot
ax = plt.subplot(111, polar=True)

# If you want the first axis to be on top:
ax.set_theta_offset(pi / 6)
ax.set_theta_direction(-1)

# Draw one axe per variable + add labels labels yet

plt.xticks(angles[0:], cell_lines, fontsize=20,fontname="Arial",fontweight='bold',linespacing=0)
x=(3,6,9)
plt.yticks(x,fontsize=20, fontweight='bold',fontname="Arial",linespacing=0)

# Draw ylabels
ax.set_rlabel_position(0)
#plt.ylabel("%", fontsize=16)
#plt.title("STUFF", fontsize=20, fontweight='bold', linespacing=2)

# ------- PART 2: Add plots

# Plot each individual = each line of the data
# I don't do a loop, because plotting more than 3 groups makes the chart unreadable
#print(control)
# Ind1
#values=df.loc[0].drop('control').values.flatten().tolist()
#values += values[:1]
#ax.plot(angles, control, linewidth=5, linestyle='solid', label="control")
#ax.fill(angles, control, 'b', alpha=0.1)

ax.plot(angles, tab1, linewidth=5, linestyle='solid', label="tab1", color='blue')
ax.fill(angles, tab1, 'b', alpha=0.1)


# Add legend
plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1), fontsize=20)
plt.show()

With this code, I'd like to be able to reverse the r-axis so that the outer most circle is 0 and the inner most circle is 1. I have no idea how to proceed about doing this. Even when I multiplied the entire data set by -1 and then graphed it (as I did below the experiment comment), the data only looked more skewed and it was missing the inner circles all together. Is there a straightforward way in which I can just take the normal graph and reverse the axis?

Upvotes: 0

Views: 529

Answers (1)

Davide Fiocco
Davide Fiocco

Reputation: 5914

A starting point following your approach should be:

#test with a more eye-friendly dataset
tab1 = np.linspace(0,1,11)

#Experiment
tab1 = -tab1 + 1

N = len(tab1)

angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:0]

# Initialise the spider plot
ax = plt.subplot(111, polar=True)

ax.set_theta_offset(pi / 6)
ax.set_theta_direction(-1)

plt.xticks(angles[0:], tab1)
r=(0.3, 0.6, 0.9)
plt.yticks(r)

ax.set_rlabel_position(0)
ax.plot(angles, tab1, linewidth=5, linestyle='solid', color='blue')

plt.show()

with the "experiment" being a multiplication by -1 and the addition of 1 to shift back the values to the [0,1] interval (if you don't shift the data, the tuple that you indicated as x might be out of the range of the datapoints in tab1).

EDIT: You might also want to check improvements to the polar plot introduced in Matplotlib 2.1

Upvotes: 1

Related Questions