8000 Backport PR #22279: Remove Axes sublists from docs · matplotlib/matplotlib@f876d9e · GitHub
[go: up one dir, main page]

Skip to content

Commit f876d9e

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #22279: Remove Axes sublists from docs
1 parent feb68c7 commit f876d9e

File tree

3 files changed

+44
-51
lines changed

3 files changed

+44
-51
lines changed

examples/mplot3d/wire3d_animation_sgskip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def generate(X, Y, phi):
3939
for phi in np.linspace(0, 180. / np.pi, 100):
4040
# If a line collection is already remove it before drawing.
4141
if wframe:
42-
ax.collections.remove(wframe)
42+
wframe.remove()
4343

4444
# Plot the new wireframe and pause briefly before continuing.
4545
Z = generate(X, Y, phi)

examples/pie_and_polar_charts/bar_of_pie.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,36 @@
1919
fig.subplots_adjust(wspace=0)
2020

2121
# pie chart parameters
22-
ratios = [.27, .56, .17]
22+
overall_ratios = [.27, .56, .17]
2323
labels = ['Approve', 'Disapprove', 'Undecided']
2424
explode = [0.1, 0, 0]
2525
# rotate so that first wedge is split by the x-axis
26-
angle = -180 * ratios[0]
27-
ax1.pie(ratios, autopct='%1.1f%%', startangle=angle,
28-
labels=labels, explode=explode)
26+
angle = -180 * overall_ratios[0]
27+
wedges, *_ = ax1.pie(overall_ratios, autopct='%1.1f%%', startangle=angle,
28+
labels=labels, explode=explode)
2929

3030
# bar chart parameters
31-
32-
xpos = 0
33-
bottom = 0
34-
ratios = [.33, .54, .07, .06]
31+
age_ratios = [.33, .54, .07, .06]
32+
age_labels = ['Under 35', '35-49', '50-65', 'Over 65']
33+
bottom = 1
3534
width = .2
36-
colors = [[.1, .3, .5], [.1, .3, .3], [.1, .3, .7], [.1, .3, .9]]
3735

38-
for j in range(len(ratios)):
39-
height = ratios[j]
40-
ax2.bar(xpos, height, width, bottom=bottom, color=colors[j])
41-
ypos = bottom + ax2.patches[j].get_height() / 2
42-
bottom += height
43-
ax2.text(xpos, ypos, "%d%%" % (ax2.patches[j].get_height() * 100),
44< 8000 code class="diff-text syntax-highlighted-line deletion">-
ha='center')
36+
# Adding from the top matches the legend.
37+
for j, (height, label) in enumerate(reversed([*zip(age_ratios, age_labels)])):
38+
bottom -= height
39+
bc = ax2.bar(0, height, width, bottom=bottom, color='C0', label=label,
40+
alpha=0.1 + 0.25 * j)
41+
ax2.bar_label(bc, labels=[f"{height:.0%}"], label_type='center')
4542

4643
ax2.set_title('Age of approvers')
47-
ax2.legend(('50-65', 'Over 65', '35-49', 'Under 35'))
44+
ax2.legend()
4845
ax2.axis('off')
4946
ax2.set_xlim(- 2.5 * width, 2.5 * width)
5047

5148
# use ConnectionPatch to draw lines between the two plots
52-
# get the wedge data
53-
theta1, theta2 = ax1.patches[0].theta1, ax1.patches[0].theta2
54-
center, r = ax1.patches[0].center, ax1.patches[0].r
55-
bar_height = sum([item.get_height() for item in ax2.patches])
49+
theta1, theta2 = wedges[0].theta1, wedges[0].theta2
50+
center, r = wedges[0].center, wedges[0].r
51+
bar_height = sum(age_ratios)
5652

5753
# draw top connecting line
5854
x = r * np.cos(np.pi / 180 * theta2) + center[0]

