Skip to content

Commit fb7f19a

Browse files
author
Release Manager
committed
gh-39045: partial pep8 cleanup in plot and plot3d just cleaning up plot3d for pep8 compliance ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #39045 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents 3efa0aa + 1fa4bb0 commit fb7f19a

File tree

9 files changed

+126
-110
lines changed

9 files changed

+126
-110
lines changed

src/sage/plot/misc.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ def setup_for_eval_on_grid(funcs,
6363
6464
EXAMPLES::
6565
66-
sage: x,y,z=var('x,y,z')
67-
sage: f(x,y)=x+y-z
68-
sage: g(x,y)=x+y
69-
sage: h(y)=-y
66+
sage: x,y,z = var('x,y,z')
67+
sage: f(x,y) = x+y-z
68+
sage: g(x,y) = x+y
69+
sage: h(y) = -y
7070
sage: sage.plot.misc.setup_for_eval_on_grid(f, [(0, 2),(1,3),(-4,1)], plot_points=5)
7171
(<sage...>, [(0.0, 2.0, 0.5), (1.0, 3.0, 0.5), (-4.0, 1.0, 1.25)])
7272
sage: sage.plot.misc.setup_for_eval_on_grid([g,h], [(0, 2),(-1,1)], plot_points=5)
@@ -149,20 +149,21 @@ def setup_for_eval_on_grid(funcs,
149149
# pad the variables if we don't have enough
150150
nargs = len(ranges)
151151
if len(vars) < nargs:
152-
vars += ('_',)*(nargs-len(vars))
152+
vars += ('_',) * (nargs - len(vars))
153153

154154
ranges = [[float(z) for z in r] for r in ranges]
155155

156156
if plot_points is None:
157157
plot_points = 2
158158

159159
if not isinstance(plot_points, (list, tuple)):
160-
plot_points = [plot_points]*len(ranges)
160+
plot_points = [plot_points] * len(ranges)
161161
elif len(plot_points) != nargs:
162162
raise ValueError("plot_points must be either an integer or a list of integers, one for each range")
163163

164164
plot_points = [int(p) if p >= 2 else 2 for p in plot_points]
165-
range_steps = [abs(range[1] - range[0])/(p-1) for range, p in zip(ranges, plot_points)]
165+
range_steps = [abs(range[1] - range[0]) / (p - 1)
166+
for range, p in zip(ranges, plot_points)]
166167
if min(range_steps) == float(0):
167168
raise ValueError("plot start point and end point must be different")
168169

@@ -206,11 +207,12 @@ def try_make_fast(f):
206207

207208
# Handle vectors, lists, tuples, etc.
208209
if isinstance(funcs, Iterable):
209-
funcs = tuple( try_make_fast(f) for f in funcs )
210+
funcs = tuple(try_make_fast(f) for f in funcs)
210211
else:
211212
funcs = try_make_fast(funcs)
212213

213-
#TODO: raise an error if there is a function/method in funcs that takes more values than we have ranges
214+
# TODO: raise an error if there is a function/method in funcs that
215+
# takes more values than we have ranges
214216

215217
if return_vars:
216218
return (funcs,
@@ -242,10 +244,10 @@ def unify_arguments(funcs):
242244
243245
EXAMPLES::
244246
245-
sage: x,y,z=var('x,y,z')
246-
sage: f(x,y)=x+y-z
247-
sage: g(x,y)=x+y
248-
sage: h(y)=-y
247+
sage: x,y,z = var('x,y,z')
248+
sage: f(x,y) = x+y-z
249+
sage: g(x,y) = x+y
250+
sage: h(y) = -y
249251
sage: sage.plot.misc.unify_arguments((f,g,h))
250252
((x, y, z), (z,))
251253
sage: sage.plot.misc.unify_arguments((g,h))
@@ -284,7 +286,9 @@ def _multiple_of_constant(n, pos, const):
284286
r"""
285287
Function for internal use in formatting ticks on axes with
286288
nice-looking multiples of various symbolic constants, such
287-
as `\pi` or `e`. Should only be used via keyword argument
289+
as `\pi` or `e`.
290+
291+
This should only be used via keyword argument
288292
``tick_formatter`` in :meth:`plot.show`. See documentation
289293
for the matplotlib.ticker module for more details.
290294
@@ -311,11 +315,11 @@ def _multiple_of_constant(n, pos, const):
311315
from sage.misc.latex import latex
312316
from sage.rings.continued_fraction import continued_fraction
313317
from sage.rings.infinity import Infinity
314-
cf = continued_fraction(n/const)
318+
cf = continued_fraction(n / const)
315319
k = 1
316320
while cf.quotient(k) != Infinity and cf.denominator(k) < 12:
317321
k += 1
318-
return '$%s$' % latex(cf.convergent(k-1)*const)
322+
return '$%s$' % latex(cf.convergent(k - 1) * const)
319323

320324

321325
def get_matplotlib_linestyle(linestyle, return_type):
@@ -401,10 +405,14 @@ def get_matplotlib_linestyle(linestyle, return_type):
401405
{'solid', 'dashed', 'dotted', dashdot', 'None'}, respectively {'-',
402406
'--', ':', '-.', ''}
403407
"""
404-
long_to_short_dict = {'solid' : '-','dashed' : '--', 'dotted' : ':',
405-
'dashdot':'-.'}
406-
short_to_long_dict = {'-' : 'solid','--' : 'dashed', ':' : 'dotted',
407-
'-.':'dashdot'}
408+
long_to_short_dict = {'solid': '-',
409+
'dashed': '--',
410+
'dotted': ':',
411+
'dashdot': '-.'}
412+
short_to_long_dict = {'-': 'solid',
413+
'--': 'dashed',
414+
':': 'dotted',
415+
'-.': 'dashdot'}
408416

409417
# We need this to take care of region plot. Essentially, if None is
410418
# passed, then we just return back the same thing.
@@ -416,16 +424,16 @@ def get_matplotlib_linestyle(linestyle, return_type):
416424
elif linestyle.startswith("steps"):
417425
if linestyle.startswith("steps-mid"):
418426
return "steps-mid" + get_matplotlib_linestyle(
419-
linestyle.strip("steps-mid"), "short")
427+
linestyle.strip("steps-mid"), "short")
420428
elif linestyle.startswith("steps-post"):
421429
return "steps-post" + get_matplotlib_linestyle(
422-
linestyle.strip("steps-post"), "short")
430+
linestyle.strip("steps-post"), "short")
423431
elif linestyle.startswith("steps-pre"):
424432
return "steps-pre" + get_matplotlib_linestyle(
425-
linestyle.strip("steps-pre"), "short")
433+
linestyle.strip("steps-pre"), "short")
426434
else:
427435
return "steps" + get_matplotlib_linestyle(
428-
linestyle.strip("steps"), "short")
436+
linestyle.strip("steps"), "short")
429437

