Skip to content

Commit

Permalink
Fixed issue in list_plot where it assumed data had been enumerated wh…
Browse files Browse the repository at this point in the history
…en it might not have been
  • Loading branch information
Noel Roemmele committed Feb 9, 2025
1 parent dc99dc8 commit 349bc2d
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/sage/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3107,6 +3107,22 @@ def list_plot(data, plotjoined=False, **kwargs):
100.0
sage: d['ymin']
100.0
Verify that :trac:`38037` is fixed::
sage: list_plot([(0,-1),(1,-2),(2,-3),(3,-4),(4,None)])
Traceback (most recent call last):
...
TypeError: unable to coerce to a ComplexNumber:
<class 'sage.rings.integer.Integer'>
#Non enumerated list example
sage: list_plot([3+I, 4, I, 1+5*i, None, 1+i])
Graphics object consisting of 1 graphics primitive
#Enumerated list example
sage: list_plot([4, 3+I, I, 1+5*i, None, 1+i])
Graphics object consisting of 1 graphics primitive
"""
from sage.plot.all import point
try:
Expand All @@ -3124,10 +3140,12 @@ def list_plot(data, plotjoined=False, **kwargs):
else:
list_data = list(data.items())
return list_plot(list_data, plotjoined=plotjoined, **kwargs)
listEnumerated = False
try:
from sage.rings.real_double import RDF
RDF(data[0])
data = list(enumerate(data))
listEnumerated = True
except TypeError: # we can get this TypeError if the element is a list
# or tuple or numpy array, or an element of CC, CDF
# We also want to avoid doing CC(data[0]) here since it will go
Expand All @@ -3138,6 +3156,7 @@ def list_plot(data, plotjoined=False, **kwargs):
# element of the Symbolic Ring.
if isinstance(data[0], Expression):
data = list(enumerate(data))
listEnumerated = True

Check warning on line 3159 in src/sage/plot/plot.py

View check run for this annotation

Codecov / codecov/patch

src/sage/plot/plot.py#L3159

Added line #L3159 was not covered by tests

try:
if plotjoined:
Expand All @@ -3150,9 +3169,11 @@ def list_plot(data, plotjoined=False, **kwargs):
# point3d() throws an IndexError on the (0,1) before it ever
# gets to (1, I).
from sage.rings.cc import CC
# if we get here, we already did "list(enumerate(data))",
# so look at z[1] in inner list
data = [(z.real(), z.imag()) for z in [CC(z[1]) for z in data]]
# It is not guaranteed that we enumerated the data so we have two cases
if listEnumerated:
data = [(z.real(), z.imag()) for z in [CC(z[1]) for z in data]]
else:
data = [(z.real(), z.imag()) for z in [CC(z) for z in data]]
if plotjoined:
return line(data, **kwargs)
else:
Expand Down

0 comments on commit 349bc2d

Please sign in to comment.