Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: pie chart , variable pctdistance

I want to be able to position each percentage value at different distance from the center, but pctdistance needs to be a single value.

For my case pctdistance should be a list that would contain generated distances (generated by a range).

import matplotlib.pyplot as plt

fig =plt.figure(figsize = (10,10))
ax11 = fig.add_subplot(111)
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 2000]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0)  # explode 1st slice

# Plot
w,l,p = ax11.pie(sizes,  labels=labels, colors=colors,
        autopct='%1.1f%%', startangle=140, pctdistance=0.8, radius = 0.5)
[t.set_rotation(0) for t in p]
[t.set_fontsize(50) for t in p]
plt.axis('equal')
plt.show()

What I have: enter image description here What I want: enter image description here

like image 531
Silvia Avatar asked Jan 28 '23 00:01

Silvia


1 Answers

The pie function does not take lists or arrays as input for the pctdistance argument.

You may position the texts manually using a predefined list of pctdistances.

import numpy as np
import matplotlib.pyplot as plt

fig =plt.figure(figsize = (4,4))
ax11 = fig.add_subplot(111)
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 2000]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']

# Plot
w,l,p = ax11.pie(sizes,  labels=labels, colors=colors,
        autopct='%1.1f%%', startangle=140, pctdistance=1, radius = 0.5)

pctdists = [.8, .5, .4, .2]

for t,d in zip(p, pctdists):
    xi,yi = t.get_position()
    ri = np.sqrt(xi**2+yi**2)
    phi = np.arctan2(yi,xi)
    x = d*ri*np.cos(phi)
    y = d*ri*np.sin(phi)
    t.set_position((x,y))

plt.axis('equal')
plt.show()

enter image description here

like image 107
ImportanceOfBeingErnest Avatar answered Jan 30 '23 14:01

ImportanceOfBeingErnest