设置步长极坐标图 matplotlib python [英] Set step size polar plot matplotlib python

查看:34
本文介绍了设置步长极坐标图 matplotlib python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently i'm making a polar plot in matplotlib. Unfortionaly the step size is 10, like shown below. 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]) 

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":

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

推荐答案

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

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) 

输出

这篇关于设置步长极坐标图 matplotlib python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