Radar chart (aka spider or star chart)
matplotlib.pyplot
as
plt
import
numpy
as
np
from
matplotlib.patches
import
,
from
matplotlib.path
import
from
matplotlib.projections
import
from
matplotlib.projections.polar
import
from
matplotlib.spines
import
from
matplotlib.transforms
import
def
radar_factory
(
num_vars
,
frame
=
'circle'
):
"""
Create a radar chart with `num_vars` Axes.
This function creates a RadarAxes projection and registers it.
Parameters
----------
num_vars : int
Number of variables for radar chart.
frame : {'circle', 'polygon'}
Shape of frame surrounding Axes.
"""
=
(
0
,
2
*
,
num_vars
,
endpoint
=
False
)
class
RadarTransform
(
):
def
transform_path_non_affine
(
self
,
path
):
if
path
.
_interpolation_steps
>
1
:
path
=
path
.
interpolated
(
num_vars
)
return
(
self
.
transform
(
path
.
vertices
),
path
.
codes
)
class
RadarAxes
(
):
name
=
'radar'
PolarTransform
=
RadarTransform
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
set_theta_zero_location
(
'N'
)
def
fill
(
self
,
*
args
,
closed
=
True
,
**
kwargs
):
"""Override fill so that line is closed by default"""
return
super
()
.
fill
(
closed
=
closed
,
*
args
,
**
kwargs
)
def
plot
(
self
,
*
args
,
**
kwargs
):
"""Override plot so that line is closed by default"""
lines
=
super
()
.
plot
(
*
args
,
**
kwargs
)
for
line
in
lines
:
self
.
_close_line
(
line
)
def
_close_line
(
self
,
line
):
x
,
y
=
line
.
get_data
()
if
x
[
0
]
!=
x
[
-
1
]:
x
=
(
x
,
x
[
0
])
y
=
(
y
,
y
[
0
])
line
.
set_data
(
x
,
y
)
def
set_varlabels
(
self
,
):
self
.
set_thetagrids
(
(
),
)
def
_gen_axes_patch
(
self
):
if
frame
==
'circle'
:
return
((
0.5
,
0.5
),
0.5
)
elif
frame
==
'polygon'
:
return
((
0.5
,
0.5
),
num_vars
,
radius
=
.5
,
edgecolor
=
"k"
)
else
:
raise
ValueError
(
"Unknown value for 'frame':
%s
"
%
frame
)
def
_gen_axes_spines
(
self
):
if
frame
==
'circle'
:
return
super
()
.
_gen_axes_spines
()
elif
frame
==
'polygon'
:
spine
=
(
axes
=
self
,
spine_type
=
'circle'
,
path
=
(
num_vars
))
spine
.
set_transform
(
()
.
scale
(
.5
)
.
translate
(
.5
,
.5
)
+
self
.
transAxes
)
return
{
'polar'
:
spine
}
else
:
raise
ValueError
(
"Unknown value for 'frame':
%s
"
%
frame
)
(
RadarAxes
)
return
def
example_data
():
=
[
[
'Sulfate'
,
'Nitrate'
,
'EC'
,
'OC1'
,
'OC2'
,
'OC3'
,
'OP'
,
'CO'
,
'O3'
],
(
'Basecase'
,
[
[
0.88
,
0.01
,
0.03
,
0.03
,
0.00
,
0.06
,
0.01
,
0.00
,
0.00
],
[
0.07
,
0.95
,
0.04
,
0.05
,
0.00
,
0.02
,
0.01
,
0.00
,
0.00
],
[
0.01
,
0.02
,
0.85
,
0.19
,
0.05
,
0.10
,
0.00
,
0.00
,
0.00
],
[
0.02
,
0.01
,
0.07
,
0.01
,
0.21
,
0.12
,
0.98
,
0.00
,
0.00
],
[
0.01
,
0.01
,
0.02
,
0.71
,
0.74
,
0.70
,
0.00
,
0.00
,
0.00
]]),
(
'With CO'
,
[
[
0.88
,
0.02
,
0.02
,
0.02
,
0.00
,
0.05
,
0.00
,
0.05
,
0.00
],
[
0.08
,
0.94
,
0.04
,
0.02
,
0.00
,
0.01
,
0.12
,
0.04
,
0.00
],
[
0.01
,
0.01
,
0.79
,
0.10
,
0.00
,
0.05
,
0.00
,
0.31
,
0.00
],
[
0.00
,
0.02
,
0.03
,
0.38
,
0.31
,
0.31
,
0.00
,
0.59
,
0.00
],
[
0.02
,
0.02
,
0.11
,
0.47
,
0.69
,
0.58
,
0.88
,
0.00
,
0.00
]]),
(
'With O3'
,
[
[
0.89
,
0.01
,
0.07
,
0.00
,
0.00
,
0.05
,
0.00
,
0.00
,
0.03
],
[
0.07
,
0.95
,
0.05
,
0.04
,
0.00
,
0.02
,
0.12
,
0.00
,
0.00
],
[
0.01
,
0.02
,
0.86
,
0.27
,
0.16
,
0.19
,
0.00
,
0.00
,
0.00
],
[
0.01
,
0.03
,
0.00
,
0.32
,
0.29
,
0.27
,
0.00
,
0.00
,
0.95
],
[
0.02
,
0.00
,
0.03
,
0.37
,
0.56
,
0.47
,
0.87
,
0.00
,
0.00
]]),
(
'CO & O3'
,
[
[
0.87
,
0.01
,
0.08
,
0.00
,
0.00
,
0.04
,
0.00
,
0.00
,
0.01
],
[
0.09
,
0.95
,
0.02
,
0.03
,
0.00
,
0.01
,
0.13
,
0.06
,
0.00
],
[
0.01
,
0.02
,
0.71
,
0.24
,
0.13
,
0.16
,
0.00
,
0.50
,
0.00
],
[
0.01
,
0.03
,
0.00
,
0.28
,
0.24
,
0.23
,
0.00
,
0.44
,
0.88
],
[
0.02
,
0.00
,
0.18
,
0.45
,
0.64
,
0.55
,
0.86
,
0.00
,
0.16
]])
]
return
if
__name__
==
'__main__'
:
=
9
=
radar_factory
(
,
frame
=
'polygon'
)
=
example_data
()
=
.
pop
(
0
)
,
=
(
figsize
=
(
9
,
9
),
nrows
=
2
,
ncols
=
2
,
subplot_kw
=
dict
(
projection
=
'radar'
))
(
wspace
=
0.25
,
hspace
=
0.20
,
top
=
0.85
,
bottom
=
0.05
)
=
[
'b'
,
'r'
,
'g'
,
'm'
,
'y'
]
for
ax
,
(
,
)
in
zip
(
,
):
ax
.
set_rgrids
([
0.2
,
0.4
,
0.6
,
0.8
])
(
,
weight
=
'bold'
,
size
=
'medium'
,
position
=
(
0.5
,
1.1
),
horizontalalignment
=
'center'
,
verticalalignment
=
'center'
)
for
,
in
zip
(
,
):
(
,
,
=
)
(
,
,
facecolor
=
,
alpha
=
0.25
,
label
=
'_nolegend_'
)
ax
.
set_varlabels
(
)
=
(
'Factor 1'
,
'Factor 2'
,
'Factor 3'
,
'Factor 4'
,
'Factor 5'
)
=
[
0
,
0
]
.
(
,
loc
=
(
0.9
,
.95
),
labelspacing
=
0.1
,
fontsize
=
'small'
)
(
0.5
,
0.965
,
'5-Factor Solution Profiles Across Four Scenarios'
,
horizontalalignment
=
'center'
,
=
'black'
,
weight
=
'bold'
,
size
=
'large'
)
()
The use of the following functions, methods, classes and modules is shown
in this example:
Total running time of the script: (0 minutes 2.328 seconds)