如何在一个图中显示多个具有不同比例轴的雷达图?
How do I display multiple radar charts with differently scaled axes each in one figure?
我正在尝试使用 matplotlib 在一个 window 中显示两个极坐标图。这是使用子图实现的。每个子图都是使用 this solution. The two diagrams are then combined using this 解决方案创建的:
雷达 class 处理单个雷达图的创建:
class Radar:
def __init__(self, fig, titles, labels, ylimit, lines, rect=None):
if rect is None:
rect = [0.2, 0.2, 0.6, 0.6]
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(lines, angle=angle, labels=label)
ax.spines["polar"].set_visible(False)
ax.set_ylim(ylimit[0], ylimit[1])
def plot(self, values, *args, **kw):
angle = np.deg2rad(np.r_[self.angles, self.angles[0]])
values = np.r_[values, values[0]]
return self.ax.plot(angle, values, *args, **kw)
以下代码用于创建两个雷达图并将它们添加到一个图中:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import Line2D
fig1 = plt.figure(figsize=(9, 9))
plt.ioff()
#############################
# first radar chart
#############################
titles = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
lbl_count = 7
upper_bound = 70
values = [0, 10, 40, 30, 20, 50, 30, 40]
labels1 = np.tile(np.arange(-60 + upper_bound / lbl_count, 20, upper_bound / lbl_count), (8, 1))
lines1 = np.arange(10, upper_bound, 10)
radar1 = Radar(fig1, titles, labels1, (0, upper_bound), lines1)
plt1 = radar1.plot(values, "-", lw=2, color="b", alpha=0.4, label="Fitness") # type: List[Line2D]
#############################
# second radar chart
#############################
fig2 = plt.figure(figsize=(9, 9))
values = [0.4, 0.7, 0.2, 0.1, 0.8, 0.3, 0.5, 0.7]
lbl_count = 5
labels2 = [list("12345"), [0.1, 0.2, 0.3, 0.4, 0.5], list("54321"), [10, 8, 6, 4, 2], list("12345"), list("12345"), list("12345"), list("12345")]
lines2 = np.arange(0.2, 1.2, 0.2)
radar2 = Radar(fig2, titles, labels2, (0, 1), lines2)
plt2 = radar2.plot(values, "-", lw=2, color="b", alpha=0.4, label="Values")
plt3 = radar2.plot([0.1, 0.2, 0.5, 0.2, 0.1, 0.7, 0.4, 0.2], "-", lw=2, color="r", alpha=0.4, label="Critical Thresholds")
#############################
# combine radar charts
#############################
fig3, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar'), figsize=(25, 15))
line1, = ax1.plot(plt1[0].get_xdata(), plt1[0].get_ydata(), 'g-', label="Fitness")
line2, = ax2.plot(plt2[0].get_xdata(), plt2[0].get_ydata(), 'b-', label="Values")
line3, = ax2.plot(plt3[0].get_xdata(), plt3[0].get_ydata(), 'r-', label="Critical Thresholds")
ax1.set_ylim(0, 80)
ax2.set_ylim(0, 1)
plt.tight_layout()
plt.show()
plt.close()
两图合并后,不同比例尺的标签没有了(图1和图2是想要的结果,而合并后的图3少了一些标签)
如何添加缺失的标签?
如果您想从雷达的功能中获益,则需要实际使用雷达 class。
fig3 = plt.figure(figsize=(13, 8))
titles = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
### first subplot:
lbl_count = 7
upper_bound = 70
values = [0, 10, 40, 30, 20, 50, 30, 40]
labels1 = np.tile(np.arange(-60 + upper_bound / lbl_count, 20, upper_bound / lbl_count), (8, 1))
lines1 = np.arange(10, upper_bound, 10)
radar1 = Radar(fig3, titles, labels1, (0, upper_bound), lines1, rect=[0.55,0.1,0.35,0.8])
plt1 = radar1.plot(values, "-", lw=2, color="b", alpha=0.4, label="Fitness")
### second subplot:
values = [0.4, 0.7, 0.2, 0.1, 0.8, 0.3, 0.5, 0.7]
lbl_count = 5
labels2 = [list("12345"), [0.1, 0.2, 0.3, 0.4, 0.5], list("54321"), [10, 8, 6, 4, 2], list("12345"), list("12345"), list("12345"), list("12345")]
lines2 = np.arange(0.2, 1.2, 0.2)
radar2 = Radar(fig3, titles, labels2, (0, 1), lines2, rect=[0.1,0.1,0.35,0.8])
plt2 = radar2.plot(values, "-", lw=2, color="b", alpha=0.4, label="Values")
plt3 = radar2.plot([0.1, 0.2, 0.5, 0.2, 0.1, 0.7, 0.4, 0.2], "-", lw=2, color="r", alpha=0.4, label="Critical Thresholds")
plt.show()
我正在尝试使用 matplotlib 在一个 window 中显示两个极坐标图。这是使用子图实现的。每个子图都是使用 this solution. The two diagrams are then combined using this 解决方案创建的:
雷达 class 处理单个雷达图的创建:
class Radar:
def __init__(self, fig, titles, labels, ylimit, lines, rect=None):
if rect is None:
rect = [0.2, 0.2, 0.6, 0.6]
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(lines, angle=angle, labels=label)
ax.spines["polar"].set_visible(False)
ax.set_ylim(ylimit[0], ylimit[1])
def plot(self, values, *args, **kw):
angle = np.deg2rad(np.r_[self.angles, self.angles[0]])
values = np.r_[values, values[0]]
return self.ax.plot(angle, values, *args, **kw)
以下代码用于创建两个雷达图并将它们添加到一个图中:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import Line2D
fig1 = plt.figure(figsize=(9, 9))
plt.ioff()
#############################
# first radar chart
#############################
titles = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
lbl_count = 7
upper_bound = 70
values = [0, 10, 40, 30, 20, 50, 30, 40]
labels1 = np.tile(np.arange(-60 + upper_bound / lbl_count, 20, upper_bound / lbl_count), (8, 1))
lines1 = np.arange(10, upper_bound, 10)
radar1 = Radar(fig1, titles, labels1, (0, upper_bound), lines1)
plt1 = radar1.plot(values, "-", lw=2, color="b", alpha=0.4, label="Fitness") # type: List[Line2D]
#############################
# second radar chart
#############################
fig2 = plt.figure(figsize=(9, 9))
values = [0.4, 0.7, 0.2, 0.1, 0.8, 0.3, 0.5, 0.7]
lbl_count = 5
labels2 = [list("12345"), [0.1, 0.2, 0.3, 0.4, 0.5], list("54321"), [10, 8, 6, 4, 2], list("12345"), list("12345"), list("12345"), list("12345")]
lines2 = np.arange(0.2, 1.2, 0.2)
radar2 = Radar(fig2, titles, labels2, (0, 1), lines2)
plt2 = radar2.plot(values, "-", lw=2, color="b", alpha=0.4, label="Values")
plt3 = radar2.plot([0.1, 0.2, 0.5, 0.2, 0.1, 0.7, 0.4, 0.2], "-", lw=2, color="r", alpha=0.4, label="Critical Thresholds")
#############################
# combine radar charts
#############################
fig3, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar'), figsize=(25, 15))
line1, = ax1.plot(plt1[0].get_xdata(), plt1[0].get_ydata(), 'g-', label="Fitness")
line2, = ax2.plot(plt2[0].get_xdata(), plt2[0].get_ydata(), 'b-', label="Values")
line3, = ax2.plot(plt3[0].get_xdata(), plt3[0].get_ydata(), 'r-', label="Critical Thresholds")
ax1.set_ylim(0, 80)
ax2.set_ylim(0, 1)
plt.tight_layout()
plt.show()
plt.close()
两图合并后,不同比例尺的标签没有了(图1和图2是想要的结果,而合并后的图3少了一些标签)
如何添加缺失的标签?
如果您想从雷达的功能中获益,则需要实际使用雷达 class。
fig3 = plt.figure(figsize=(13, 8))
titles = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
### first subplot:
lbl_count = 7
upper_bound = 70
values = [0, 10, 40, 30, 20, 50, 30, 40]
labels1 = np.tile(np.arange(-60 + upper_bound / lbl_count, 20, upper_bound / lbl_count), (8, 1))
lines1 = np.arange(10, upper_bound, 10)
radar1 = Radar(fig3, titles, labels1, (0, upper_bound), lines1, rect=[0.55,0.1,0.35,0.8])
plt1 = radar1.plot(values, "-", lw=2, color="b", alpha=0.4, label="Fitness")
### second subplot:
values = [0.4, 0.7, 0.2, 0.1, 0.8, 0.3, 0.5, 0.7]
lbl_count = 5
labels2 = [list("12345"), [0.1, 0.2, 0.3, 0.4, 0.5], list("54321"), [10, 8, 6, 4, 2], list("12345"), list("12345"), list("12345"), list("12345")]
lines2 = np.arange(0.2, 1.2, 0.2)
radar2 = Radar(fig3, titles, labels2, (0, 1), lines2, rect=[0.1,0.1,0.35,0.8])
plt2 = radar2.plot(values, "-", lw=2, color="b", alpha=0.4, label="Values")
plt3 = radar2.plot([0.1, 0.2, 0.5, 0.2, 0.1, 0.7, 0.4, 0.2], "-", lw=2, color="r", alpha=0.4, label="Critical Thresholds")
plt.show()