Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
rcomer wants to merge1 commit intomatplotlib:main
base:main
Choose a base branch
Loading
fromrcomer:pie-fmt
Draft
Show file tree
Hide file tree
Changes fromall 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 deletionsgalleries/examples/misc/svg_filter_pie.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 win pies[0]:
for w, labelinzip(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 deletionsgalleries/examples/pie_and_polar_charts/bar_of_pie.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionslib/matplotlib/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp