Set step size polar plot matplotlib python

2019-07-20 12:01发布

Currently i'm making a polar plot in matplotlib. Unfortionaly the step size is 10, like shown below. Image How can i change the step size to 6 instead of 10? The min and max value need to stay automatically, like it is now. I tried maxlags, but I can't get it to work.

With this lines of code i need to set the ranges automatically, which is impossible because I have a lot of polar plots.

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

Also

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

Does not work.

Here is the currently used code

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)

With text file "1kHz" in the folder called 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条回答
仙女界的扛把子
2楼-- · 2019-07-20 12:50

One way to approach this would be:

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) 

Output

enter image description here

查看更多
登录 后发表回答