在matplotlib极坐标图中设置中心标记的样式

时间:2018-06-12 14:58:11

标签: python matplotlib plot

我在Adding tick marks on Thetagrid lines of a polar plot中提出了相关问题,并且能够回答大部分问题。但是,我无法找到一种方法来设置中心标记的样式。此标记始终是最后一个θ网格标签下方标记的颜色和样式(示例图中的TG06)。我在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='-', zorder=1)

        # 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):
        # get the gridline
        gl = tick.gridline
        gl.set_marker('o')
        gl.set_markersize(15)
        if idx == 0:
            gl.set_markerfacecolor('#003399')
        elif idx == 1:
            gl.set_markerfacecolor('#336666')
        elif idx == 2:
            gl.set_markerfacecolor('#336699')
        elif idx == 3:
            gl.set_markerfacecolor('#CC3333')
        elif idx == 4:
            gl.set_markerfacecolor('#CC9933')
        # this doesn't get used. The center doesn't seem to be different than 5
        else:
            gl.set_markerfacecolor('#000000')

        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()

当前渲染显示最后一个标记位于最后一个θ网格标签下方以及中心位置(最后应用的标记颜色):

enter image description here

1 个答案:

答案 0 :(得分:2)

您的gl个对象实际上只是matplotlib轴上的Line2D个对象。每一个都有一个点(0,0)和一个点(0,1)。第二点是你看到的每种颜色。第一个是(0,0),是中心的那个。您只能看到最后一个,因为它会被每个后续颜色覆盖。

一个简单的解决方案是在中心用您想要的颜色绘制一个点。例如,在decorate_ticks循环后的for idx, tick末尾添加此行:

axes.plot(0, 0, 'o', markersize=15, markerfacecolor='m', markeredgecolor='k')

其中给出了以下情节:

enter image description here

为了完整性,这里有整个功能:

def decorate_ticks(self, axes):
    for idx, tick in enumerate(axes.xaxis.majorTicks):
        # get the gridline
        gl = tick.gridline
        gl.set_marker('o')
        gl.set_markersize(15)
        if idx == 0:
            gl.set_markerfacecolor('#003399')
        elif idx == 1:
            gl.set_markerfacecolor('#336666')
        elif idx == 2:
            gl.set_markerfacecolor('#336699')
        elif idx == 3:
            gl.set_markerfacecolor('#CC3333')
        elif idx == 4:
            gl.set_markerfacecolor('#CC9933')
        # this doesn't get used. The center doesn't seem to be different than 5
        else:
            gl.set_markerfacecolor('#000000')

        if idx == 0 or idx == 3:
            tick.set_pad(10)
        else:
            tick.set_pad(30)
    axes.plot(0, 0, 'o', markersize=15, markerfacecolor='m', markeredgecolor='k')