Spaces:
Build error
Build error
Commit
·
4665298
1
Parent(s):
81cbd29
radar_chart.py updated
Browse files- radar_chart.py +1 -100
radar_chart.py
CHANGED
|
@@ -1,19 +1,3 @@
|
|
| 1 |
-
"""
|
| 2 |
-
======================================
|
| 3 |
-
Radar chart (aka spider or star chart)
|
| 4 |
-
======================================
|
| 5 |
-
|
| 6 |
-
This example creates a radar chart, also known as a spider or star chart [1]_.
|
| 7 |
-
|
| 8 |
-
Although this example allows a frame of either 'circle' or 'polygon', polygon
|
| 9 |
-
frames don't have proper gridlines (the lines are circles instead of polygons).
|
| 10 |
-
It's possible to get a polygon grid by setting GRIDLINE_INTERPOLATION_STEPS in
|
| 11 |
-
matplotlib.axis to the desired number of vertices, but the orientation of the
|
| 12 |
-
polygon is not aligned with the radial axes.
|
| 13 |
-
|
| 14 |
-
.. [1] https://en.wikipedia.org/wiki/Radar_chart
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
import numpy as np
|
| 18 |
|
| 19 |
import matplotlib.pyplot as plt
|
|
@@ -26,28 +10,11 @@ from matplotlib.transforms import Affine2D
|
|
| 26 |
|
| 27 |
|
| 28 |
def radar_factory(num_vars, frame='circle'):
|
| 29 |
-
"""
|
| 30 |
-
Create a radar chart with `num_vars` axes.
|
| 31 |
-
|
| 32 |
-
This function creates a RadarAxes projection and registers it.
|
| 33 |
-
|
| 34 |
-
Parameters
|
| 35 |
-
----------
|
| 36 |
-
num_vars : int
|
| 37 |
-
Number of variables for radar chart.
|
| 38 |
-
frame : {'circle', 'polygon'}
|
| 39 |
-
Shape of frame surrounding axes.
|
| 40 |
-
|
| 41 |
-
"""
|
| 42 |
-
# calculate evenly-spaced axis angles
|
| 43 |
theta = np.linspace(0, 2*np.pi, num_vars, endpoint=False)
|
| 44 |
|
| 45 |
class RadarTransform(PolarAxes.PolarTransform):
|
| 46 |
|
| 47 |
def transform_path_non_affine(self, path):
|
| 48 |
-
# Paths with non-unit interpolation steps correspond to gridlines,
|
| 49 |
-
# in which case we force interpolation (to defeat PolarTransform's
|
| 50 |
-
# autoconversion to circular arcs).
|
| 51 |
if path._interpolation_steps > 1:
|
| 52 |
path = path.interpolated(num_vars)
|
| 53 |
return Path(self.transform(path.vertices), path.codes)
|
|
@@ -59,22 +26,18 @@ def radar_factory(num_vars, frame='circle'):
|
|
| 59 |
|
| 60 |
def __init__(self, *args, **kwargs):
|
| 61 |
super().__init__(*args, **kwargs)
|
| 62 |
-
# rotate plot such that the first axis is at the top
|
| 63 |
self.set_theta_zero_location('N')
|
| 64 |
|
| 65 |
def fill(self, *args, closed=True, **kwargs):
|
| 66 |
-
"""Override fill so that line is closed by default"""
|
| 67 |
return super().fill(closed=closed, *args, **kwargs)
|
| 68 |
|
| 69 |
def plot(self, *args, **kwargs):
|
| 70 |
-
"""Override plot so that line is closed by default"""
|
| 71 |
lines = super().plot(*args, **kwargs)
|
| 72 |
for line in lines:
|
| 73 |
self._close_line(line)
|
| 74 |
|
| 75 |
def _close_line(self, line):
|
| 76 |
x, y = line.get_data()
|
| 77 |
-
# FIXME: markers at x[0], y[0] get doubled-up
|
| 78 |
if x[0] != x[-1]:
|
| 79 |
x = np.append(x, x[0])
|
| 80 |
y = np.append(y, y[0])
|
|
@@ -84,8 +47,6 @@ def radar_factory(num_vars, frame='circle'):
|
|
| 84 |
self.set_thetagrids(np.degrees(theta), labels)
|
| 85 |
|
| 86 |
def _gen_axes_patch(self):
|
| 87 |
-
# The Axes patch must be centered at (0.5, 0.5) and of radius 0.5
|
| 88 |
-
# in axes coordinates.
|
| 89 |
if frame == 'circle':
|
| 90 |
return Circle((0.5, 0.5), 0.5)
|
| 91 |
elif frame == 'polygon':
|
|
@@ -98,13 +59,9 @@ def radar_factory(num_vars, frame='circle'):
|
|
| 98 |
if frame == 'circle':
|
| 99 |
return super()._gen_axes_spines()
|
| 100 |
elif frame == 'polygon':
|
| 101 |
-
# spine_type must be 'left'/'right'/'top'/'bottom'/'circle'.
|
| 102 |
spine = Spine(axes=self,
|
| 103 |
spine_type='circle',
|
| 104 |
path=Path.unit_regular_polygon(num_vars))
|
| 105 |
-
# unit_regular_polygon gives a polygon of radius 1 centered at
|
| 106 |
-
# (0, 0) but we want a polygon of radius 0.5 centered at (0.5,
|
| 107 |
-
# 0.5) in axes coordinates.
|
| 108 |
spine.set_transform(Affine2D().scale(.5).translate(.5, .5)
|
| 109 |
+ self.transAxes)
|
| 110 |
return {'polar': spine}
|
|
@@ -116,25 +73,6 @@ def radar_factory(num_vars, frame='circle'):
|
|
| 116 |
|
| 117 |
|
| 118 |
def example_data():
|
| 119 |
-
# The following data is from the Denver Aerosol Sources and Health study.
|
| 120 |
-
# See doi:10.1016/j.atmosenv.2008.12.017
|
| 121 |
-
#
|
| 122 |
-
# The data are pollution source profile estimates for five modeled
|
| 123 |
-
# pollution sources (e.g., cars, wood-burning, etc) that emit 7-9 chemical
|
| 124 |
-
# species. The radar charts are experimented with here to see if we can
|
| 125 |
-
# nicely visualize how the modeled source profiles change across four
|
| 126 |
-
# scenarios:
|
| 127 |
-
# 1) No gas-phase species present, just seven particulate counts on
|
| 128 |
-
# Sulfate
|
| 129 |
-
# Nitrate
|
| 130 |
-
# Elemental Carbon (EC)
|
| 131 |
-
# Organic Carbon fraction 1 (OC)
|
| 132 |
-
# Organic Carbon fraction 2 (OC2)
|
| 133 |
-
# Organic Carbon fraction 3 (OC3)
|
| 134 |
-
# Pyrolyzed Organic Carbon (OP)
|
| 135 |
-
# 2)Inclusion of gas-phase specie carbon monoxide (CO)
|
| 136 |
-
# 3)Inclusion of gas-phase specie ozone (O3).
|
| 137 |
-
# 4)Inclusion of both gas-phase species is present...
|
| 138 |
data = [
|
| 139 |
['Sulfate', 'Nitrate', 'EC', 'OC1', 'OC2', 'OC3', 'OP', 'CO', 'O3'],
|
| 140 |
('Basecase', [
|
|
@@ -181,45 +119,8 @@ if __name__ == '__main__':
|
|
| 181 |
'surprised'])
|
| 182 |
fig, axs = plt.subplots(figsize=(8, 8), nrows=1, ncols=1,
|
| 183 |
subplot_kw=dict(projection='radar'))
|
| 184 |
-
# fig.subplots_adjust(wspace=0.25, hspace=0.20, top=0.85, bottom=0.05)
|
| 185 |
vec = np.array([0.1, 0.05, 0.2, 0.05, 0.3, 0, 0.15, 0.15])
|
| 186 |
axs.plot(vec)
|
| 187 |
axs.set_varlabels(spoke_labels)
|
| 188 |
-
|
| 189 |
-
# # Plot the four cases from the example data on separate axes
|
| 190 |
-
# for ax, (title, case_data) in zip(axs.flat, data):
|
| 191 |
-
# ax.set_rgrids([0.2, 0.4, 0.6, 0.8])
|
| 192 |
-
# ax.set_title(title, weight='bold', size='medium', position=(0.5, 1.1),
|
| 193 |
-
# horizontalalignment='center', verticalalignment='center')
|
| 194 |
-
# for d, color in zip(case_data, colors):
|
| 195 |
-
# ax.plot(theta, d, color=color)
|
| 196 |
-
# ax.fill(theta, d, facecolor=color, alpha=0.25, label='_nolegend_')
|
| 197 |
-
# ax.set_varlabels(spoke_labels)
|
| 198 |
-
|
| 199 |
-
# # add legend relative to top-left plot
|
| 200 |
-
# labels = ('Factor 1', 'Factor 2', 'Factor 3', 'Factor 4', 'Factor 5')
|
| 201 |
-
# legend = axs[0, 0].legend(labels, loc=(0.9, .95),
|
| 202 |
-
# labelspacing=0.1, fontsize='small')
|
| 203 |
-
|
| 204 |
-
# fig.text(0.5, 0.965, '5-Factor Solution Profiles Across Four Scenarios',
|
| 205 |
-
# horizontalalignment='center', color='black', weight='bold',
|
| 206 |
-
# size='large')
|
| 207 |
-
|
| 208 |
plt.show()
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
#############################################################################
|
| 212 |
-
#
|
| 213 |
-
# .. admonition:: References
|
| 214 |
-
#
|
| 215 |
-
# The use of the following functions, methods, classes and modules is shown
|
| 216 |
-
# in this example:
|
| 217 |
-
#
|
| 218 |
-
# - `matplotlib.path`
|
| 219 |
-
# - `matplotlib.path.Path`
|
| 220 |
-
# - `matplotlib.spines`
|
| 221 |
-
# - `matplotlib.spines.Spine`
|
| 222 |
-
# - `matplotlib.projections`
|
| 223 |
-
# - `matplotlib.projections.polar`
|
| 224 |
-
# - `matplotlib.projections.polar.PolarAxes`
|
| 225 |
-
# - `matplotlib.projections.register_projection`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
def radar_factory(num_vars, frame='circle'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
theta = np.linspace(0, 2*np.pi, num_vars, endpoint=False)
|
| 14 |
|
| 15 |
class RadarTransform(PolarAxes.PolarTransform):
|
| 16 |
|
| 17 |
def transform_path_non_affine(self, path):
|
|
|
|
|
|
|
|
|
|
| 18 |
if path._interpolation_steps > 1:
|
| 19 |
path = path.interpolated(num_vars)
|
| 20 |
return Path(self.transform(path.vertices), path.codes)
|
|
|
|
| 26 |
|
| 27 |
def __init__(self, *args, **kwargs):
|
| 28 |
super().__init__(*args, **kwargs)
|
|
|
|
| 29 |
self.set_theta_zero_location('N')
|
| 30 |
|
| 31 |
def fill(self, *args, closed=True, **kwargs):
|
|
|
|
| 32 |
return super().fill(closed=closed, *args, **kwargs)
|
| 33 |
|
| 34 |
def plot(self, *args, **kwargs):
|
|
|
|
| 35 |
lines = super().plot(*args, **kwargs)
|
| 36 |
for line in lines:
|
| 37 |
self._close_line(line)
|
| 38 |
|
| 39 |
def _close_line(self, line):
|
| 40 |
x, y = line.get_data()
|
|
|
|
| 41 |
if x[0] != x[-1]:
|
| 42 |
x = np.append(x, x[0])
|
| 43 |
y = np.append(y, y[0])
|
|
|
|
| 47 |
self.set_thetagrids(np.degrees(theta), labels)
|
| 48 |
|
| 49 |
def _gen_axes_patch(self):
|
|
|
|
|
|
|
| 50 |
if frame == 'circle':
|
| 51 |
return Circle((0.5, 0.5), 0.5)
|
| 52 |
elif frame == 'polygon':
|
|
|
|
| 59 |
if frame == 'circle':
|
| 60 |
return super()._gen_axes_spines()
|
| 61 |
elif frame == 'polygon':
|
|
|
|
| 62 |
spine = Spine(axes=self,
|
| 63 |
spine_type='circle',
|
| 64 |
path=Path.unit_regular_polygon(num_vars))
|
|
|
|
|
|
|
|
|
|
| 65 |
spine.set_transform(Affine2D().scale(.5).translate(.5, .5)
|
| 66 |
+ self.transAxes)
|
| 67 |
return {'polar': spine}
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
def example_data():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
data = [
|
| 77 |
['Sulfate', 'Nitrate', 'EC', 'OC1', 'OC2', 'OC3', 'OP', 'CO', 'O3'],
|
| 78 |
('Basecase', [
|
|
|
|
| 119 |
'surprised'])
|
| 120 |
fig, axs = plt.subplots(figsize=(8, 8), nrows=1, ncols=1,
|
| 121 |
subplot_kw=dict(projection='radar'))
|
|
|
|
| 122 |
vec = np.array([0.1, 0.05, 0.2, 0.05, 0.3, 0, 0.15, 0.15])
|
| 123 |
axs.plot(vec)
|
| 124 |
axs.set_varlabels(spoke_labels)
|
| 125 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|