python - 设置步长极坐标图 matplotlib python

标签 python matplotlib plot pycharm

目前我正在 matplotlib 中制作极坐标图。 不幸的是,步长为 10,如下所示。 Image 如何将步长更改为 6 而不是 10? 最小值和最大值需要自动保持,就像现在一样。 我尝试了 maxlags,但无法让它工作。

使用这行代码,我需要自动设置范围,这是不可能的,因为我有很多极坐标图。

ax.set_rmax(2)
ax.set_rticks([0.5, 1, 1.5, 2]) 

还有

ax.set_yticks(range(0, 60, 6), minor=False)  

不起作用。

这是当前使用的代码

import matplotlib
import os
import matplotlib.pyplot as plt

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

    # Plot values

    ax = plt.subplot(111, projection='polar')

    # Deze maakt 6 graden
    # ax.set_yticks(range(0, 60, 6), minor=False)  # Define the yticks



    plt.polar(angles, values, label=title, color="darkviolet")


    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.savefig(output)
    plt.show()

folder = "mapje"
for filename in os.listdir(folder):
    if filename.endswith(".txt"):
        inputPath = os.path.join(folder, filename)
        bareName = os.path.splitext(filename)[0]
        outputPath = os.path.join(folder, bareName+".pdf")
        title = "dB waarde terts " + bareName
        processDir(inputPath, outputPath, title)

在名为mapje的文件夹中使用文本文件“1kHz”:

0 54.3
5 54.4
10 54.2
15 54.4
20 54.6
25 54.4
30 54.2
35 54.4
40 54.1
45 54.2
50 54.4
55 54.5
60 54.4
65 54.5
70 54.5
75 54.8
80 55.2
85 55.3
90 55.6
95 55.6
100 55.6
105 55.9
110 56.5
115 56.3
120 56.3
125 56.3
130 56.6
135 56.8
140 57
145 57.1
150 57.5
155 57.5
160 57.5
165 57.5
170 57.2
175 57.1
180 57
185 56.2
190 56.6
195 56.5
200 56.6
205 56.9
210 56.8
215 56.7
220 56.9
225 57.1
230 57
235 57
240 56.8
245 57.1
250 57.1
255 57
260 57.3
265 57.3
270 57.4
275 57.3
280 57.4
285 57.3
290 57.1
295 57.2
300 57.1
305 57.1
310 57.1
315 57.3
320 57.2
325 57.3
330 57.2
335 57.3
340 56.9
345 57
355 56.8
360 54.3

最佳答案

解决这个问题的一种方法是:

import numpy as np
#retrieve automatically generated axis values
axmin = ax.get_rmin()
axmax = ax.get_rmax()
step = 6
#generate new ticklist with desired step size
axlist = np.arange(axmin, axmax + step, step)
#set new ticks
ax.set_rticks(axlist) 

输出

enter image description here

关于python - 设置步长极坐标图 matplotlib python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50385348/

相关文章:

python - 在 Linux 到 Windows (Flask) 中将模式管道传输到 sqlite 的对应物是什么

python - 在 Pandas 中,如何根据另一列的平均值创建具有排名的新列

python - Pandas 绘图栏 : Show every nth xlabel

r - 格式化和操作来自 R 包 "hexbin"的绘图

r - 重新排序因子后正确绘制 xyplot 线

python - 如何使用 matplotlib 在 python 中绘制时间戳?

python - 在 Pandas 中分配日期

python - QML Python 中的自定义对象引用

python - 如何在条形图上清楚地呈现每个数据的标题?

python - Matplotlib 动画在 PyCharm 中不起作用