Skip to content

Commit 8c92ed7

Browse files
labeling example in two individual plots
1 parent f4b4dd1 commit 8c92ed7

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

examples/pie_and_polar_charts/pie_and_donut_labels.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,7 @@
1111

1212
###############################################################################
1313
# As usual we would start by defining the imports and create a figure with
14-
# subplots. To have some space for the big cake, we use
15-
# :meth:`subplots_adjust <matplotlib.figure.Figure.subplots_adjust>` to
16-
# adjust the spacings.
17-
18-
import numpy as np
19-
import matplotlib.pyplot as plt
20-
21-
fig, (ax, ax2) = plt.subplots(nrows=2, figsize=(6, 6))
22-
fig.subplots_adjust(top=0.92, bottom=0.08)
23-
plt.setp((ax, ax2), aspect="equal")
24-
25-
14+
# subplots.
2615
# Now it's time for the pie. Starting with a pie recipe, we create the data
2716
# and a list of labels from it.
2817
#
@@ -41,6 +30,10 @@
4130
# the left central point of the legend will be at the left central point of the
4231
# bounding box, spanning from ``(1,0)`` to ``(1.5,1)`` in axes coordinates.
4332

33+
import numpy as np
34+
import matplotlib.pyplot as plt
35+
36+
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
4437

4538
recipe = ["375 g flour",
4639
"75 g sugar",
@@ -68,7 +61,10 @@ def func(pct, allvals):
6861

6962
ax.set_title("Matplotlib bakery: A pie")
7063

64+
plt.show()
65+
7166

67+
###############################################################################
7268
# Now it's time for the donut. Starting with a donut recipe, we transcribe
7369
# the data to numbers (converting 1 egg to 50 g), and directly plot the pie.
7470
# The pie? Wait... it's going to be donut, is it not?
@@ -91,6 +87,9 @@ def func(pct, allvals):
9187
# * finally, create the annotation with all the previously
9288
# determined parameters.
9389

90+
91+
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
92+
9493
recipe = ["225 g flour",
9594
"90 g sugar",
9695
"1 egg",
@@ -100,7 +99,7 @@ def func(pct, allvals):
10099

101100
data = [225, 90, 50, 60, 100, 5]
102101

103-
wedges, texts = ax2.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
102+
wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
104103

105104
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
106105
kw = dict(xycoords='data', textcoords='data', arrowprops=dict(arrowstyle="-"),
@@ -113,9 +112,14 @@ def func(pct, allvals):
113112
horizontalalignment = ["", "left", "right"][int(np.sign(x))]
114113
connectionstyle = "angle,angleA=0,angleB={}".format(ang)
115114
kw["arrowprops"].update({"connectionstyle": connectionstyle})
116-
ax2.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
115+
ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
117116
horizontalalignment=horizontalalignment, **kw)
118117

119-
ax2.set_title("Matplotlib bakery: A donut")
118+
ax.set_title("Matplotlib bakery: A donut")
120119

121120
plt.show()
121+
122+
###############################################################################
123+
# And here it is, the donut. Note however, that if we were to use this recipe,
124+
# the ingredients would suffice for around 6 donuts - producing one huge
125+
# donut is untested and might result in kitchen errors.

0 commit comments

Comments
 (0)