Creating a pie chart in python

I would be interested to use the circular bar code visualization for my project and don’t know how to do it in Python. Please see an example of what I mean for a round burplot below. The data will be presented as a series of pandas - a dummy example below, to vaguely reflect the graph:

A 33
B 62
C 56
D 70

Any idea?

here using r

+4
source share
3 answers

This is just a horizontal graph in polar projection. By default, Matplotlib values ​​will look slightly 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))

enter image description here

But it can be configured:

  • Use set_theta_zero_location()to start the bars from the north.
  • Use the set_theta_direction()bars to go clockwise.
  • 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'])

enter image description here

, .

PS , , :

ax.barh([0, 1, 2, 3], np.radians([150, 300, 270, 320]),
        color=plt.rcParams['axes.prop_cycle'].by_key()['color'])
+6

:

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

:

enter image description here

+2

Source: https://habr.com/ru/post/1695969/


All Articles