Skip to content Skip to sidebar Skip to footer

Circular Barplot In Python With Percentage Labels

I am a newbie to python and have little experience in R. I have a data frame with gut_list and percentage. I want to create a circular barplot/race track plot with labels. I saw a

Solution 1:

Here is some sample code to create such a "concentric circle chart" aka "concentric rings chart".

The main idea is to use the x array of the pie to indicate how much of the circle to use. And only put one x at a time. The docs tell that if the total of x is smaller than 1, it will be taken as a percentage (otherwise everything will be summed and shown proportionally to the sum, which would make the single x 100%). counterclock=False will have the arc in the desired direction. The single x is now recalculated such that the largest arc will be the percentage as set in the percentage list.

Important to note is that both an outer radius r and an inner radius is needed. In the current code the inner radius only plays a role to calculate the width step which offsets each circle.

The pie chart can display labels on the pie parts themselves, but the automatic placement can be confusing in our case. Setting labels=['desired label'] will get the label into the legend. Setting labeldistance=None will have it not displayed on the chart. The legend can be placed such that its upper right corner is at the top center of the chart. Place it elsewhere when the percentages are too high and the arcs would overlap.

Alternatively, the text could be displayed directly next to the arcs. In data coordinates the center of the circles is at 0,0. So, y=radius-w/2 is at the center of the starting edge of each ring. The text gets right aligned and vertically centered.

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 inrange(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()

resulting plot

Post a Comment for "Circular Barplot In Python With Percentage Labels"