Skip to content

Instantly share code, notes, and snippets.

@geberl
Created September 21, 2018 09:58
Show Gist options
  • Save geberl/c65517bf8273552486f9a8954e80ddf4 to your computer and use it in GitHub Desktop.
Save geberl/c65517bf8273552486f9a8954e80ddf4 to your computer and use it in GitHub Desktop.
A 12-axis radar chart in Python (conda/miniconda) via numpy/pandas/matplotlib
#!/usr/bin/python
"""
Installation: Get the *Miniconda* Python Distribution - not the Python distrubution from python.org!
- https://conda.io/miniconda.html
Then install modules:
- `cd ~/miniconda3/bin`
- `./conda install numpy pandas matplotlib`
Original source:
- https://stackoverflow.com/questions/24659005/radar-chart-with-multiple-scales-on-multiple-axes
- That code has problems with 5+ axes though
"""
import numpy as np
import matplotlib.pyplot as plt
# Optionally use different styles for the graph
# Gallery: http://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html
# import matplotlib
# matplotlib.style.use('dark_background') # interesting: 'bmh' / 'ggplot' / 'dark_background'
class Radar(object):
def __init__(self, figure, title, labels, rect=None):
if rect is None:
rect = [0.05, 0.05, 0.9, 0.9]
self.n = len(title)
self.angles = np.arange(0, 360, 360.0/self.n)
self.axes = [figure.add_axes(rect, projection='polar', label='axes%d' % i) for i in range(self.n)]
self.ax = self.axes[0]
self.ax.set_thetagrids(self.angles, labels=title, fontsize=14)
for ax in self.axes[1:]:
ax.patch.set_visible(False)
ax.grid(False)
ax.xaxis.set_visible(False)
for ax, angle, label in zip(self.axes, self.angles, labels):
ax.set_rgrids(range(1, 6), angle=angle, labels=label)
ax.spines['polar'].set_visible(False)
ax.set_ylim(0, 5)
def plot(self, values, *args, **kw):
angle = np.deg2rad(np.r_[self.angles, self.angles[0]])
values = np.r_[values, values[0]]
self.ax.plot(angle, values, *args, **kw)
if __name__ == '__main__':
fig = plt.figure(figsize=(8, 8))
tit = list('ABCDEFGHIJKJ') # 12x
lab = [
list('abcde'),
list('12345'),
list('uvwxy'),
['one', 'two', 'three', 'four', 'five'],
list('jklmn'),
list('jklmn'),
list('jklmn'),
list('jklmn'),
list('jklmn'),
list('jklmn'),
list('jklmn'),
list('jklmn')
]
radar = Radar(fig, tit, lab)
radar.plot([1, 3, 2, 1, 3, 1, 1, 1, 1, 2, 3, 1], '-', lw=2, color='b', alpha=0.4, label='first')
radar.plot([2, 2, 3, 3, 3, 2, 2, 2, 0, 2, 3, 2], '-', lw=2, color='r', alpha=0.4, label='second')
radar.plot([3, 4, 3, 1, 2, 2, 4, 3, 1, 2, 3, 3], '-', lw=2, color='g', alpha=0.4, label='third')
radar.ax.legend()
fig.savefig('result.png')
@rajan3452
Copy link

Hi Claartje,

I was trying to plot a multiple graph and i have attached the script for your perusal. after adding the lines what you have suggested i am getting an error ....I am a beginner and finding it very difficult to correct it, any help in this regards will be highly appreciated

File "2_radar.py", line 47
for ax, angle, limit in zip(self.axes, self.angles,self.limits):
#!/usr/bin/python

