为什么不显示此轴或标题?
Why isn't this axis or title showing up?
我正在努力创建极坐标图,但我无法让所有内容都按照我想要的方式显示。
谁能帮我弄清楚为什么 2 点钟的轴和标题没有显示出来?任何其他使它看起来更好的技巧将不胜感激!这是我的代码:
import numpy as np
import pylab as pl
import matplotlib.pyplot as py
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=12, weight="bold")
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, 7), labels=label, angle=angle, fontsize=12)
ax.spines["polar"].set_visible(False)
ax.set_ylim(0, 6)
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=(20, 10))
titles = [
"Canada", "Australia",
"New Zealand", "Japan", "China", "USA", "Mexico", "Finland",
]
labels = [
list("abcde"), list("12345"), list("uvwxy"),
[" ", " ", "6", "8", "0"],
list("jklmn"), list("asdfg"), list("qwert"), [" ", "4.3", "4.4", "4.5", "4.6"]
]
radar = Radar(fig, titles, labels)
radar.plot([1, 3, 2, 5, 4, 5, 3, 3], "--", lw=1, color="b", alpha=.5, label="USA 2014")
radar.plot([2.3, 2, 3, 3, 2, 3, 2, 4],"-", lw=1, color="r", alpha=.5, label="2014")
radar.plot([3, 4, 3, 4, 2, 2, 1, 3], "-", lw=1, color="g", alpha=.5, label="2013")
radar.plot([4.5, 5, 4, 5, 3, 3, 4, 4], "-", lw=1, color="y", alpha=.5, label="2012")
radar.ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.10),
fancybox=True, shadow=True, ncol=4)
fig = py.gcf()
fig.set_size_inches(6, 10, forward=True)
fig.savefig('test2png.png', dpi=100, bbox_inches="tight", pad_inches=1)
这是它生成的图像:
非常感谢任何帮助。
问题是,当您使用此行定义 radar.angles
时:
self.angles = np.arange(90, 90+360, 360.0/self.n)
最后的角度是 >360.
:
print self.angles
>>> [ 90. 135. 180. 225. 270. 315. 360. 405.]
你只需要分配不同的角度,确保它们都是 <=360.
例如,如果将 self.angles
的定义更改为:
self.angles = [a if a <=360. else a - 360. for a in np.arange(90, 90+360, 360.0/self.n)]
print self.angles
>>> [ 90. 135. 180. 225. 270. 315. 360. 45.]
然后一切正常:
我正在努力创建极坐标图,但我无法让所有内容都按照我想要的方式显示。
谁能帮我弄清楚为什么 2 点钟的轴和标题没有显示出来?任何其他使它看起来更好的技巧将不胜感激!这是我的代码:
import numpy as np
import pylab as pl
import matplotlib.pyplot as py
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=12, weight="bold")
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, 7), labels=label, angle=angle, fontsize=12)
ax.spines["polar"].set_visible(False)
ax.set_ylim(0, 6)
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=(20, 10))
titles = [
"Canada", "Australia",
"New Zealand", "Japan", "China", "USA", "Mexico", "Finland",
]
labels = [
list("abcde"), list("12345"), list("uvwxy"),
[" ", " ", "6", "8", "0"],
list("jklmn"), list("asdfg"), list("qwert"), [" ", "4.3", "4.4", "4.5", "4.6"]
]
radar = Radar(fig, titles, labels)
radar.plot([1, 3, 2, 5, 4, 5, 3, 3], "--", lw=1, color="b", alpha=.5, label="USA 2014")
radar.plot([2.3, 2, 3, 3, 2, 3, 2, 4],"-", lw=1, color="r", alpha=.5, label="2014")
radar.plot([3, 4, 3, 4, 2, 2, 1, 3], "-", lw=1, color="g", alpha=.5, label="2013")
radar.plot([4.5, 5, 4, 5, 3, 3, 4, 4], "-", lw=1, color="y", alpha=.5, label="2012")
radar.ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.10),
fancybox=True, shadow=True, ncol=4)
fig = py.gcf()
fig.set_size_inches(6, 10, forward=True)
fig.savefig('test2png.png', dpi=100, bbox_inches="tight", pad_inches=1)
这是它生成的图像:
非常感谢任何帮助。
问题是,当您使用此行定义 radar.angles
时:
self.angles = np.arange(90, 90+360, 360.0/self.n)
最后的角度是 >360.
:
print self.angles
>>> [ 90. 135. 180. 225. 270. 315. 360. 405.]
你只需要分配不同的角度,确保它们都是 <=360.
例如,如果将 self.angles
的定义更改为:
self.angles = [a if a <=360. else a - 360. for a in np.arange(90, 90+360, 360.0/self.n)]
print self.angles
>>> [ 90. 135. 180. 225. 270. 315. 360. 45.]
然后一切正常: