-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
@@ -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__": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}$", | ||
|
@@ -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() |
There was a problem hiding this comment.
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...