430438
if return_type == 'short':
431439
if linestyle in short_to_long_dict.keys():

src/sage/plot/plot3d/parametric_plot3d.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,8 +989,8 @@ def g(x, y): return x, y+sin(y), x**2 + y**2
989989
if isinstance(f, Vector):
990990
f = tuple(f)
991991

992-
if isinstance(f, (list, tuple)) and len(f) > 0 and isinstance(f[0], (list, tuple)):
993-
return sum([parametric_plot3d(v, urange, vrange, plot_points=plot_points, **kwds) for v in f])
992+
if isinstance(f, (list, tuple)) and f and isinstance(f[0], (list, tuple)):
993+
return sum(parametric_plot3d(v, urange, vrange, plot_points=plot_points, **kwds) for v in f)
994994

995995
if not isinstance(f, (tuple, list)) or len(f) != 3:
996996
raise ValueError("f must be a list, tuple, or vector of length 3")
@@ -1121,7 +1121,9 @@ def _parametric_plot3d_surface(f, urange, vrange, plot_points, boundary_style, *
11211121

11221122
if boundary_style is not None:
11231123
for u in (urange[0], urange[-1]):
1124-
G += line3d([(g[0](u,v), g[1](u,v), g[2](u,v)) for v in vrange], **boundary_style)
1124+
G += line3d([(g[0](u, v), g[1](u, v), g[2](u, v)) for v in vrange],
1125+
**boundary_style)
11251126
for v in (vrange[0], vrange[-1]):
1126-
G += line3d([(g[0](u,v), g[1](u,v), g[2](u,v)) for u in urange], **boundary_style)
1127+
G += line3d([(g[0](u, v), g[1](u, v), g[2](u, v)) for u in urange],
1128+
**boundary_style)
11271129
return G

src/sage/plot/plot3d/plot3d.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def f(x, y): return math.exp(x/5)*math.cos(y)
155155
from sage.plot.plot3d.shapes import arrow3d
156156
from sage.plot.plot3d.texture import Texture
157157
from sage.plot.plot3d.tri_plot import TrianglePlot
158-
158+
from . import parametric_plot3d
159159
lazy_import("sage.functions.trig", ["cos", "sin"])
160160

161161

@@ -190,8 +190,12 @@ def __init__(self, dep_var, indep_vars):
190190
Arbitrary Coordinates coordinate transform (z in terms of x, y)
191191
"""
192192
all_vars = sage_getargspec(self.transform).args[1:]
193-
if set(all_vars) != set(indep_vars + [dep_var]):
194-
raise ValueError('variables were specified incorrectly for this coordinate system; incorrect variables were %s' % list(set(all_vars).symmetric_difference(set(indep_vars+[dep_var]))))
193+
A = set(all_vars)
194+
B = set(indep_vars + [dep_var])
195+
if A != B:
196+
raise ValueError('variables were specified incorrectly for this '
197+
'coordinate system; incorrect variables '
198+
'were %s' % list(A.symmetric_difference(B)))
195199
self.dep_var = dep_var
196200
self.indep_vars = indep_vars
197201

@@ -790,10 +794,7 @@ def smooth_triangle(self, a, b, c, da, db, dc, color=None):
790794
sage: sm_tri
791795
[[0, 0, 0], [0, 0, 1], [1, 1, 0]]
792796
"""
793-
return [a,b,c]
794-
795-
796-
from . import parametric_plot3d
797+
return [a, b, c]
797798

798799

799800
def plot3d(f, urange, vrange, adaptive=False, transformation=None, **kwds):
@@ -1083,7 +1084,7 @@ def plot3d(f, urange, vrange, adaptive=False, transformation=None, **kwds):
10831084
params = f.variables()
10841085

10851086
from sage.modules.vector_callable_symbolic_dense import Vector_callable_symbolic_dense
1086-
if isinstance(transformation, (tuple, list,Vector_callable_symbolic_dense)):
1087+
if isinstance(transformation, (tuple, list, Vector_callable_symbolic_dense)):
10871088
if len(transformation) == 3:
10881089
if params is None:
10891090
raise ValueError("must specify independent variable names in the ranges when using generic transformation")
@@ -1095,7 +1096,7 @@ def plot3d(f, urange, vrange, adaptive=False, transformation=None, **kwds):
10951096
raise ValueError("unknown transformation type")
10961097
# find out which variable is the function variable by
10971098
# eliminating the parameter variables.
1098-
all_vars = set(sum([list(s.variables()) for s in transformation],[]))
1099+
all_vars = set(sum([list(s.variables()) for s in transformation], []))
10991100
dep_var = all_vars - set(indep_vars)
11001101
if len(dep_var) == 1:
11011102
dep_var = dep_var.pop()
@@ -1173,11 +1174,11 @@ def plot3d_adaptive(f, x_range, y_range, color='automatic',
11731174
max_depth = max(max_depth, initial_depth)
11741175

11751176
from sage.plot.misc import setup_for_eval_on_grid
1176-
g, ranges = setup_for_eval_on_grid(f, [x_range,y_range], plot_points=2)
1177-
xmin,xmax = ranges[0][:2]
1178-
ymin,ymax = ranges[1][:2]
1177+
g, ranges = setup_for_eval_on_grid(f, [x_range, y_range], plot_points=2)
1178+
xmin, xmax = ranges[0][:2]
1179+
ymin, ymax = ranges[1][:2]
11791180

1180-
opacity = float(kwds.get('opacity',1))
1181+
opacity = float(kwds.get('opacity', 1))
11811182

11821183
if color == "automatic":
11831184
texture = rainbow(num_colors, 'rgbtuple')
@@ -1197,9 +1198,9 @@ def plot3d_adaptive(f, x_range, y_range, color='automatic',
11971198
if isinstance(texture, (list, tuple)):
11981199
if len(texture) == 2:
11991200
# do a grid coloring
1200-
xticks = (xmax - xmin)/2**initial_depth
1201-
yticks = (ymax - ymin)/2**initial_depth
1202-
parts = P.partition(lambda x,y,z: (int((x-xmin)/xticks) + int((y-ymin)/yticks)) % 2)
1201+
xticks = (xmax - xmin) / 2**initial_depth
1202+
yticks = (ymax - ymin) / 2**initial_depth
1203+
parts = P.partition(lambda x, y, z: (int((x - xmin) / xticks) + int((y - ymin) / yticks)) % 2)
12031204
else:
12041205
# do a topo coloring
12051206
bounds = P.bounding_box()
@@ -1208,8 +1209,8 @@ def plot3d_adaptive(f, x_range, y_range, color='automatic',
12081209
if max_z == min_z:
12091210
span = 0
12101211
else:
1211-
span = (len(texture)-1) / (max_z - min_z) # max to avoid dividing by 0
1212-
parts = P.partition(lambda x, y, z: int((z-min_z)*span))
1212+
span = (len(texture) - 1) / (max_z - min_z) # max to avoid dividing by 0
1213+
parts = P.partition(lambda x, y, z: int((z - min_z) * span))
12131214
all = []
12141215
for k, G in parts.items():
12151216
G.set_texture(texture[k], opacity=opacity)

src/sage/plot/plot3d/plot_field3d.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
Plotting 3D fields
44
"""
5-
#*****************************************************************************
5+
# ****************************************************************************
66
# Copyright (C) 2009 Jason Grout <[email protected]>
77
#
88
# Distributed under the terms of the GNU General Public License (GPL)
@@ -14,8 +14,8 @@
1414
#
1515
# The full text of the GPL is available at:
1616
#
17-
# http://www.gnu.org/licenses/
18-
#*****************************************************************************
17+
# https://www.gnu.org/licenses/
18+
# ****************************************************************************
1919

2020
from sage.arith.srange import srange
2121
from sage.plot.misc import setup_for_eval_on_grid
@@ -143,16 +143,19 @@ def plot_vector_field3d(functions, xrange, yrange, zrange,
143143
from matplotlib.colors import LinearSegmentedColormap
144144
cm = LinearSegmentedColormap.from_list('mymap', colors)
145145
else:
146-
cm = lambda x: colors
146+
def cm(x):
147+
return colors
147148

148149
max_len = max(v.norm() for v in vectors)
149-
scaled_vectors = [v/max_len for v in vectors]
150+
scaled_vectors = [v / max_len for v in vectors]
150151

151152
if center_arrows:
152-
G = sum([plot(v, color=cm(v.norm()), **kwds).translate(p-v/2) for v, p in zip(scaled_vectors, points)])
153+
G = sum(plot(v, color=cm(v.norm()), **kwds).translate(p - v / 2)
154+
for v, p in zip(scaled_vectors, points))
153155
G._set_extra_kwds(kwds)
154156
return G
155157
else:
156-
G = sum([plot(v, color=cm(v.norm()), **kwds).translate(p) for v, p in zip(scaled_vectors, points)])
158+
G = sum(plot(v, color=cm(v.norm()), **kwds).translate(p)
159+
for v, p in zip(scaled_vectors, points))
157160
G._set_extra_kwds(kwds)
158161
return G

src/sage/plot/plot3d/revolution_plot3d.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- Oscar Gerardo Lazo Arjona (2010): initial version.
88
"""
99

10-
#*****************************************************************************
10+
# ****************************************************************************
1111
# Copyright (C) 2010 Oscar Gerardo Lazo Arjona [email protected]
1212
#
1313
# Distributed under the terms of the GNU General Public License (GPL)
@@ -19,8 +19,8 @@
1919
#
2020
# The full text of the GPL is available at:
2121
#
22-
# http://www.gnu.org/licenses/
23-
#*****************************************************************************
22+
# https://www.gnu.org/licenses/
23+
# ****************************************************************************
2424
from sage.misc.decorators import rename_keyword
2525

2626
from sage.plot.plot3d.parametric_plot3d import parametric_plot3d
@@ -253,10 +253,10 @@ def cf(u, phi): return float(2 * u / pi) % 1
253253
phirange = (phi, phirange[0], phirange[1])
254254

255255
if isinstance(curve, (tuple, list)):
256-
#this if-else provides a vector v to be plotted
257-
#if curve is a tuple or a list of length 2, it is interpreted as a parametric curve
258-
#in the x-z plane.
259-
#if it is of length 3 it is interpreted as a parametric curve in 3d space
256+
# this if-else provides a vector v to be plotted
257+
# if curve is a tuple or a list of length 2, it is interpreted as a parametric curve
258+
# in the x-z plane.
259+
# if it is of length 3 it is interpreted as a parametric curve in 3d space
260260

261261
if len(curve) == 2:
262262
x = curve[0]

0 commit comments

Comments
 (0)