Skip to content

DOC: Fixup to AnchoredArtist examples in the gallery #11093

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

Merged
merged 1 commit into from
Apr 28, 2018
Merged
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
41 changes: 29 additions & 12 deletions examples/axes_grid1/simple_anchored_artists.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@
Simple Anchored Artists
=======================

This example illustrates the use of the anchored helper classes found in
:py:mod:`~matplotlib.offsetbox` and in the :ref:`toolkit_axesgrid1-index`.
An implementation of a similar figure, but without use of the toolkit,
can be found in :ref:`sphx_glr_gallery_misc_anchored_artists.py`.
"""

import matplotlib.pyplot as plt


def draw_text(ax):
"""
Draw two text-boxes, anchored by different corners to the upper-left
corner of the figure.
"""
from matplotlib.offsetbox import AnchoredText
# loc=2 is equivalent to loc='upper left'
at = AnchoredText("Figure 1a",
loc=2, prop=dict(size=8), frameon=True,
)
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)

# loc=3 is eqivalent to loc='lower left'
at2 = AnchoredText("Figure 1(b)",
loc=3, prop=dict(size=8), frameon=True,
bbox_to_anchor=(0., 1.),
Expand All @@ -24,7 +35,10 @@ def draw_text(ax):
ax.add_artist(at2)


def draw_circle(ax): # circle in the canvas coordinate
def draw_circle(ax):
"""
Draw a circle in axis coordinates
"""
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDrawingArea
from matplotlib.patches import Circle
ada = AnchoredDrawingArea(20, 20, 0, 0,
Expand All @@ -35,18 +49,22 @@ def draw_circle(ax): # circle in the canvas coordinate


def draw_ellipse(ax):
"""
Draw an ellipse of width=0.1, height=0.15 in data coordinates
"""
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredEllipse
# draw an ellipse of width=0.1, height=0.15 in the data coordinate
ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
loc=3, pad=0.5, borderpad=0.4, frameon=True)

ax.add_artist(ae)


def draw_sizebar(ax):
"""
Draw a horizontal bar with length of 0.1 in data coordinates,
with a fixed label underneath.
"""
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
# draw a horizontal bar with length of 0.1 in Data coordinate
# (ax.transData) with a label underneath.
asb = AnchoredSizeBar(ax.transData,
0.1,
r"1$^{\prime}$",
Expand All @@ -56,13 +74,12 @@ def draw_sizebar(ax):
ax.add_artist(asb)


if 1:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why that was here in the first place...

ax = plt.gca()
ax.set_aspect(1.)
ax = plt.gca()
ax.set_aspect(1.)

draw_text(ax)
draw_circle(ax)
draw_ellipse(ax)
draw_sizebar(ax)
draw_text(ax)
draw_circle(ax)
draw_ellipse(ax)
draw_sizebar(ax)

plt.show()
plt.show()
115 changes: 70 additions & 45 deletions examples/misc/anchored_artists.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
Anchored Artists
================

This example illustrates the use of the anchored objects without the
helper classes found in the :ref:`toolkit_axesgrid1-index`. This version
of the figure is similar to the one found in
:ref:`sphx_glr_gallery_axes_grid1_simple_anchored_artists.py`, but it is
implemented using only the matplotlib namespace, without the help
of additional toolkits.
"""

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle, Ellipse
from matplotlib.offsetbox import (
AnchoredOffsetbox, AuxTransformBox, DrawingArea, TextArea, VPacker)
Expand All @@ -18,27 +25,34 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5,
child=self.txt, prop=prop, frameon=frameon)


class AnchoredSizeBar(AnchoredOffsetbox):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing got deleted here, just moved around so the functions line up with the other file

def __init__(self, transform, size, label, loc,
pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True):
"""
Draw a horizontal bar with the size in data coordinate of the given
axes. A label will be drawn underneath (center-aligned).
def draw_text(ax):
"""
Draw a text-box anchored to the upper-left corner of the figure.
"""
# loc=2 is equivalent to loc='upper left'
at = AnchoredText("Figure 1a", loc=2, frameon=True)
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)

pad, borderpad in fraction of the legend font size (or prop)
sep in points.
"""
self.size_bar = AuxTransformBox(transform)
self.size_bar.add_artist(Rectangle((0, 0), size, 0, ec="black", lw=1.0))

