在极坐标图的 Thetagrid 线上添加刻度线 [英] Adding tick marks on Thetagrid lines of a polar plot

查看:86
本文介绍了在极坐标图的 Thetagrid 线上添加刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已修改此示例:

这是我当前的代码:

将 numpy 导入为 np导入matplotlib.pyplot作为plt类Radar(object):def __init __(自身,无花果,标题,标签,rect = None):如果rect为None:rect = [0.05,0.15,0.95,0.75]self.n = len(标题)self.angles = [a if a <=360.否则 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)对于我在范围内([self.n])self.ax = self.axes [0]# 显示标签self.ax.set_thetagrids(self.angles, labels=titles, fontsize=14, weight="bold", color="black")用于self.axes [1:]中的斧头:ax.patch.set_visible(False)ax.grid(假)ax.xaxis.set_visible(False)self.ax.yaxis.grid(False)对于斧头,zip中的角度(self.axes,self.angles):ax.set_rgrids(range(1, 6),labels=label,angle=angle,fontsize=12)#隐藏外脊(圆形)ax.spines["polar"].set_visible(False)ax.set_ylim(0, 6)ax.xaxis.grid(True, color='black', linestyle='-')def图(自身,值,* args,** kw):角度 = np.deg2rad(np.r_[self.angles, self.angles[0]])值= np.r_ [值,值[0]]self.ax.plot(角度,值,*args,**kw)无花果= plt.figure(1)标题 = ['TG01'、'TG02'、'TG03'、'TG04'、'TG05'、'TG06']label = list("ABCDE")雷达=雷达(图,标题,标签)雷达.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],-",线宽= 2,color ="r",alpha = .7,label ="Data02")radar.ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.10),花式框=真,阴影=真,ncol=4)plt.show()

以及极坐标图的当前外观:

我仔细查看了使用 ax.xaxis.majorTicks 检索到的 ThetaTick 对象.但是,我对ThetaTick对象的属性所做的任何更改都没有改变线条的显示方式.

解决方案

通过为y轴配置轴刻度参数,我可以得到大多数想要的东西:

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

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

将 numpy 导入为 np导入matplotlib.pyplot作为plt类雷达(对象):def __init __(自身,无花果,标题,标签,rect = None):如果 rect 为 None:rect = [0.05,0.15,0.95,0.75]self.n = len(标题)self.angles = [a if a <=360.否则为-360.代表np.arange(90,90 + 360,360.0/self.n)]self.axes = [fig.add_axes(rect,projection="polar", label="axes%d" % i)因为我在范围内(self.n)]self.ax = self.axes[0]#显示标签self.ax.set_thetagrids(self.angles,labels = titles,fontsize = 14,weight ="bold",color ="black")对于 self.axes[1:] 中的 ax:ax.patch.set_visible(False)ax.grid(假)ax.xaxis.set_visible(False)self.ax.yaxis.grid(False)对于斧头,zip 中的角度(self.axes,self.angles):ax.set_rgrids(range(1,6),标签=标签,角度=角度,字体大小= 12)# 隐藏外脊(圆圈)ax.spines["polar"].set_visible(False)ax.set_ylim(0, 6)ax.xaxis.grid(True, color='black', linestyle='-')# 在每个标签的y轴上画一条线ax.tick_params(axis ='y',pad = 0,left = True,length = 6,width = 1,direction ='inout')def decorate_ticks(自己,坐标轴):对于 idx,在 enumerate(axes.xaxis.majorTicks) 中打勾:#打印(idx,tick.label._text)#获取网格线gl = tick.gridlinegl.set_marker('o')gl.set_markersize(15)如果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')# 这不会被使用.中心似乎与 5 没有什么不同别的:gl.set_markerfacecolor('黑色')如果 idx == 0 或 idx == 3:tick.set_pad(10)别的:tick.set_pad(30)def图(自身,值,* args,** kw):角度= np.deg2rad(np.r_ [self.angles,self.angles [0]])值= np.r_ [值,值[0]]self.ax.plot(角度,值,* args,** kw)无花果= plt.figure(1)标题= ['TG01','TG02','TG03','TG04','TG05','TG06']标签=列表("ABCDE")雷达=雷达(图,标题,标签)Radar.plot([3.75,3.25,3.0,2.75,4.25,3.5],-",线宽= 2,color ="b",alpha = .7,label ="Data01")Radar.plot([3.25,2.25,2.25,2.25,1.5,1.75],-",线宽= 2,color ="r",alpha = .7,label ="Data02")radar.decorate_ticks(radar.ax)# 这可以避免剪掉 thetagrid 标签下方的标记雷达.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()

和结果:

I've adapted this sample: Matplotlib: Polar plot axis tick label location. I want to add tick marks next to the tick labels (A, B, C, D and E) along the thetagrid from the center of the polar axis to the thetagrid labels. Also, I want a round circle ending each line, directly below the thetagrid titles (TG01, TG02, etc.). You will see these tick labels and thetagrid titles in my code sample. Visually, here's part of line TG01 to demonstrate what I'm looking for:

Here's my current code:

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

And how the polar plot currently looks:

I took a close look at the ThetaTick objects retrieved with ax.xaxis.majorTicks. However, nothing I changed in the properties of a ThetaTick object changed the lines in the way I would like them to render.

解决方案

I was able to get most of what I wanted by configuring the axes tick params for the y axis:

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

and the marker decorations below the theta tick labels with the decorate_ticks function (as shown in the code and the result). However, it appears the center marker is always the color and style of the last theta grid marker. I've noted this in a comment inside of the decorate_ticks function. If you know of a way to style the center marker differently, please let me know how.

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

and the result:

这篇关于在极坐标图的 Thetagrid 线上添加刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