我有兴趣为我的项目使用圆形条形图可视化,并且不知道如何在Python中生成它。请看一个我的意思的例子"圆形条形图"下面。数据将以大熊猫系列的形式出现 - 下面的虚拟示例模糊地反映了情节:
A 33
B 62
C 56
D 70
有什么想法吗?
答案 0 :(得分:6)
这只是极地投影中的水平条形图。 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))
但可以调整:
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'])
必须有办法将径向标签移动到条形图的左侧,但我找不到它。
PS 更简洁,更快捷的方式:
ax.barh([0, 1, 2, 3], np.radians([150, 300, 270, 320]),
color=plt.rcParams['axes.prop_cycle'].by_key()['color'])
答案 1 :(得分:2)
你也可以利用被解雇的甜甜圈情节:
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()
输出:
答案 2 :(得分:1)
这可以使用matplotlib完成。诀窍是在极坐标中绘制条形图。看一下这个来自matplotlib文档的例子: https://matplotlib.org/1.2.1/examples/pylab_examples/polar_bar.html
其他种类的"圆形条形图"可以根据这些例子制作: https://python-graph-gallery.com/donut-plot/
有关https://datavizcatalogue.com/methods/donut_chart.html
的更多信息但是,我不知道使用matplotlib获取您正在寻找的内容的直接方式。