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
Any idea?
A circular barplot is a plot where each bar is displayed along a circle instead of a line. Here no Y scale is displayed since exact values are written on each bar which could also be represented on interaction with the chart.
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))
But it can be tuned:
set_theta_zero_location()
to make the bars start at North.set_theta_direction()
to make the bars go clockwise.set_rlabel_position()
to move the radial labels.set_thetagrids()
and set_rgrids()
to set the ticks and labels.The result is very similar:
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 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'])
You could also make use of the dismissed donut plot:
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()
Output:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With