如何使用Python和pyplot将标准直方图数据绘制为极坐标直方图?

时间:2018-02-13 14:55:09

标签: python matplotlib histogram polar-coordinates

我有一个以度为单位的角度列表。我想显示一个极坐标直方图,其中[0°,360°]值的范围被细分为相等的区间,并显示角度列表中有多少值落入每个区域。我使用以下代码得到直方图数据(我已经检查过它是正确的):

bins_number = 8 # the [0, 360) interval will be subdivided into this number of equal bins
bins = np.linspace(0.0, 360.0, bins_number + 1)
n, _, _ = plt.hist(angles, bins)

现在,我尝试使用以下代码将此数据绘制成极坐标直方图:

plt.clf()
width = 2 * np.pi / bins_number
ax = plt.subplot(1, 1, 1, projection='polar')
bars = ax.bar(bins[:bins_number], n, width=width, bottom=0.0)
for bar in bars:
    bar.set_alpha(0.5)
plt.show()

但是我得到的是这张图片: enter image description here

正如您所看到的,条形没有以正确的角度放置,并且它们中的一些相互重叠,而它们应该是连续的而不重叠。

我做错了什么?提前谢谢。

2 个答案:

答案 0 :(得分:1)

与评论一样,使用弧度代替度数:

import numpy as np
import matplotlib.pyplot as plt

n_numbers = 100
bins_number = 8  # the [0, 360) interval will be subdivided into this
# number of equal bins
bins = np.linspace(0.0, 2 * np.pi, bins_number + 1)
angles = 2 * np.pi * np.random.rand(n_numbers)
n, _, _ = plt.hist(angles, bins)

plt.clf()
width = 2 * np.pi / bins_number
ax = plt.subplot(1, 1, 1, projection='polar')
bars = ax.bar(bins[:bins_number], n, width=width, bottom=0.0)
for bar in bars:
    bar.set_alpha(0.5)
plt.show()

enter image description here

答案 1 :(得分:0)

这里仅是绘制垃圾箱的中心与每个垃圾箱中角度的出现次数的关系图

@RunWith(AndroidJUnit4::class)
class MyMediaClassTest {

    @Rule
    @JvmField
    val activityRule = ActivityTestRule(MainActivity::class.java)

    @Rule
    @JvmField
    val grantPermissionRule: GrantPermissionRule = GrantPermissionRule
            .grant(android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
// tests and stuff
}

enter image description here