Skip to content

Navigation Menu

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

ENH: add option to label pie charts with absolute values #29152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions 7 galleries/examples/misc/svg_filter_pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@

# We want to draw the shadow for each pie, but we will not use "shadow"
# option as it doesn't save the references to the shadow patches.
pies = ax.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%')
pies = ax.pie(fracs, explode=explode,
wedge_labels=[labels, '{frac:.1%}'], wedge_label_distance=[1.1, 0.6])

for w in pies[0]:
for w, label in zip(pies[0], labels):
# set the id with the label.
w.set_gid(w.get_label())
w.set_gid(label)

# we don't want to draw the edge of the pie
w.set_edgecolor("none")
Expand Down
5 changes: 3 additions & 2 deletions 5 galleries/examples/pie_and_polar_charts/bar_of_pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
explode = [0.1, 0, 0]
# rotate so that first wedge is split by the x-axis
angle = -180 * overall_ratios[0]
wedges, *_ = ax1.pie(overall_ratios, autopct='%1.1f%%', startangle=angle,
labels=labels, explode=explode)
wedges, *_ = ax1.pie(
overall_ratios, startangle=angle, explode=explode,
wedge_labels=[labels, '{frac:.1%}'], wedge_label_distance=[1.1, 0.6])

# bar chart parameters
age_ratios = [.33, .54, .07, .06]
Expand Down
19 changes: 5 additions & 14 deletions 19 galleries/examples/pie_and_polar_charts/pie_and_donut_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,16 @@
"250 g butter",
"300 g berries"]

data = [float(x.split()[0]) for x in recipe]
data = [int(x.split()[0]) for x in recipe]
ingredients = [x.split()[-1] for x in recipe]

ax.pie(data, wedge_labels='{frac:.1%}\n({abs:d}g)', labels=ingredients,
labeldistance=None, textprops=dict(color="w", size=8, weight="bold"))

def func(pct, allvals):
absolute = int(np.round(pct/100.*np.sum(allvals)))
return f"{pct:.1f}%\n({absolute:d} g)"


wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data),
textprops=dict(color="w"))

ax.legend(wedges, ingredients,
title="Ingredients",
ax.legend(title="Ingredients",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))

plt.setp(autotexts, size=8, weight="bold")

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

plt.show()
Expand Down Expand Up @@ -97,7 +88,7 @@ def func(pct, allvals):

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

wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
wedges, _ = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)

bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(arrowprops=dict(arrowstyle="-"),
Expand Down
13 changes: 9 additions & 4 deletions 13 lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

import atexit
from collections import namedtuple
from collections.abc import MutableMapping
from collections.abc import MutableMapping, Sequence
import contextlib
import functools
import importlib
Expand Down Expand Up @@ -1377,14 +1377,17 @@ def _init_tests():

def _replacer(data, value):
"""
Either returns ``data[value]`` or passes ``data`` back, converts either to
a sequence.
Either returns ``data[value]`` or passes ``value`` back, converts either to
a sequence. If ``value`` is a non-string sequence, processes each element
and returns a list.
"""
try:
# if key isn't a string don't bother
if isinstance(value, str):
# try to use __getitem__
value = data[value]
elif isinstance(value, Sequence):
return [_replacer(data, x) for x in value]
except Exception:
# key does not exist, silently fall back to key
pass
Expand Down Expand Up @@ -1459,7 +1462,9 @@ def func(ax, *args, **kwargs): ...
- if called with ``data=None``, forward the other arguments to ``func``;
- otherwise, *data* must be a mapping; for any argument passed in as a
string ``name``, replace the argument by ``data[name]`` (if this does not
throw an exception), then forward the arguments to ``func``.
throw an exception), then forward the arguments to ``func``. For any
argument passed as a non-string sequence, replace any string elements
by ``data[name]`` (if that does not throw an exception).

In either case, any argument that is a `MappingView` is also converted to a
list.
Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.