Skip to content

Commit

Permalink
Trac #32912: Add pictures to disk.py
Browse files Browse the repository at this point in the history
Add illustrations to the examples presented on the docstring

URL: https://trac.sagemath.org/32912
Reported by: jhonrubia6
Ticket author(s): Javier Honrubia González
Reviewer(s): Kwankyu Lee
  • Loading branch information
Release Manager committed Feb 14, 2022
2 parents f413b85 + 57a4352 commit 91710c3
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions src/sage/plot/disk.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""
Disks
"""
#*****************************************************************************
# *****************************************************************************
# Copyright (C) 2006 Alex Clemesha <clemesha@gmail.com>,
# William Stein <wstein@gmail.com>,
# 2008 Mike Hansen <mhansen@gmail.com>,
# 2021 Javier Honrubia <jhonrubia6@alumno.uned.es>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
Expand All @@ -16,13 +17,14 @@
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
# *****************************************************************************

from sage.plot.primitive import GraphicPrimitive
from sage.misc.decorators import options, rename_keyword
from sage.plot.colors import to_mpl_color
from math import sin, cos, pi


class Disk(GraphicPrimitive):
"""
Primitive class for the ``Disk`` graphics type. See ``disk?`` for
Expand Down Expand Up @@ -122,14 +124,14 @@ def _allowed_options(self):
sage: p[0]._allowed_options()['zorder']
'The layer level in which to draw'
"""
return {'alpha':'How transparent the figure is.',
'fill':'Whether or not to fill the disk.',
'legend_label':'The label for this item in the legend.',
'legend_color':'The color of the legend text.',
'thickness':'How thick the border of the disk is.',
'rgbcolor':'The color as an RGB tuple.',
'hue':'The color given as a hue.',
'zorder':'The layer level in which to draw'}
return {'alpha': 'How transparent the figure is.',
'fill': 'Whether or not to fill the disk.',
'legend_label': 'The label for this item in the legend.',
'legend_color': 'The color of the legend text.',
'thickness': 'How thick the border of the disk is.',
'rgbcolor': 'The color as an RGB tuple.',
'hue': 'The color given as a hue.',
'zorder': 'The layer level in which to draw'}

def _repr_(self):
"""
Expand All @@ -141,8 +143,7 @@ def _repr_(self):
sage: p = P[0]; p
Disk defined by (3.0,3.0) with r=1.0 spanning (0.0, 1.5707963267...) radians
"""
return "Disk defined by (%s,%s) with r=%s spanning (%s, %s) radians" % (self.x,
self.y, self.r, self.rad1, self.rad2)
return "Disk defined by (%s,%s) with r=%s spanning (%s, %s) radians" % (self.x, self.y, self.r, self.rad1, self.rad2)

def _render_on_subplot(self, subplot):
"""
Expand All @@ -160,11 +161,11 @@ def _render_on_subplot(self, subplot):
"""
import matplotlib.patches as patches
options = self.options()
deg1 = self.rad1*(180./pi) #convert radians to degrees
deg1 = self.rad1*(180./pi) # convert radians to degrees
deg2 = self.rad2*(180./pi)
z = int(options.pop('zorder', 0))
p = patches.Wedge((float(self.x), float(self.y)), float(self.r), float(deg1),
float(deg2), zorder=z)
float(deg2), zorder=z)
a = float(options['alpha'])
p.set_alpha(a)
p.set_linewidth(float(options['thickness']))
Expand Down Expand Up @@ -198,6 +199,7 @@ def plot3d(self, z=0, **kwds):
sage: disk((0,0), 1, (pi/2, 0), fill=False).plot3d(3)
Graphics3d Object
These examples show that the appropriate options are passed::
sage: D = disk((2,3), 1, (pi/4,pi/3), hue=.8, alpha=.3, fill=True)
Expand Down Expand Up @@ -232,10 +234,11 @@ def plot3d(self, z=0, **kwds):
return Polygon(xdata, ydata, options).plot3d(z)
else:
from .line import Line
return Line(xdata, ydata, options).plot3d().translate((0,0,z))
return Line(xdata, ydata, options).plot3d().translate((0, 0, z))


@rename_keyword(color='rgbcolor')
@options(alpha=1, fill=True, rgbcolor=(0,0,1), thickness=0, legend_label=None,
@options(alpha=1, fill=True, rgbcolor=(0, 0, 1), thickness=0, legend_label=None,
aspect_ratio=1.0)
def disk(point, radius, angle, **options):
r"""
Expand All @@ -257,6 +260,16 @@ def disk(point, radius, angle, **options):
sage: P = tl+tr+bl+br
sage: P.show(xmin=-2,xmax=2,ymin=-2,ymax=2)
.. PLOT::
from sage.plot.disk import Disk
bl = disk((0.0,0.0), 1, (pi, 3*pi/2), color='yellow')
tr = disk((0.0,0.0), 1, (0, pi/2), color='yellow')
tl = disk((0.0,0.0), 1, (pi/2, pi), color='black')
br = disk((0.0,0.0), 1, (3*pi/2, 2*pi), color='black')
P = tl+tr+bl+br
sphinx_plot(P)
The default aspect ratio is 1.0::
sage: disk((0.0,0.0), 1, (pi, 3*pi/2)).aspect_ratio()
Expand All @@ -267,12 +280,24 @@ def disk(point, radius, angle, **options):
sage: bl = disk((0.0,0.0), 1, (pi, 3*pi/2), rgbcolor=(1,1,0))
sage: bl.show(figsize=[5,5])
.. PLOT::
from sage.plot.disk import Disk
bl = disk((0.0,0.0), 1, (pi, 3*pi/2), rgbcolor=(1,1,0))
sphinx_plot(bl)
Note that since ``thickness`` defaults to zero, it is best to change
that option when using ``fill=False``::
sage: disk((2,3), 1, (pi/4,pi/3), hue=.8, alpha=.3, fill=False, thickness=2)
Graphics object consisting of 1 graphics primitive
.. PLOT::
from sage.plot.disk import Disk
D = disk((2,3), 1, (pi/4,pi/3), hue=.8, alpha=.3, fill=False, thickness=2)
sphinx_plot(D)
The previous two examples also illustrate using ``hue`` and ``rgbcolor``
as ways of specifying the color of the graphic.
Expand All @@ -285,6 +310,12 @@ def disk(point, radius, angle, **options):
sage: type(d)
<... 'sage.plot.plot3d.index_face_set.IndexFaceSet'>
.. PLOT::
from sage.plot.disk import Disk
d = disk((1,1,3), 1, (pi,3*pi/2), rgbcolor=(1,0,0))
sphinx_plot(d)
Extra options will get passed on to ``show()``, as long as they are valid::
sage: disk((0, 0), 5, (0, pi/2), xmin=0, xmax=5, ymin=0, ymax=5, figsize=(2,2), rgbcolor=(1, 0, 1))
Expand Down Expand Up @@ -321,9 +352,9 @@ def disk(point, radius, angle, **options):
if options['legend_label']:
g.legend(True)
g._legend_colors = [options['legend_color']]
if len(point)==2:
if len(point) == 2:
return g
elif len(point)==3:
elif len(point) == 3:
return g[0].plot3d(z=point[2])
else:
raise ValueError('The center point of a plotted disk should have two or three coordinates.')

0 comments on commit 91710c3

Please sign in to comment.