Skip to content

Commit 8f37350

Browse files
committed
partial pep8 cleanup in plot and plot3d
1 parent b9e396a commit 8f37350

File tree

9 files changed

+117
-107
lines changed

9 files changed

+117
-107
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ 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)):
992+
if isinstance(f, (list, tuple)) and f and isinstance(f[0], (list, tuple)):
993993
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:
@@ -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: 14 additions & 17 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

@@ -191,7 +191,7 @@ def __init__(self, dep_var, indep_vars):
191191
"""
192192
all_vars = sage_getargspec(self.transform).args[1:]
193193
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]))))
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]))))
195195
self.dep_var = dep_var
196196
self.indep_vars = indep_vars
197197

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

798795

799796
def plot3d(f, urange, vrange, adaptive=False, transformation=None, **kwds):
@@ -1083,7 +1080,7 @@ def plot3d(f, urange, vrange, adaptive=False, transformation=None, **kwds):
10831080
params = f.variables()
10841081

10851082
from sage.modules.vector_callable_symbolic_dense import Vector_callable_symbolic_dense
1086-
if isinstance(transformation, (tuple, list,Vector_callable_symbolic_dense)):
1083+
if isinstance(transformation, (tuple, list, Vector_callable_symbolic_dense)):
10871084
if len(transformation) == 3:
10881085
if params is None:
10891086
raise ValueError("must specify independent variable names in the ranges when using generic transformation")
@@ -1095,7 +1092,7 @@ def plot3d(f, urange, vrange, adaptive=False, transformation=None, **kwds):
10951092
raise ValueError("unknown transformation type")
10961093
# find out which variable is the function variable by
10971094
# eliminating the parameter variables.
1098-
all_vars = set(sum([list(s.variables()) for s in transformation],[]))
1095+
all_vars = set(sum([list(s.variables()) for s in transformation], []))
10991096
dep_var = all_vars - set(indep_vars)
11001097
if len(dep_var) == 1:
11011098
dep_var = dep_var.pop()
@@ -1173,11 +1170,11 @@ def plot3d_adaptive(f, x_range, y_range, color='automatic',
11731170
max_depth = max(max_depth, initial_depth)
11741171

11751172
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]
1173+
g, ranges = setup_for_eval_on_grid(f, [x_range, y_range], plot_points=2)
1174+
xmin, xmax = ranges[0][:2]
1175+
ymin, ymax = ranges[1][:2]
11791176

1180-
opacity = float(kwds.get('opacity',1))
1177+
opacity = float(kwds.get('opacity', 1))
11811178

11821179
if color == "automatic":
11831180
texture = rainbow(num_colors, 'rgbtuple')
@@ -1197,9 +1194,9 @@ def plot3d_adaptive(f, x_range, y_range, color='automatic',
11971194
if isinstance(texture, (list, tuple)):
11981195
if len(texture) == 2:
11991196
# 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)
1197+
xticks = (xmax - xmin) / 2**initial_depth
1198+
yticks = (ymax - ymin) / 2**initial_depth
1199+
parts = P.partition(lambda x, y, z: (int((x - xmin) / xticks) + int((y - ymin) / yticks)) % 2)
12031200
else:
12041201
# do a topo coloring
12051202
bounds = P.bounding_box()
@@ -1208,8 +1205,8 @@ def plot3d_adaptive(f, x_range, y_range, color='automatic',
12081205
if max_z == min_z:
12091206
span = 0
12101207
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))
1208+
span = (len(texture) - 1) / (max_z - min_z) # max to avoid dividing by 0
1209+
parts = P.partition(lambda x, y, z: int((z - min_z) * span))
12131210
all = []
12141211
for k, G in parts.items():
12151212
G.set_texture(texture[k], opacity=opacity)

src/sage/plot/plot3d/plot_field3d.py

Lines changed: 7 additions & 6 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,13 +143,14 @@ 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) for v, p in zip(scaled_vectors, points)])
153154
G._set_extra_kwds(kwds)
154155
return G
155156
else:

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]

src/sage/plot/plot3d/shapes2.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
- William Stein and Robert Bradshaw (2008-01): Many improvements
99
"""
10-
#*****************************************************************************
10+
# ****************************************************************************
1111
# Copyright (C) 2007 William Stein <[email protected]>
1212
# Copyright (C) 2008 Robert Bradshaw <[email protected]>
1313
#
@@ -20,10 +20,11 @@
2020
#
2121
# The full text of the GPL is available at:
2222
#
23-
# http://www.gnu.org/licenses/
24-
#*****************************************************************************
23+
# https://www.gnu.org/licenses/
24+
# ****************************************************************************
2525