"""
Installation: Get the Miniconda Python Distribution - not the Python distrubution from python.org!

Then install modules:

  • cd ~/miniconda3/bin
  • ./conda install numpy pandas matplotlib

Original source:

import numpy as np
import matplotlib.pyplot as plt

Optionally use different styles for the graph

Gallery: http://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html

import matplotlib

matplotlib.style.use('dark_background') # interesting: 'bmh' / 'ggplot' / 'dark_background'

def _invert(x, limits):
"""inverts a value x on a scale from
limits[0] to limits[1]"""
return limits[1] - (x - limits[0])

class Radar(object):
def init(self, figure, title, labels, rect=None):
if rect is None:
rect = [0.05, 0.05, 0.9, 0.9]

    self.n = len(title)
    self.angles = np.arange(0, 360, 360.0/self.n)

    self.axes = [figure.add_axes(rect, projection='polar', label='axes%d' % i) for i in range(self.n)]

    self.ax = self.axes[0]
    self.ax.set_thetagrids(self.angles, labels=title, fontsize=12)

    for ax in self.axes[1:]:
        ax.patch.set_visible(False)
        ax.grid(False)
        ax.xaxis.set_visible(False)

   for ax, angle, limit in zip(self.axes, self.angles,self.limits):
        ticks = [(limit/self.n_ticks)*i for i in range(self.n_ticks+1)]
        ax.set_rgrids(ticks, angle=angle, label=ticks) #labels=[str(5*(j+1)) for j in range(5)]
        ax.spines['polar'].set_visible(False)
        ax.set_ylim[1:end] # limit

for column in columns:
values = np.array(date_df[column].values)
limits = np.array(limits)
# Hacky way to adjust for the first scale which apparently
# is leading...
values[1:] = (values[1:] / limits[1:]) * limits[0]
angle = np.deg2rad(np.r_[self.angles, self.angles[0]])
values = np.r_[values, values[0]]
self.ax.plot(angle, values, lw=2, label=column)```

def plot(self, values, *args, **kw):
    angle = np.deg2rad(np.r_[self.angles, self.angles[0]])
    values = np.r_[values, values[0]]
    self.ax.plot(angle, values, *args, **kw)

if name == 'main':
fig = plt.figure(figsize=(14, 10))

tit =tit = ['xray','NMR','CD \SPECTROSCOPY','FLORI','SEC-MALS','DLS','HDX-MS','Intact Mass-LCI-MS','Reduced Mass LCI-MS','PMF (NON-REDUCED)','PMF (reduced)','CHARGE VARIANT','HMWP IDENTITY MONOMER SEC-MS','HMWP IDENTITY DIMER SEC-MS','DSC','USP HPLC ASSAY, UV214','SEC','AUC','RP-HPLC','DEAMIDATION','HMWP','RELATED SUBSTANCE','ZINC CONTENT','IR (B) LONG FORM BINDING','IR-B CELL BASED PHOSPHORYLATION ASSAY','ADIPOGENESIS','GLUCOSE UPTAKE','LIPOLYSIS','IR (A) SHORT FORM BINDING','MITOGENIC POTENTIAL','IGF-1 RECEPTOR BINDING','IR-A PHOSPHORYLATION','MITOGENIC H4IIE CELLS','SEDIMENTATION RATE','FTIR','ISOELECTRIC POINT','pH','%API','9'] # 39x

lab = [
    ['1', '2', '0.5', '3', '0.0654'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1'],
    ['1', '2', '0.5', '3', '1']

]

radar = Radar(fig, tit, lab)
radar.plot([2, 3, 2, 1, 3, 1, 1, 1, 1, 2, 3, 1, 1.5,1, 3, 2, 1, 3, 1, 1, 1, 1, 2, 3, 1, 1.5,1, 3, 2, 1, 3, 1, 1, 1, 1, 2, 3, 1, 1.5],  '-', lw=2, color='b', alpha=0.4, label='RLD')
radar.plot([2, 2, 3, 3, 3, 2, 2, 2, 0, 2, 3, 2, 3,2, 2, 3, 3, 3, 2, 2, 2, 0, 2, 3, 2, 3,2, 2, 3, 3, 3, 2, 2, 2, 0, 2, 3, 2, 3], '-', lw=2, color='r', alpha=0.4, label='CART')
radar.plot([3, 4, 3, 1, 2, 2, 4, 3, 1, 2, 3, 3, 2,3, 4, 3, 1, 2, 2, 4, 3, 1, 2, 3, 3, 2,3, 4, 3, 1, 2, 2, 4, 3, 1, 2, 3, 3, 2], '-', lw=2, color='g', alpha=0.4, label='VIALS')
radar.ax.legend()

fig.savefig('rkcresult.png')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment