【问题标题】:Adding tick marks on Thetagrid lines of a polar plot在极坐标图的 Thetagrid 线上添加刻度线
【发布时间】:2018-06-05 16:31:34
【问题描述】:

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

这是我当前的代码:

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 对象的属性中所做的任何更改都没有以我希望它们呈现的方式更改线条。

【问题讨论】:

    标签: python matplotlib spider-chart


    【解决方案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()
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      相关资源
      最近更新 更多