在python中创建一个圆形的barplot

fam*_*gar 4 python matplotlib bar-chart

我有兴趣为我的项目使用圆形条形图可视化,并且不知道如何在Python中生成它.请看下面我对"圆形条形图"的意思.数据将以大熊猫系列的形式出现 - 下面的虚拟示例模糊地反映了情节:

A 33
B 62
C 56
D 70
Run Code Online (Sandbox Code Playgroud)

任何的想法?

在这里,使用R.

Sto*_*ica 7

这只是极坐标投影中的水平条形图.Matplotlib的默认设置会使它看起来有点不同.

ax = plt.subplot(projection='polar')
ax.barh(0, math.radians(150))
ax.barh(1, math.radians(300))
ax.barh(2, math.radians(270))
ax.barh(3, math.radians(320))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但它可以调整:

  • 使用set_theta_zero_location()使酒吧北开始.
  • 使用set_theta_direction()使棒顺时针方向走.
  • 使用set_rlabel_position()移动径向标签.
  • 使用set_thetagrids()set_rgrids()设置刻度和标签.

结果非常相似:

ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_rlabel_position(0)
ax.set_thetagrids([0, 96, 192, 288], labels=[0, 20, 40, 60])
ax.set_rgrids([0, 1, 2, 3], labels=['a', 'b', 'c', 'd'])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

必须有一种方法将径向标签移动到条形图的左侧,但我找不到它.

PS更简洁,也许更快的方式:

ax.barh([0, 1, 2, 3], np.radians([150, 300, 270, 320]),
        color=plt.rcParams['axes.prop_cycle'].by_key()['color'])
Run Code Online (Sandbox Code Playgroud)


Mr.*_*. T 6

您还可以利用被解雇的甜甜圈图:

import matplotlib.pyplot as plt
from matplotlib import cm
from math import log10

labels = list("ABCDEFG")
data = [21, 57, 88, 14, 76, 91, 26]
#number of data points
n = len(data)
#find max value for full ring
k = 10 ** int(log10(max(data)))
m = k * (1 + max(data) // k)

#radius of donut chart
r = 1.5
#calculate width of each ring
w = r / n 

#create colors along a chosen colormap
colors = [cm.terrain(i / n) for i in range(n)]

#create figure, axis
fig, ax = plt.subplots()
ax.axis("equal")

#create rings of donut chart
for i in range(n):
    #hide labels in segments with textprops: alpha = 0 - transparent, alpha = 1 - visible
    innerring, _ = ax.pie([m - data[i], data[i]], radius = r - i * w, startangle = 90, labels = ["", labels[i]], labeldistance = 1 - 1 / (1.5 * (n - i)), textprops = {"alpha": 0}, colors = ["white", colors[i]])
    plt.setp(innerring, width = w, edgecolor = "white")

plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明