1

我已经修改了这个示例:Matplotlib: Polar plot axis tick label location。我想在从极轴中心到 thetagrid 标签的 thetagrid 旁边的刻度标签(A、B、C、D 和 E)旁边添加刻度线。另外,我想要一个圆形结束每一行,直接在 thetagrid 标题(TG01、TG02 等)下方。您将在我的代码示例中看到这些刻度标签和 thetagrid 标题。从视觉上看,这是 TG01 行的一部分,用于演示我正在寻找的内容:

所需的 Thetagrid 线装饰

这是我当前的代码:

import numpy as np
import matplotlib.pyplot as plt

class Radar(object):

def __init__(self, fig, titles, label, rect=None):
    if rect is None:
        rect = [0.05, 0.15, 0.95, 0.75]

    self.n = len(titles)
    self.angles = [a if a <=360. else a - 360. for a in 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]

    # Show the labels
    self.ax.set_thetagrids(self.angles, labels=titles, fontsize=14, weight="bold", color="black")

    for ax in self.axes[1:]:
        ax.patch.set_visible(False)
        ax.grid(False)
        ax.xaxis.set_visible(False)
        self.ax.yaxis.grid(False)

    for ax, angle in zip(self.axes, self.angles):
        ax.set_rgrids(range(1, 6), labels=label, angle=angle, fontsize=12)
        # hide outer spine (circle)
        ax.spines["polar"].set_visible(False)
        ax.set_ylim(0, 6)  
        ax.xaxis.grid(True, color='black', linestyle='-')

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 = plt.figure(1)

titles = ['TG01', 'TG02', 'TG03', 'TG04', 'TG05', 'TG06']
label = list("ABCDE")

radar = Radar(fig, titles, label)
radar.plot([3.75, 3.25, 3.0, 2.75, 4.25, 3.5], "-", linewidth=2, color="b", alpha=.7, label="Data01")
radar.plot([3.25, 2.25, 2.25, 2.25, 1.5, 1.75],"-", linewidth=2, color="r", alpha=.7, label="Data02")

radar.ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.10),
  fancybox=True, shadow=True, ncol=4)

plt.show()

以及极坐标图目前的样子:

当前代码运行时的极坐标图

我仔细查看了使用 ax.xaxis.majorTicks 检索到的 ThetaTick 对象。但是,我在 ThetaTick 对象的属性中所做的任何更改都没有以我希望它们呈现的方式更改线条。

4

1 回答 1

1

通过为 y 轴配置轴刻度参数,我能够获得我想要的大部分内容:

ax.tick_params(axis='y', pad=0, left=True, length=6, width=1, direction='inout')

以及带有 decorate_ticks 函数的 theta 刻度标签下方的标记装饰(如代码和结果所示)。但是,中心标记似乎始终是最后一个 theta 网格标记的颜色和样式。我在 decorate_ticks 函数内部的评论中注意到了这一点。如果您知道以不同方式设置中心标记样式的方法,请告诉我如何。

import numpy as np
import matplotlib.pyplot as plt

class Radar(object):

  def __init__(self, fig, titles, label, rect=None):
    if rect is None:
        rect = [0.05, 0.15, 0.95, 0.75]

    self.n = len(titles)
    self.angles = [a if a <=360. else a - 360. for a in 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]

    # Show the labels
    self.ax.set_thetagrids(self.angles, labels=titles, fontsize=14, weight="bold", color="black")

    for ax in self.axes[1:]:
        ax.patch.set_visible(False)
        ax.grid(False)
        ax.xaxis.set_visible(False)
        self.ax.yaxis.grid(False)

    for ax, angle in zip(self.axes, self.angles):
        ax.set_rgrids(range(1, 6), labels=label, angle=angle, fontsize=12)
        # hide outer spine (circle)
        ax.spines["polar"].set_visible(False)
        ax.set_ylim(0, 6)  
        ax.xaxis.grid(True, color='black', linestyle='-')

        # draw a line on the y axis at each label
        ax.tick_params(axis='y', pad=0, left=True, length=6, width=1, direction='inout')

  def decorate_ticks(self, axes):
    for idx, tick in enumerate(axes.xaxis.majorTicks):
        # print(idx, tick.label._text)
        # get the gridline
        gl = tick.gridline
        gl.set_marker('o')
        gl.set_markersize(15)
        if idx == 0:
            gl.set_markerfacecolor('b')
        elif idx == 1:
            gl.set_markerfacecolor('c')
        elif idx == 2:
            gl.set_markerfacecolor('g')
        elif idx == 3:
            gl.set_markerfacecolor('y')
        elif idx == 4:
            gl.set_markerfacecolor('r')
        # this doesn't get used. The center doesn't seem to be different than 5
        else:
            gl.set_markerfacecolor('black')

        if idx == 0 or idx == 3:
            tick.set_pad(10)
        else:
            tick.set_pad(30)

  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 = plt.figure(1)

titles = ['TG01', 'TG02', 'TG03', 'TG04', 'TG05', 'TG06']
label = list("ABCDE")

radar = Radar(fig, titles, label)
radar.plot([3.75, 3.25, 3.0, 2.75, 4.25, 3.5], "-", linewidth=2, color="b",  alpha=.7, label="Data01")
radar.plot([3.25, 2.25, 2.25, 2.25, 1.5, 1.75],"-", linewidth=2, color="r", alpha=.7, label="Data02")

radar.decorate_ticks(radar.ax)

# this avoids clipping the markers below the thetagrid labels
radar.ax.xaxis.grid(clip_on = False)

radar.ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.10),
  fancybox=True, shadow=True, ncol=4)

plt.show()

结果: 在此处输入图像描述

于 2018-06-07T13:34:11.333 回答