Skip to content

Commit

Permalink
gh-36460: add default legend_color to eliminate KeyError when plottin…
Browse files Browse the repository at this point in the history
…g arrow2d or disk

    
Fixes #36153.

The line `g._legend_colors = [options['legend_color']]` appears in the
code for seven graphics objects. Five of them (`circle`, `ellipse`,
`line2d`, `point2d`, and  `polygon2d`) set `None` as the default value
for `legend_color`. The other two (`arrow2d` and `disk`) do not set a
default, so a `KeyError` is raised if a `legend_label` is supplied, but
no `legend_color` is specified. This bug was pointed out in #36153.

This PR adds `None` as the default value of `legend_color` in `arrow2d`
and `disk`.

It also adds the corresponding doctest to all seven of these graphics
objects, except `line2d`, which already has the example
`line([(0,0),(1,1)], legend_label='line')`.

The PR also corrects a typo in the docstring of the `cone` graphic
object.

### 📝 Checklist

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
    
URL: #36460
Reported by: DaveWitteMorris
Reviewer(s): Kwankyu Lee
  • Loading branch information
Release Manager committed Oct 18, 2023
2 parents 2ef76f3 + f2dfb7b commit 47b5eb9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/sage/combinat/root_system/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ def cone(self, rays=[], lines=[], color="black", thickness=1, alpha=1, wireframe
- ``alpha`` -- a number in the interval `[0, 1]` (default: `1`)
the desired transparency
- ``label`` -- an object to be used as for this cone.
- ``label`` -- an object to be used as the label for this cone.
The label itself will be constructed by calling
:func:`~sage.misc.latex.latex` or :func:`repr` on the
object depending on the graphics backend.
Expand Down
9 changes: 8 additions & 1 deletion src/sage/plot/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,8 @@ def arrow(tailpoint=None, headpoint=None, **kwds):


@rename_keyword(color='rgbcolor')
@options(width=2, rgbcolor=(0,0,1), zorder=2, head=1, linestyle='solid', legend_label=None)
@options(width=2, rgbcolor=(0,0,1), zorder=2, head=1, linestyle='solid',
legend_label=None, legend_color=None)
def arrow2d(tailpoint=None, headpoint=None, path=None, **options):
"""
If ``tailpoint`` and ``headpoint`` are provided, returns an arrow from
Expand Down Expand Up @@ -640,6 +641,12 @@ def arrow2d(tailpoint=None, headpoint=None, path=None, **options):
::
sage: arrow2d((-2,2), (7,1)).show(frame=True)
TESTS:
Verify that :issue:`36153` is fixed::
sage: A = arrow2d((-1,-1), (2,3), legend_label="test")
"""
from sage.plot.all import Graphics
g = Graphics()
Expand Down
4 changes: 4 additions & 0 deletions src/sage/plot/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ def circle(center, radius, **options):
sage: P = circle((1,1), 1)
sage: P.aspect_ratio()
1.0
Verify that :issue:`36153` does not arise::
sage: C = circle((1,1), 1, legend_label="test")
"""
from sage.plot.all import Graphics

Expand Down
6 changes: 5 additions & 1 deletion src/sage/plot/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def plot3d(self, z=0, **kwds):

@rename_keyword(color='rgbcolor')
@options(alpha=1, fill=True, rgbcolor=(0, 0, 1), thickness=0, legend_label=None,
aspect_ratio=1.0)
legend_color=None, aspect_ratio=1.0)
def disk(point, radius, angle, **options):
r"""
A disk (that is, a sector or wedge of a circle) with center
Expand Down Expand Up @@ -346,6 +346,10 @@ def disk(point, radius, angle, **options):
Traceback (most recent call last):
...
ValueError: the center point of a plotted disk should have two or three coordinates
Verify that :issue:`36153` is fixed::
sage: D = disk((0, 0), 5, (0, pi/2), legend_label="test")
"""
from sage.plot.all import Graphics
g = Graphics()
Expand Down
5 changes: 5 additions & 0 deletions src/sage/plot/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ def ellipse(center, r1, r2, angle=0, **options):
E=ellipse((0,0),2,1,legend_label="My ellipse", legend_color='green')
sphinx_plot(E)
TESTS:
Verify that :issue:`36153` does not arise::
sage: E = ellipse((0,0), 2, 1, legend_label="test")
"""
from sage.plot.all import Graphics
g = Graphics()
Expand Down
4 changes: 4 additions & 0 deletions src/sage/plot/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,10 @@ def point2d(points, **options):
sage: point2d(iter([]))
Graphics object consisting of 0 graphics primitives
Verify that :issue:`36153` does not arise::
sage: P = point((0.5, 0.5), legend_label="test")
"""
from sage.plot.plot import xydata_from_point_list
from sage.plot.all import Graphics
Expand Down
7 changes: 6 additions & 1 deletion src/sage/plot/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,15 @@ def polygon2d(points, **options):
sage: polygon2d([[1,2], [5,6], [5,0]]).aspect_ratio()
1.0
TESTS:
Verify that :issue:`36153` does not arise::
sage: P = polygon2d([[1,2], [5,6], [5,0]], legend_label="test")
AUTHORS:
- David Joyner (2006-04-14): the long list of examples above.
"""
from sage.plot.plot import xydata_from_point_list
from sage.plot.all import Graphics
Expand Down

0 comments on commit 47b5eb9

Please sign in to comment.