在python中创建圆形条形图 [英] Create a circular barplot in python
问题描述
我会对在项目中使用圆形条形图可视化感兴趣,并且不知道如何在Python中生成它.请在下面查看我对圆形barplot"的含义的示例.数据将以熊猫系列的形式出现-下面的虚拟示例模糊地反映了该图:
I would be interested in using a circular barplot visualisation for my project, and have no idea whatsoever how to produce it in Python. Please see an example of what I mean for "circular barplot" below. Data would come in form of a pandas series - dummy example below to vaguely reflect the plot:
A 33
B 62
C 56
D 70
有什么主意吗?
推荐答案
那只是极坐标投影中的水平条形图. Matplotlib的默认设置会使它看起来有所不同.
That is just an horizontal bar plot in polar projection. Matplotlib defaults will make it look a bit different.
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()
设置刻度和标签.
- Use
set_theta_zero_location()
to make the bars start at North. - Use
set_theta_direction()
to make the bars go clockwise. - Use
set_rlabel_position()
to move the radial labels. - Use
set_thetagrids()
andset_rgrids()
to set the ticks and labels.
结果非常相似:
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'])
必须有一种方法可以将径向标签移动到条形的左侧,但我找不到它.
There must be a way to move the radial labels to the left of the bars but I could not find it.
PS :一种更简洁甚至更快的方法:
PS A more concise and maybe faster way:
ax.barh([0, 1, 2, 3], np.radians([150, 300, 270, 320]),
color=plt.rcParams['axes.prop_cycle'].by_key()['color'])
这篇关于在python中创建圆形条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!