设置步长极坐标图matplotlib python

时间:2018-05-17 07:05:13

标签: 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)

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

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

1 个答案:

答案 0 :(得分:2)

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

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