Created
February 4, 2014 15:04
-
-
Save dschien/8805271 to your computer and use it in GitHub Desktop.
From matplotlib radar example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'schien' | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.projections.polar import PolarAxes | |
from matplotlib.projections import register_projection | |
def radar_factory(num_vars, frame='circle'): | |
"""Create a radar chart with `num_vars` axes.""" | |
# calculate evenly-spaced axis angles | |
theta = 2 * np.pi * np.linspace(0, 1 - 1. / num_vars, num_vars) | |
# rotate theta such that the first axis is at the top | |
theta += np.pi / 2 | |
def draw_poly_frame(self, x0, y0, r): | |
# TODO: use transforms to convert (x, y) to (r, theta) | |
verts = [(r * np.cos(t) + x0, r * np.sin(t) + y0) for t in theta] | |
return plt.Polygon(verts, closed=True, edgecolor='k') | |
def draw_circle_frame(self, x0, y0, r): | |
return plt.Circle((x0, y0), r) | |
frame_dict = {'polygon': draw_poly_frame, 'circle': draw_circle_frame} | |
if frame not in frame_dict: | |
raise ValueError, 'unknown value for `frame`: %s' % frame | |
class RadarAxes(PolarAxes): | |
"""Class for creating a radar chart (a.k.a. a spider or star chart) | |
http://en.wikipedia.org/wiki/Radar_chart | |
""" | |
name = 'radar' | |
# use 1 line segment to connect specified points | |
RESOLUTION = 1 | |
# define draw_frame method | |
draw_frame = frame_dict[frame] | |
def fill(self, *args, **kwargs): | |
"""Override fill so that line is closed by default""" | |
closed = kwargs.pop('closed', True) | |
return super(RadarAxes, self).fill(closed=closed, *args, **kwargs) | |
def plot(self, *args, **kwargs): | |
"""Override plot so that line is closed by default""" | |
lines = super(RadarAxes, self).plot(*args, **kwargs) | |
for line in lines: | |
self._close_line(line) | |
def _close_line(self, line): | |
x, y = line.get_data() | |
# FIXME: markers at x[0], y[0] get doubled-up | |
if x[0] != x[-1]: | |
x = np.concatenate((x, [x[0]])) | |
y = np.concatenate((y, [y[0]])) | |
line.set_data(x, y) | |
def set_varlabels(self, labels): | |
self.set_thetagrids(theta * 180 / np.pi, labels) | |
def _gen_axes_patch(self): | |
x0, y0 = (0.5, 0.5) | |
r = 0.5 | |
return self.draw_frame(x0, y0, r) | |
register_projection(RadarAxes) | |
return theta | |
if __name__ == '__main__': | |
#The following data is from the Denver Aerosol Sources and Health study. | |
#See doi:10.1016/j.atmosenv.2008.12.017 | |
# | |
#The data are pollution source profile estimates for five modeled pollution | |
#sources (e.g., cars, wood-burning, etc) that emit 7-9 chemical species. | |
#The radar charts are experimented with here to see if we can nicely | |
#visualize how the modeled source profiles change across four scenarios: | |
# 1) No gas-phase species present, just seven particulate counts on | |
# Sulfate | |
# Nitrate | |
# Elemental Carbon (EC) | |
# Organic Carbon fraction 1 (OC) | |
# Organic Carbon fraction 2 (OC2) | |
# Organic Carbon fraction 3 (OC3) | |
# Pyrolized Organic Carbon (OP) | |
# 2)Inclusion of gas-phase specie carbon monoxide (CO) | |
# 3)Inclusion of gas-phase specie ozone (O3). | |
# 4)Inclusion of both gas-phase speciesis present... | |
N = 6 | |
theta = radar_factory(N) | |
spoke_labels = ['A', 'B', 'C', 'D', 'E', 'F'] | |
a = [0.88, 0.6, 0.7, 0.3, 0.30, 0.6] # [6, 7, 5, 6, 8, 4] | |
a = np.asarray(a) * 10 | |
b = [0.5, 0.6, 0.8, 0.7, 0.8, 0.4] | |
b = np.asarray(b) * 10 | |
fig = plt.figure(figsize=(9, 9)) | |
# adjust spacing around the subplots | |
fig.subplots_adjust(wspace=0.25, hspace=0.20, top=0.85, bottom=0.05) | |
title_list = ['Basecase'] | |
data = {'Basecase': [a, b]} | |
colors = ['b', 'r'] | |
# chemicals range from 0 to 1 | |
radial_grid = [2., 4., 6., 8.] | |
# If you don't care about the order, you can loop over data_dict.items() | |
for n, title in enumerate(title_list): | |
ax = fig.add_subplot(2, 2, n + 1, projection='radar') | |
plt.rgrids(radial_grid) | |
ax.set_title(title, weight='bold', size='medium', position=(0.5, 1.1), | |
horizontalalignment='center', verticalalignment='center') | |
for d, color in zip(data[title], colors): | |
ax.plot(theta, d, color=color) | |
ax.fill(theta, d, facecolor=color, alpha=0.25) | |
ax.set_varlabels(spoke_labels) | |
# add legend relative to top-left plot | |
plt.subplot(2, 2, 1) | |
labels = ('A', 'B') | |
legend = plt.legend(labels, loc=(0.9, .95), labelspacing=0.1) | |
plt.setp(legend.get_texts(), fontsize='small') | |
plt.figtext(0.5, 0.965, 'Result compared A with B', | |
ha='center', color='black', weight='bold', size='large') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ttcttdtm