Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 93879f8

Browse filesBrowse files
authored
Merge pull request matplotlib#11865 from pjk645/bar_of_pie
example file for making a bar of pie chart
2 parents ed8df70 + 70df299 commit 93879f8
Copy full SHA for 93879f8

File tree

Expand file treeCollapse file tree

2 files changed

+93
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+93
-0
lines changed

‎.flake8

Copy file name to clipboardExpand all lines: .flake8
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ per-file-ignores =
182182
examples/misc/table_demo.py: E201
183183
examples/mplot3d/voxels.py: E501
184184
examples/mplot3d/wire3d_zero_stride.py: E501
185+
examples/pie_and_polar_charts/bar_of_pie.py: E402
185186
examples/pie_and_polar_charts/nested_pie.py: E402
186187
examples/pie_and_polar_charts/pie_and_donut_labels.py: E402
187188
examples/pie_and_polar_charts/pie_demo2.py: E402
+92Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
==========
3+
Bar of pie
4+
==========
5+
Make a "bar of pie" chart where the first slice of the pie is
6+
"exploded" into a bar chart with a further breakdown of said slice's
7+
characteristics. The example demonstrates using a figure with multiple
8+
sets of axes and using the axes patches list to add two ConnectionPatches
9+
to link the subplot charts.
10+
"""
11+
12+
import matplotlib.pyplot as plt
13+
from matplotlib.patches import ConnectionPatch
14+
import numpy as np
15+
16+
# make figure and assign axis objects
17+
fig = plt.figure(figsize=(9, 5.0625))
18+
ax1 = fig.add_subplot(121)
19+
ax2 = fig.add_subplot(122)
20+
fig.subplots_adjust(wspace=0)
21+
22+
# pie chart parameters
23+
ratios = [.27, .56, .17]
24+
labels = ['Approve', 'Disapprove', 'Undecided']
25+
explode = [0.1, 0, 0]
26+
# rotate so that first wedge is split by the x-axis
27+
angle = -180 * ratios[0]
28+
ax1.pie(ratios, autopct='%1.1f%%', startangle=angle,
29+
labels=labels, explode=explode)
30+
31+
# bar chart parameters
32+
33+
xpos = 0
34+
bottom = 0
35+
ratios = [.33, .54, .07, .06]
36+
width = .2
37+
colors = [[.1, .3, .5], [.1, .3, .3], [.1, .3, .7], [.1, .3, .9]]
38+
39+
for j in range(len(ratios)):
40+
height = ratios[j]
41+
ax2.bar(xpos, height, width, bottom=bottom, color=colors[j])
42+
ypos = bottom + ax2.patches[j].get_height() / 2
43+
bottom += height
44+
ax2.text(xpos, ypos, "%d%%" % (ax2.patches[j].get_height() * 100),
45+
ha='center')
46+
47+
ax2.set_title('Age of approvers')
48+
ax2.legend(('50-65', 'Over 65', '35-49', 'Under 35'))
49+
ax2.axis('off')
50+
ax2.set_xlim(- 2.5 * width, 2.5 * width)
51+
52+
# use ConnectionPatch to draw lines between the two plots
53+
# get the wedge data
54+
theta1, theta2 = ax1.patches[0].theta1, ax1.patches[0].theta2
55+
center, r = ax1.patches[0].center, ax1.patches[0].r
56+
bar_height = sum([item.get_height() for item in ax2.patches])
57+
58+
# draw top connecting line
59+
x = r * np.cos(np.pi / 180 * theta2) + center[0]
60+
y = np.sin(np.pi / 180 * theta2) + center[1]
61+
con = ConnectionPatch(xyA=(- width / 2, bar_height), xyB=(x, y),
62+
coordsA="data", coordsB="data", axesA=ax2, axesB=ax1)
63+
con.set_color([0, 0, 0])
64+
con.set_linewidth(4)
65+
ax2.add_artist(con)
66+
67+
# draw bottom connecting line
68+
x = r * np.cos(np.pi / 180 * theta1) + center[0]
69+
y = np.sin(np.pi / 180 * theta1) + center[1]
70+
con = ConnectionPatch(xyA=(- width / 2, 0), xyB=(x, y), coordsA="data",
71+
coordsB="data", axesA=ax2, axesB=ax1)
72+
con.set_color([0, 0, 0])
73+
ax2.add_artist(con)
74+
con.set_linewidth(4)
75+
76+
plt.show()
77+
78+
#############################################################################
79+
#
80+
# ------------
81+
#
82+
# References
83+
# """"""""""
84+
#
85+
# The use of the following functions, methods, classes and modules is shown
86+
# in this example:
87+
88+
import matplotlib
89+
matplotlib.axes.Axes.pie
90+
matplotlib.axes.Axes.bar
91+
matplotlib.pyplot
92+
matplotlib.patches.ConnectionPatch

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.