2626
import math
27+
2728
from . import shapes
2829

2930
from .base import PrimitiveObject, point_list_bounding_box
@@ -34,11 +35,10 @@
3435
from sage.arith.srange import srange
3536

3637
from .texture import Texture
37-
38-
TACHYON_PIXEL = 1/200.0
39-
4038
from .shapes import Text, Sphere
4139

40+
TACHYON_PIXEL = 1 / 200.0
41+
4242

4343
@rename_keyword(alpha='opacity')
4444
def line3d(points, thickness=1, radius=None, arrow_head=False, **kwds):
@@ -516,18 +516,20 @@ def frame_labels(lower_left, upper_right,
516516
# Helper function for formatting the frame labels
517517
from math import log
518518
log10 = log(10)
519-
nd = lambda a: int(log(a)/log10)
519+
520+
def nd(a):
521+
return int(log(a) / log10)
520522

521523
def fmt_string(a):
522-
b = a/2.0
524+
b = a / 2.0
523525
if b >= 1:
524526
return "%.1f"
525527
n = max(0, 2 - nd(a/2.0))
526528
return "%%.%sf" % n
527529

528530
# Slightly faster than mean for this situation
529531
def avg(a, b):
530-
return (a+b)/2.0
532+
return (a + b) / 2.0
531533

532534
color = (0.3, 0.3, 0.3)
533535

@@ -722,8 +724,8 @@ def ruler_frame(lower_left, upper_right, ticks=4, sub_ticks=4, **kwds):
722724
sphinx_plot(ruler_frame([1,2,3],vector([2,3,4]),ticks=6, sub_ticks=2, color='red'))
723725
"""
724726
return ruler(lower_left, (upper_right[0], lower_left[1], lower_left[2]), ticks=ticks, sub_ticks=sub_ticks, absolute=True, **kwds) \
725-
+ ruler(lower_left, (lower_left[0], upper_right[1], lower_left[2]), ticks=ticks, sub_ticks=sub_ticks, absolute=True, **kwds) \
726-
+ ruler(lower_left, (lower_left[0], lower_left[1], upper_right[2]), ticks=ticks, sub_ticks=sub_ticks, absolute=True, **kwds)
727+
+ ruler(lower_left, (lower_left[0], upper_right[1], lower_left[2]), ticks=ticks, sub_ticks=sub_ticks, absolute=True, **kwds) \
728+
+ ruler(lower_left, (lower_left[0], lower_left[1], upper_right[2]), ticks=ticks, sub_ticks=sub_ticks, absolute=True, **kwds)
727729

728730

729731
###########################
@@ -1143,8 +1145,8 @@ def tachyon_repr(self, render_params):
11431145
cmd = ('FCylinder base {pos[0]!r} {pos[1]!r} {pos[2]!r} '
11441146
'apex {apex[0]!r} {apex[1]!r} {apex[2]!r} '
11451147
'rad {radius!r} {texture}').format(
1146-
pos=(px, py, pz), apex=(x, y, z), radius=radius,
1147-
texture=self.texture.id)
1148+
pos=(px, py, pz), apex=(x, y, z), radius=radius,
1149+
texture=self.texture.id)
11481150
cmds.append(cmd)
11491151
px, py, pz = x, y, z
11501152
return cmds

0 commit comments

Comments
 (0)