如何删除极坐标图中的最后一个勾号

2024-04-25 09:07:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

结果:

enter image description here


Tags: comreturnifformatterdefnppiplt
1条回答
网友
1楼 · 发布于 2024-04-25 09:07:27

如果你改变

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

相关问题 更多 >

    热门问题