I've got a pie chart (example) with following fracs = [10, 20, 50, 30]. Drawing this with matplotlib is no problem. How do I get a breakdown of the first wedge (10) into 6 and 4? Ideally, I want a second wedge for the 20, to breakdown into 10, 3, 7. This would be displayed as a barchart near the specific wedge or a pie chart (which would make it a pie of pie chart similar to the ones in Excel).
Here is one way of doing it (possibly not the best...). I've adapted some of the code found here, on the matplotlib site to make a little_pie function, that will draw small pie charts at arbitrary positions.
from pylab import *
import math
import numpy as np
def little_pie(breakdown,location,size):
breakdown = [0] + list(np.cumsum(breakdown)* 1.0 / sum(breakdown))
for i in xrange(len(breakdown)-1):
x = [0] + np.cos(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi *
breakdown[i+1], 20)).tolist()
y = [0] + np.sin(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi *
breakdown[i+1], 20)).tolist()
xy = zip(x,y)
scatter( location[0], location[1], marker=(xy,0), s=size, facecolor=
['gold','yellow', 'orange', 'red','purple','indigo','violet'][i%7])
figure(1, figsize=(6,6))
little_pie([10,3,7],(1,1),600)
little_pie([10,27,4,8,4,5,6,17,33],(-1,1),800)
fracs = [10, 8, 7, 10]
explode=(0, 0, 0.1, 0)
pie(fracs, explode=explode, autopct='%1.1f%%')
show()

I couldn't find a solution for this, so I hacked my own. I used the ConnectionPatch object in the matplotlib.patches module. This allows you to draw lines between different axes in the same figure. The following creates a pie chart on the left and a stacked bar on the right:
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np
import math
# style choice
plt.style.use('fivethirtyeight')
# make figure and assign axis objects
fig = plt.figure(figsize=(15,7.5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
# pie chart parameters
ratios = [.4, .56, .04]
labels = ['Approve', 'Disapprove', 'Undecided']
explode=[0.1,0,0]
# rotate so that first wedge is split by the x-axis
angle = -180*ratios[0]
ax1.pie(ratios, autopct='%1.1f%%', startangle=angle,
labels=labels,explode=explode )
# bar chart parameters
xpos = 0
bottom = 0
ratios = [.33, .54, .07, .06]
width = .2
colors = ['y','m','#99ff99','#ffcc99']
for j in range(len(ratios)):
height = ratios[j]
ax2.bar(xpos, height, width, bottom=bottom, color=colors[j])
ypos = bottom + ax2.patches[j].get_height()/2
bottom += height
ax2.text(xpos,ypos, "%d%%" %
(ax2.patches[j].get_height()*100), ha='center')
plt.title('Gender of approvers')
plt.legend(('Women', 'Men', 'Gender Neutral', 'Alien'))
plt.axis('off')
plt.xlim(-2.5*width, 2.5*width)
Then I add two lines connected the first wedge of the pie chart with the top and bottom, respectively, of the stacked bar plot:
# use ConnectionPatch to draw lines between the two plots
# get the wedge data for the first group
theta1, theta2 = ax1.patches[0].theta1, ax1.patches[0].theta2
center, r = ax1.patches[0].center, ax1.patches[0].r
bar_height = sum([item.get_height() for item in ax2.patches])
x = r*np.cos(math.pi/180*theta2)+center[0]
y = np.sin(math.pi/180*theta2)+center[1]
con = ConnectionPatch(xyA=(-width/2,bar_height), xyB=(x,y),
coordsA="data", coordsB="data", axesA=ax2, axesB=ax1)
con.set_color([0,0,0])
con.set_linewidth(4)
ax2.add_artist(con)
x = r*np.cos(math.pi/180*theta1)+center[0]
y = np.sin(math.pi/180*theta1)+center[1]
con = ConnectionPatch(xyA=(-width/2,0), xyB=(x,y),
coordsA="data", coordsB="data", axesA=ax2, axesB=ax1)
con.set_color([0,0,0])
ax2.add_artist(con)
con.set_linewidth(4)
plt.show()
Here is the plot:

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