【问题标题】:How to remove the last tick in a polar plot如何删除极坐标图中的最后一个刻度
【发布时间】:2020-05-26 06:50:57
【问题描述】:

我想删除极坐标图中的最后一个刻度(2π)。我找到了一种非极地图的方法here,它说:

yticks[-1].set_visible(False)

导致:

AttributeError: 'PolarAxesSubplot' object has no attribute 'yticks'

我尝试写 rticks 而不是 yticks 但这产生了同样的错误。我在最后附上了一张图片。

我正在寻找一种等效的方法来删除最后一个刻度条目,例如非极坐标图。

import numpy as np
import matplotlib.pyplot as plt

def multiple_formatter(denominator=2, number=np.pi, latex='\pi'):
    # produces pi in the axis labels
    # https://stackoverflow.com/a/53586826
    def gcd(a, b):
        while b:
            a, b = b, a%b
        return a
    def _multiple_formatter(x, pos):
        den = denominator
        num = np.int(np.rint(den*x/number))
        com = gcd(num,den)
        (num,den) = (int(num/com),int(den/com))
        if den==1:
            if num==0:
                return r'$0$'
            if num==1:
                return r'$%s$'%latex
            elif num==-1:
                return r'$-%s$'%latex
            else:
                return r'$%s%s$'%(num,latex)
        else:
            if num==1:
                return r'$\frac{%s}{%s}$'%(latex,den)
            elif num==-1:
                return r'$\frac{-%s}{%s}$'%(latex,den)
            else:
                return r'$\frac{%s%s}{%s}$'%(num,latex,den)
    return _multiple_formatter

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r

ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])  # Less radial ticks
ax.set_rlabel_position(-22.5)  # Move radial labels away from plotted line
ax.grid(True)

ax.set_title("A line plot on a polar axis", va='bottom')
ax.xaxis.set_major_locator(plt.MultipleLocator(np.pi / 4))
ax.xaxis.set_minor_locator(plt.MultipleLocator(np.pi / 12))
ax.xaxis.set_major_formatter(plt.FuncFormatter(multiple_formatter(4)))

plt.show()

pi 标签来自here

结果:

【问题讨论】:

  • 另一个问得不好的问题。目前尚不清楚您要删除哪个刻度。这个问题太混乱了。如果要删除 2.0,那么为什么要首先设置它。正确解释你的问题和想要的数字,消除人们心中的困惑。否则,人们会继续回答不同的问题,而您将继续编辑和评论
  • 我编辑了我的问题,希望现在更清楚。刻度是由我正在使用的功能设置的。现在我正在寻找一种方法来摆脱它产生的最后一个条目,相当于非极坐标图的方法。
  • 如果我只使用 ax.xaxis.set_major_formatter(plt.FuncFormatter(multiple_formatter(4))) 并注释掉上面的 2 行,我会得到与问题中相同的图表,但没有 2pi

标签: python matplotlib axes polar-coordinates


【解决方案1】:

如果你改变了

ax.xaxis.set_major_locator(plt.MultipleLocator(np.pi / 4))

ax.xaxis.set_major_locator(plt.FixedLocator(np.arange(0,2*np.pi,np.pi/4)))

应该满足你的要求。

完整代码:

import numpy as np
import matplotlib.pyplot as plt

def multiple_formatter(denominator=2, number=np.pi, latex='\pi'):
    # produces pi in the axis labels
    # https://stackoverflow.com/a/53586826
    def gcd(a, b):
        while b:
            a, b = b, a%b
        return a
    def _multiple_formatter(x, pos):
        den = denominator
        num = np.int(np.rint(den*x/number))
        com = gcd(num,den)
        (num,den) = (int(num/com),int(den/com))
        if den==1:
            if num==0:
                return r'$0$'
            if num==1:
                return r'$%s$'%latex
            elif num==-1:
                return r'$-%s$'%latex
            else:
                return r'$%s%s$'%(num,latex)
        else:
            if num==1:
                return r'$\frac{%s}{%s}$'%(latex,den)
            elif num==-1:
                return r'$\frac{-%s}{%s}$'%(latex,den)
            else:
                return r'$\frac{%s%s}{%s}$'%(num,latex,den)
    return _multiple_formatter

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r

ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])  # Less radial ticks
ax.set_rlabel_position(-22.5)  # Move radial labels away from plotted line
ax.grid(True)

ax.set_title("A line plot on a polar axis", va='bottom')
ax.xaxis.set_major_locator(plt.FixedLocator(np.arange(0,2*np.pi,np.pi/4)))
ax.xaxis.set_minor_locator(plt.MultipleLocator(np.pi / 12))
ax.xaxis.set_major_formatter(plt.FuncFormatter(multiple_formatter(4)))

plt.show()

【讨论】:

  • 它正在工作并朝着我打算去的方向前进,但我想保留我目前正在使用的 pi 标记功能。我应该之前添加代码,我现在已经这样做了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多