self.txt_label = TextArea(label, minimumdescent=False)
class AnchoredDrawingArea(AnchoredOffsetbox):
def __init__(self, width, height, xdescent, ydescent,
loc, pad=0.4, borderpad=0.5, prop=None, frameon=True):
self.da = DrawingArea(width, height, xdescent, ydescent)
super().__init__(loc, pad=pad, borderpad=borderpad,
child=self.da, prop=None, frameon=frameon)

self._box = VPacker(children=[self.size_bar, self.txt_label],
align="center",
pad=0, sep=sep)

super().__init__(loc, pad=pad, borderpad=borderpad,
child=self._box, prop=prop, frameon=frameon)
def draw_circle(ax):
"""
Draw a circle in axis coordinates
"""
from matplotlib.patches import Circle
ada = AnchoredDrawingArea(20, 20, 0, 0,
loc=1, pad=0., frameon=False)
p = Circle((10, 10), 10)
ada.da.add_artist(p)
ax.add_artist(ada)


class AnchoredEllipse(AnchoredOffsetbox):
Expand All @@ -56,41 +70,44 @@ def __init__(self, transform, width, height, angle, loc,
child=self._box, prop=prop, frameon=frameon)


class AnchoredDrawingArea(AnchoredOffsetbox):
def __init__(self, width, height, xdescent, ydescent,
loc, pad=0.4, borderpad=0.5, prop=None, frameon=True):
self.da = DrawingArea(width, height, xdescent, ydescent)
super().__init__(loc, pad=pad, borderpad=borderpad,
child=self.da, prop=None, frameon=frameon)
def draw_ellipse(ax):
"""
Draw an ellipse of width=0.1, height=0.15 in data coordinates
"""
ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
loc=3, pad=0.5, borderpad=0.4, frameon=True)

ax.add_artist(ae)

if __name__ == "__main__":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, OK. I did delete this line, and made the same set of small functions as in the similar file to highlight the parallels.


import matplotlib.pyplot as plt
class AnchoredSizeBar(AnchoredOffsetbox):
def __init__(self, transform, size, label, loc,
pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True):
"""
Draw a horizontal bar with the size in data coordinate of the given
axes. A label will be drawn underneath (center-aligned).

ax = plt.gca()
ax.set_aspect(1.)
pad, borderpad in fraction of the legend font size (or prop)
sep in points.
"""
self.size_bar = AuxTransformBox(transform)
self.size_bar.add_artist(Rectangle((0, 0), size, 0, ec="black", lw=1.0))

at = AnchoredText("Figure 1a",
loc=2, frameon=True)
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)
self.txt_label = TextArea(label, minimumdescent=False)

from matplotlib.patches import Circle
ada = AnchoredDrawingArea(20, 20, 0, 0,
loc=1, pad=0., frameon=False)
p = Circle((10, 10), 10)
ada.da.add_artist(p)
ax.add_artist(ada)
self._box = VPacker(children=[self.size_bar, self.txt_label],
align="center",
pad=0, sep=sep)

# draw an ellipse of width=0.1, height=0.15 in the data coordinate
ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
loc=3, pad=0.5, borderpad=0.4, frameon=True)
super().__init__(loc, pad=pad, borderpad=borderpad,
child=self._box, prop=prop, frameon=frameon)

ax.add_artist(ae)

# draw a horizontal bar with length of 0.1 in Data coordinate
# (ax.transData) with a label underneath.
def draw_sizebar(ax):
"""
Draw a horizontal bar with length of 0.1 in data coordinates,
with a fixed label underneath.
"""
asb = AnchoredSizeBar(ax.transData,
0.1,
r"1$^{\prime}$",
Expand All @@ -99,5 +116,13 @@ def __init__(self, width, height, xdescent, ydescent,
frameon=False)
ax.add_artist(asb)

plt.draw()
plt.show()

ax = plt.gca()
ax.set_aspect(1.)

draw_text(ax)
draw_circle(ax)
draw_ellipse(ax)
draw_sizebar(ax)

plt.show()
2 changes: 1 addition & 1 deletion tutorials/text/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@

from matplotlib.offsetbox import AnchoredText
at = AnchoredText("Figure 1a",
prop=dict(size=8), frameon=True,
prop=dict(size=15), frameon=True,
loc=2,
)
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
Expand Down