Generating Compound Pie, or Pie of Pie Charts

2024/10/10 20:17:29

Below is an example of a compound pie chart, also known as a pie of pie chart drawn using Excel. Is it possible to create a figure like this using python?

enter image description here

Answer

Yes, this is possible with matplotlib - below is an example adapted from here.

Code:

import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np# make figure and assign axis objects
fig = plt.figure(figsize=(9, 5.0625))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
fig.subplots_adjust(wspace=0)# large pie chart parameters
ratios = [.27, .56, .17]
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)# small pie chart parameters
ratios = [.20, .20, .60]
labels = ['Male', 'Female', 'Undecided']
width = .2ax2.pie(ratios, autopct='%1.1f%%', startangle=angle,labels=labels, radius=0.5, textprops={'size': 'smaller'})ax1.set_title('Approval')
ax2.set_title('Gender')# use ConnectionPatch to draw lines between the two plots
# get the wedge data
theta1, theta2 = ax1.patches[0].theta1, ax1.patches[0].theta2
center, r = ax1.patches[0].center, ax1.patches[0].r# draw top connecting line
x = r * np.cos(np.pi / 180 * theta2) + center[0]
y = np.sin(np.pi / 180 * theta2) + center[1]
con = ConnectionPatch(xyA=(- width / 2, .5), xyB=(x, y),coordsA="data", coordsB="data", axesA=ax2, axesB=ax1)
con.set_color([0, 0, 0])
con.set_linewidth(2)
ax2.add_artist(con)# draw bottom connecting line
x = r * np.cos(np.pi / 180 * theta1) + center[0]
y = np.sin(np.pi / 180 * theta1) + center[1]
con = ConnectionPatch(xyA=(- width / 2, -.5), xyB=(x, y), coordsA="data",coordsB="data", axesA=ax2, axesB=ax1)
con.set_color([0, 0, 0])
ax2.add_artist(con)
con.set_linewidth(2)plt.show()

Output:

enter image description here

https://en.xdnf.cn/q/69852.html

Related Q&A

GitPython : git push - set upstream

Im using GitPython to clone a master branch and do a checkout of a feature branch, I do my local updates, commit and push back to git. The code snippet looks like below, Note : my branch name is featur…

How to access multi-level index in pandas data frame?

I would like to call those row with same index.so this is the example data frame, arrays = [np.array([bar, bar, baz, baz, foo, foo, qux, qux]), np.array([one, two, one, two, one, two, one, two])]df = p…

How to represent graphs with IPython

Recently I discovered IPython notebook which is a powerful tool. As an IT student, I was looking for a way to represent graphs in Python. For example, I would like to know if theres a library (like num…

How to check TypeVars Type at runtime

I have a generic class Graph[Generic[T], object]. My question, is there any function which returns type passed as generic to the class Graph>>> g = Graph[int]() >>> magic_func(g) <…

Can django-pagination do multiple paginations per page?

If it cant then are there any other alternatives (either Djangos native pagination or an alternate package) that allows multiple paginations per page?I would like to display a list of about 5 objects …

List directory file contents in a Django template

Im just learning Python & Django. (Thanks to everyone who contributes here -- its been an invaluable resource!)One seemingly basic thing that Im having trouble with is rendering a simple list of s…

Produce PDF files, draw polygons with rounded corners

Whats the right tool for the job if I want to write a Python script that produces vector graphics in PDF format? In particular, I need to draw filled polygons with rounded corners (i.e., plane figures…

How can I unit test this Flask app?

I have a Flask app that is using Flask-Restless to serve an API.I have just written some authentication that checksIf the consumers host is recognised The request includes a hash (calculated by encrypt…

How to completely reset Python stdlib logging module in an ipython session?

Id like to make repeated calls to Python scripts using %run in an ipython session, and for each of those scripts to log based on cmdline arguments passed via %run.For example while debugging cmd.py I m…

I think Librosa.effect.split has some problem?

firstly, this function is to remove silence of an audio. here is the official description:https://librosa.github.io/librosa/generated/librosa.effects.split.htmllibrosa.effects.split(y, top_db=10, *karg…