Reputation: 2003
I'm trying to learn how radar charts work in matplotlib. I'm using the code in this thread, but the plot I'm producing is not rightly centered and there are axis missing. I' ve tried with matplotlib 1.3.1, 1.4.1 and 1.5.1 in case something changed in the last versions.
import numpy as np
import pylab as pl
class Radar(object):
def __init__(self, fig, titles, labels, rect=None):
if rect is None:
rect = [0.05, 0.05, 0.95, 0.95]
self.n = len(titles)
self.angles = np.arange(90, 90+360, 360.0/self.n)
self.axes = [fig.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=titles, fontsize=14)
for ax in self.axes[1:]:
ax.patch.set_visible(False)
ax.grid("off")
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)
fig = pl.figure(figsize=(6, 6))
titles = list("ABCDE")
labels = [
list("abcde"), list("12345"), list("uvwxy"),
["one", "two", "three", "four", "five"],
list("jklmn")
]
radar = Radar(fig, titles, labels)
radar.plot([1, 3, 2, 5, 4], "-", lw=2, color="b", alpha=0.4, label="first")
radar.plot([2.3, 2, 3, 3, 2],"-", lw=2, color="r", alpha=0.4, label="second")
radar.plot([3, 4, 3, 4, 2], "-", lw=2, color="g", alpha=0.4, label="third")
radar.ax.legend()
Upvotes: 0
Views: 2451
Reputation: 2656
I had the same problem, but I found that the problem was the following part:
self.angles = np.arange(90, 90+360, 360.0/self.n)
So change it to
self.angles = np.arange(0, 360, 360.0/self.n)
and rotate each ax
with ax.set_theta_offset(np.deg2rad(90))
instead.
The modified class looks like this:
# Python 3.4
# matplotlib 1.5.3
class Radar(object):
def __init__(self, fig, titles, labels, rotation=0, rect=None):
if rect is None:
rect = [0.05, 0.05, 0.95, 0.95]
self.n = len(titles)
self.angles = np.arange(0, 360, 360.0/self.n)
self.axes = [fig.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=titles, fontsize=14)
for ax in self.axes[1:]:
ax.patch.set_visible(False)
ax.grid("off")
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, 6)
ax.set_theta_offset(np.deg2rad(rotation))
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)
Note: I've added the parameter rotation=0
to __init__
and apply the rotation to all ax
in the last loop of it.
I know it's been a while since the question was asked, but I assume someone else will stumble upon this.
Upvotes: 4