极标间隔matplotlib

2024-04-23 16:12:57 发布

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

matplotlib的标准间隔为45°。 如何将间隔改为15°? 所以我从下面的图片开始

45 degrees interval

到这个图像

15 degrees interval

问题是我找不到合适的文件。 这是当前使用的代码:

#Define arrays
def processDir(file, output, title):
    angles=list()
    values=list()
lines = [line.rstrip('\n') for line in open(file)]
for line in lines: # Iterate lines
    stringElement = str.split(line, " ") # Split elements
    angle = int(stringElement[0])
    value = float(stringElement[1])

    angles.append((angle/360)*2*3.1415926)
    values.append(value)
ax = plt.subplot(111, projection='polar')
plt.polar(angles, values, label=title, color="darkviolet")


ax.legend(bbox_to_anchor=(0., 1.1, -1., .102), loc=3,
       ncol=2, mode="expand", borderaxespad=0., frameon=False)

ax.grid(True)
axmin = ax.get_rmin()
axmax = ax.get_rmax()
#Zorg ervoor dat de stap 6dB per lijn is
step = 6
# generate new ticklist with desired step size
axlist = np.arange(axmin, axmax + step, step)
# set new ticks
ax.set_rticks(axlist)

ax.figure.set_size_inches(8, 5)
plt.savefig(output)
plt.show()


processDir("test.txt", "no output", "My Title")

使用文本文件:

0 54.3
15 54.4
30 54.2
45 54.2
60 54.4
75 54.8
90 55.6
120 56.3
150 57.5
180 57
210 56.8
240 56.8
270 57.4
300 57.1
330 57.2
360 54.3

Tags: output间隔titlesteplinepltaxlist
1条回答
网友
1楼 · 发布于 2024-04-23 16:12:57

您应该始终提供Minimal, Complete, and Verifiable example,否则问题可能会被搁置。
幸运的是,我记得your previous question。也可以在极坐标图中调整x记号。使用以前的示例代码:

def processDir(file, output, title):
    angles=list()
    values=list()
    lines = [line.rstrip('\n') for line in open(file)]
    for line in lines: # Iterate lines
        stringElement = str.split(line, " ") # Split elements
        angle = int(stringElement[0])
        value = float(stringElement[1])

        angles.append((angle/360)*2*3.1415926)
        values.append(value)

    ax = plt.subplot(111, projection='polar')
    plt.polar(angles, values, label=title, color="darkviolet")
    #here we set the xticks to 24 values, i.e. 15°
    ax.set_xticks(np.linspace(0,  2 * np.pi, 24, endpoint = False))

    ax.text(-0.4, 0.3, 'Test\nTest', horizontalalignment = 'center', verticalalignment = 'center', transform = ax.transAxes)
    ax.legend(bbox_to_anchor=(0., 1.1, 1., .102), loc=3,
           ncol=2, mode="expand", borderaxespad=0., frameon=False)

    ax.grid(True)
        ax.figure.set_size_inches(8, 5)
    plt.show()

processDir("test.txt", "no output", "My Title")

回顾你以前的问题,你同样可以解决你以前的问题:

ax.set_yticks(np.linspace(0, 60, 11))

如果我们也将其整合,输出结果将是:

enter image description here

相关问题 更多 >