Python中带有百分比标签的圆形barplot

问题描述 投票:0回答:1

我是python的新手,对R的经验很少。

我有一个带有gut_list和百分比的数据框。我想创建一个带有标签的圆形barplot / race轨迹图。我在R ggplot中看到了一个帖子,用于创建与圣殿中心相似的内容。但是我不确定如何在python中使用ggplot。

我想要一个与以下R组类似的输出。但是我想尝试使用python包来创建带有标记值和图例的图。

Making a circular barplot with a hollow center (aka race track plot)

示例数据:

gut_list = ("Micro1", "Micro2", "Micro3", "Micro4", "Micro5", "Micro6")
percent = (2, 77, 22, 41, 21, 9)

初审:

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

gut_list = ("Micro1", "Micro2", "Micro3", "Micro4", "Micro5", "Micro6") 
percent = [2, 77, 22, 41, 21, 9]
#number of data points
n = len(percent)
#find max value for full ring
k = 10 ** int(log10(max(percent)))
m = k * (1 + max(percent) // k)

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

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

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

for i in range(n):
    innerring, _ = ax.pie([m - percent[i], percent[i]], radius = r - i * w, startangle = 90, colors = ["white", colors[i]])
    plt.setp(innerring, width = w, edgecolor = "lightgrey")


plt.legend()
plt.show()

我仍然没有设法添加标签或图例。

预期结果是(i.stack.imgur.com/hOv9q.png)enter image description here

python matplotlib data-visualization donut-chart python-ggplot
1个回答
0
投票

这里有一些示例代码可创建这样的“同心圆图”,又称为“同心圆图”。

主要思想是使用xpie数组指示要使用的圆数。并且一次只输入一个xdocs告诉我们,如果x的总和小于1,则将其作为一个百分比(否则,将对所有总和求和并与之成比例显示,这将使单个x为100%)。 counterclock=False将在所需方向上形成弧。现在重新计算单个x,以使最大弧将成为百分比列表中设置的百分比。

要注意的是,既需要外半径r,也需要内半径。在当前代码中,内半径仅在计算使每个圆偏移的width步幅中起作用。

饼形图可以在饼形零件本身上显示标签,但是在我们的情况下,自动放置可能会造成混淆。设置labels=['desired label']将使标签进入图例。设置labeldistance=None将使其不显示在图表上。可以放置图例,使其图例的右上角在图表的顶部中心。当百分比过高并且弧线重叠时,请将其放置在其他位置。

或者,文本可以直接显示在圆弧旁边。在data coordinates中,圆圈的中心位于data coordinates。因此,0,0位于每个环的起始边缘的中心。文本右对齐并垂直居中。

y=radius-w/2

import matplotlib.pyplot as plt cathegories = ["Electronics", "Appliances", "Books", "Music", "Clothing", "Cars", "Food/Beverages", "Personal Hygiene", "Personal Health/OTC", "Hair Care"] percent = [81, 77, 70, 69, 69, 68, 62, 62, 61, 60] # number of data points n = len(percent) # percent of circle to draw for the largest circle percent_circle = max(percent) / 100 r = 1.5 # outer radius of the chart r_inner = 0.4 # inner radius of the chart # calculate width of each ring w = (r - r_inner) / n # create colors along a chosen colormap #colors = [plt.cm.plasma(i / n) for i in range(n)] colors = plt.cm.tab10.colors # create figure, axis fig, ax = plt.subplots() ax.axis("equal") for i in range(n): radius = r - i * w ax.pie([percent[i] / max(percent) * percent_circle], radius=radius, startangle=90, counterclock=False, colors=[colors[i]], labels=[f'{cathegories[i]} – {percent[i]}%'], labeldistance=None, wedgeprops={'width': w, 'edgecolor': 'white'}) ax.text(0, radius - w / 2, f'{cathegories[i]} – {percent[i]}% ', ha='right', va='center') # plt.legend(loc='upper right', bbox_to_anchor=(0.5, 1.1), prop={'size': 12}) plt.tight_layout() plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.