tutorials/intermediate/artists.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,11 @@ class in the Matplotlib API, and the one you will be working with most
7979
line, = ax.plot(t, s, color='blue', lw=2)
8080
8181
In this example, ``ax`` is the ``Axes`` instance created by the
82-
``fig.add_subplot`` call above (remember ``Subplot`` is just a
83-
subclass of ``Axes``) and when you call ``ax.plot``, it creates a
84-
``Line2D`` instance and adds it to the :attr:`Axes.lines
85-
<matplotlib.axes.Axes.lines>` list. In the interactive `IPython
86-
<https://ipython.org/>`_ session below, you can see that the
87-
``Axes.lines`` list is length one and contains the same line that was
88-
returned by the ``line, = ax.plot...`` call:
82+
``fig.add_subplot`` call above (remember ``Subplot`` is just a subclass of
83+
``Axes``) and when you call ``ax.plot``, it creates a ``Line2D`` instance and
84+
adds it to the ``Axes``. In the interactive `IPython <https://ipython.org/>`_
85+
session below, you can see that the ``Axes.lines`` list is length one and
86+
contains the same line that was returned by the ``line, = ax.plot...`` call:
8987
9088
.. sourcecode:: ipython
9189
@@ -97,11 +95,10 @@ class in the Matplotlib API, and the one you will be working with most
9795
9896
If you make subsequent calls to ``ax.plot`` (and the hold state is "on"
9997
which is the default) then additional lines will be added to the list.
100-
You can remove lines later simply by calling the list methods; either
101-
of these will work::
98+
You can remove a line later by calling its ``remove`` method::
10299
103-
del ax.lines[0]
104-
ax.lines.remove(line) # one or the other, not both!
100+
line = ax.lines[0]
101+
line.remove()
105102
106103
The Axes also has helper methods to configure and decorate the x-axis
107104
and y-axis tick, tick labels and axis labels::
@@ -386,11 +383,10 @@ class in the Matplotlib API, and the one you will be working with most
386383
# rect.set_facecolor('green')
387384
#
388385
# When you call a plotting method, e.g., the canonical
389-
# :meth:`~matplotlib.axes.Axes.plot` and pass in arrays or lists of
390-
# values, the method will create a :meth:`matplotlib.lines.Line2D`
391-
# instance, update the line with all the ``Line2D`` properties passed as
392-
# keyword arguments, add the line to the :attr:`Axes.lines
393-
# <matplotlib.axes.Axes.lines>` container, and returns it to you:
386+
# `~matplotlib.axes.Axes.plot` and pass in arrays or lists of values, the
387+
# method will create a `matplotlib.lines.Line2D` instance, update the line with
388+
# all the ``Line2D`` properties passed as keyword arguments, add the line to
389+
# the ``Axes``, and return it to you:
394390
#
395391
# .. sourcecode:: ipython
396392
#
@@ -423,19 +419,20 @@ class in the Matplotlib API, and the one you will be working with most
423419
# In [235]: print(len(ax.patches))
424420
# Out[235]: 50
425421
#
426-
# You should not add objects directly to the ``Axes.lines`` or
427-
# ``Axes.patches`` lists unless you know exactly what you are doing,
428-
# because the ``Axes`` needs to do a few things when it creates and adds
429-
# an object. It sets the figure and axes property of the ``Artist``, as
430-
# well as the default ``Axes`` transformation (unless a transformation
431-
# is set). It also inspects the data contained in the ``Artist`` to
432-
# update the data structures controlling auto-scaling, so that the view
433-
# limits can be adjusted to contain the plotted data. You can,
434-
# nonetheless, create objects yourself and add them directly to the
435-
# ``Axes`` using helper methods like
436-
# :meth:`~matplotlib.axes.Axes.add_line` and
437-
# :meth:`~matplotlib.axes.Axes.add_patch`. Here is an annotated
438-
# interactive session illustrating what is going on:
422+
# You should not add objects directly to the ``Axes.lines`` or ``Axes.patches``
423+
# lists, because the ``Axes`` needs to do a few things when it creates and adds
424+
# an object:
425+
#
426+
# - It sets the ``figure`` and ``axes`` property of the ``Artist``;
427+
# - It sets the default ``Axes`` transformation (unless one is already set);
428+
# - It inspects the data contained in the ``Artist`` to update the data
429+
# structures controlling auto-scaling, so that the view limits can be
430+
# adjusted to contain the plotted data.
431+
#
432+
# You can, nonetheless, create objects yourself and add them directly to the
433+
# ``Axes`` using helper methods like `~matplotlib.axes.Axes.add_line` and
434+
# `~matplotlib.axes.Axes.add_patch`. Here is an annotated interactive session
435+
# illustrating what is going on:
439436
#
440437
# .. sourcecode:: ipython
441438
#

0 commit comments

Comments
 (0)
0