为什么我在极轴上的条形图不能从圆心开始绘制

时间:2019-06-13 02:24:58

标签: python matplotlib

我想使用matplotlib.axes.Axes.bar绘制以r = 0.9为中心的径向轴的极线图,这里是示例:https://matplotlib.org/gallery/pie_and_polar_charts/polar_bar.html#sphx-glr-gallery-pie-and-polar-charts-polar-bar-py 我对示例进行了一些更改以适合我的数据,这是我的代码

import numpy as np
import matplotlib.pyplot as plt

N = 15
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
r = np.array(
    [0.9928, 0.9854, 0.9829, 0.9794, 0.9727, 0.9698, 0.9657, 0.9641, 0.9651, 0.9482, 0.9557, 0.9404, 0.9360, 0.9270,
     0.9253])
width = np.array([0.4] * N)
label = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "m", "n", "o", "p"]

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

ax.set_rlim(0.9, 1)
ax.set_rticks(np.arange(0.94, 1, 0.02))
ax.set_thetagrids(theta * 180 / np.pi)
ax.set_rlabel_position(-15)

ax.bar(x=theta, height=r, width=width, bottom=0.0, alpha=0.5, tick_label=label)

plt.show()

但是我发现圆的中心非常整齐,似乎条形图的起点不是r = 0.9(圆的中心)。

情节看起来像这样:

箭头是圆的中心。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您想从r = 0.9开始绘图。为此,也让您的柱线从r = 0.9开始而不是r = 0。

import numpy as np
import matplotlib.pyplot as plt

N = 15
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
r = np.array(
    [0.9928, 0.9854, 0.9829, 0.9794, 0.9727, 0.9698, 0.9657, 0.9641, 0.9651, 0.9482, 0.9557, 0.9404, 0.9360, 0.9270,
     0.9253])
width = np.array([0.4] * N)
label = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "m", "n", "o", "p"]

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

ax.set_rlim(0.9, 1)
ax.set_rticks(np.arange(0.94, 1, 0.02))
ax.set_thetagrids(theta * 180 / np.pi)
ax.set_rlabel_position(-15)

ax.bar(x=theta, height=r-.9, width=width, bottom=0.9, alpha=0.5, tick_label=label)

plt.show()

enter image description here

答案 1 :(得分:-1)

这些行似乎是导致问题的原因:

ax.set_rlim(0.9, 1)

ax.bar(x=theta, height=r, width=width, bottom=0.0, alpha=0.5, tick_label=label)

其中一行将rlim设置为0.9,另一行将bottom设置为0.0。中心附近的条形图的最后一位未被渲染,因为我认为是零除,当条形图意外到达图的中心时,会夹住条形图。

极极图对我来说很好,只需对代码进行以下少量更改即可:

import numpy as np
import matplotlib.pyplot as plt

r_min = 0.9
r_max = 1.0

N = 15
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
r = np.array(
    [0.9928, 0.9854, 0.9829, 0.9794, 0.9727, 0.9698, 0.9657, 0.9641, 0.9651, 0.9482, 0.9557, 0.9404, 0.9360, 0.9270,
     0.9253])
width = np.array([0.4] * N)
label = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "m", "n", "o", "p"]

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

ax.set_rlim(r_min, r_max)
ax.set_rticks(np.arange(0.94, 1, 0.02))
ax.set_thetagrids(theta * 180 / np.pi)
ax.set_rlabel_position(-15)
ax.set_yticklabels([])

ax.bar(x=theta, height=r-r_min, width=width, bottom=r_min, alpha=0.5, tick_label=label)

plt.show()