Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2855691

Browse files
authored
Merge pull request #18199 from timhoffm/dic-tickedstroke
Some cleanup on TickedStroke
2 parents 640604d + b9fbd01 commit 2855691

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

examples/images_contours_and_fields/contours_in_optimization_demo.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import numpy as np
2424
import matplotlib.pyplot as plt
25-
import matplotlib.patheffects as patheffects
25+
from matplotlib import patheffects
2626

2727
fig, ax = plt.subplots(figsize=(6, 6))
2828

@@ -39,22 +39,22 @@
3939
# Evaluate some stuff to plot
4040
obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2
4141
g1 = -(3*x1 + x2 - 5.5)
42-
g2 = -(x1 + 2*x2 - 4)
42+
g2 = -(x1 + 2*x2 - 4.5)
4343
g3 = 0.8 + x1**-3 - x2
4444

4545
cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16],
46-
colors=('k',))
46+
colors='black')
4747
ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)
4848

49-
cg1 = ax.contour(x1, x2, g1, [0], colors=('k',))
49+
cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')
5050
plt.setp(cg1.collections,
5151
path_effects=[patheffects.withTickedStroke(angle=135)])
5252

53-
cg2 = ax.contour(x1, x2, g2, [0], colors=('r',))
53+
cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
5454
plt.setp(cg2.collections,
5555
path_effects=[patheffects.withTickedStroke(angle=60, length=2)])
5656

57-
cg3 = ax.contour(x1, x2, g3, [0], colors=('b',))
57+
cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
5858
plt.setp(cg3.collections,
5959
path_effects=[patheffects.withTickedStroke(spacing=7)])
6060

examples/misc/tickedstroke_demo.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
</gallery/images_contours_and_fields/contours_in_optimization_demo>`.
2121
"""
2222

23+
###############################################################################
24+
# Applying TickedStroke to paths
25+
# ==============================
2326
import matplotlib.patches as patches
2427
from matplotlib.path import Path
2528
import numpy as np
2629
import matplotlib.pyplot as plt
2730
import matplotlib.patheffects as patheffects
2831

29-
# Direct to path
3032
fig, ax = plt.subplots(figsize=(6, 6))
3133
path = Path.unit_circle()
3234
patch = patches.PathPatch(path, facecolor='none', lw=2, path_effects=[
@@ -40,7 +42,8 @@
4042
plt.show()
4143

4244
###############################################################################
43-
# Lines and curves with plot and legend
45+
# Applying TickedStroke to lines
46+
# ==============================
4447
fig, ax = plt.subplots(figsize=(6, 6))
4548
ax.plot([0, 1], [0, 1], label="Line",
4649
path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)])
@@ -55,6 +58,9 @@
5558
plt.show()
5659

5760
###############################################################################
61+
# Applying TickedStroke to contour plots
62+
# ======================================
63+
#
5864
# Contour plot with objective and constraints.
5965
# Curves generated by contour to represent a typical constraint in an
6066
# optimization problem should be plotted with angles between zero and
@@ -74,22 +80,22 @@
7480
# Evaluate some stuff to plot
7581
obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2
7682
g1 = -(3*x1 + x2 - 5.5)
77-
g2 = -(x1 + 2*x2 - 4)
83+
g2 = -(x1 + 2*x2 - 4.5)
7884
g3 = 0.8 + x1**-3 - x2
7985

8086
cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16],
81-
colors=('k',))
87+
colors='black')
8288
ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)
8389

84-
cg1 = ax.contour(x1, x2, g1, [0], colors='black')
90+
cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')
8591
plt.setp(cg1.collections,
8692
path_effects=[patheffects.withTickedStroke(angle=135)])
8793

88-
cg2 = ax.contour(x1, x2, g2, [0], colors='red')
94+
cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
8995
plt.setp(cg2.collections,
9096
path_effects=[patheffects.withTickedStroke(angle=60, length=2)])
9197

92-
cg3 = ax.contour(x1, x2, g3, [0], colors='blue')
98+
cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
9399
plt.setp(cg3.collections,
94100
path_effects=[patheffects.withTickedStroke(spacing=7)])
95101

lib/matplotlib/patheffects.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def __init__(self, offset=(0., 0.)):
2626
"""
2727
Parameters
2828
----------
29-
offset : pair of floats
30-
The offset to apply to the path, measured in points.
29+
offset : (float, float), default: (0, 0)
30+
The (x, y) offset to apply to the path, measured in points.
3131
"""
3232
self._offset = offset
3333

@@ -38,10 +38,10 @@ def _offset_transform(self, renderer):
3838

3939
def _update_gc(self, gc, new_gc_dict):
4040
"""
41-
Update the given GraphicsCollection with the given
42-
dictionary of properties. The keys in the dictionary are used to
43-
identify the appropriate set_ method on the gc.
41+
Update the given GraphicsContext with the given dict of properties.
4442
43+
The keys in the dictionary are used to identify the appropriate
44+
``set_`` method on the *gc*.
4545
"""
4646
new_gc_dict = new_gc_dict.copy()
4747

@@ -61,7 +61,6 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace=None):
6161
Derived should override this method. The arguments are the same
6262
as :meth:`matplotlib.backend_bases.RendererBase.draw_path`
6363
except the first argument is a renderer.
64-
6564
"""
6665
# Get the real renderer, not a PathEffectRenderer.
6766
if isinstance(renderer, PathEffectRenderer):
@@ -227,8 +226,8 @@ def __init__(self, offset=(2, -2),
227226
"""
228227
Parameters
229228
----------
230-
offset : pair of floats
231-
The offset of the shadow in points.
229+
offset : (float, float), default: (2, -2)
230+
The (x, y) offset of the shadow in points.
232231
shadow_rgbFace : color
233232
The shadow color.
234233
alpha : float, default: 0.3
@@ -295,8 +294,8 @@ def __init__(self, offset=(2, -2),
295294
"""
296295
Parameters
297296
----------
298-
offset : pair of floats
299-
The offset to apply to the path, in points.
297+
offset : (float, float), default: (2, -2)
298+
The (x, y) offset to apply to the path, in points.
300299
shadow_color : color, default: 'black'
301300
The shadow color.
302301
A value of ``None`` takes the original artist's color
@@ -354,8 +353,8 @@ def __init__(self, offset=(0, 0), **kwargs):
354353
"""
355354
Parameters
356355
----------
357-
offset : pair of floats
358-
The offset to apply to the path, in points.
356+
offset : (float, float), default: (0, 0)
357+
The (x, y) offset to apply to the path, in points.
359358
**kwargs
360359
All keyword arguments are passed through to the
361360
:class:`~matplotlib.patches.PathPatch` constructor. The
@@ -401,8 +400,8 @@ def __init__(self, offset=(0, 0),
401400
"""
402401
Parameters
403402
----------
404-
offset : pair of floats, default: (0, 0)
405-
The offset to apply to the path, in points.
403+
offset : (float, float), default: (0, 0)
404+
The (x, y) offset to apply to the path, in points.
406405
spacing : float, default: 10.0
407406
The spacing between ticks in points.
408407
angle : float, default: 45.0
@@ -430,9 +429,7 @@ def __init__(self, offset=(0, 0),
430429
self._gc = kwargs
431430

432431
def draw_path(self, renderer, gc, tpath, affine, rgbFace):
433-
"""
434-
Draw the path with updated gc.
435-
"""
432+
"""Draw the path with updated gc."""
436433
# Do not modify the input! Use copy instead.
437434
gc0 = renderer.new_gc()
438435
gc0.copy_properties(gc)
@@ -445,7 +442,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
445442
[np.sin(theta), np.cos(theta)]])
446443

447444
# Convert spacing parameter to pixels.
448-
spcpx = renderer.points_to_pixels(self._spacing)
445+
spacing_px = renderer.points_to_pixels(self._spacing)
449446

450447
# Transform before evaluation because to_polygons works at resolution
451448
# of one -- assuming it is working in pixel space.
@@ -469,11 +466,11 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
469466

470467
# Build parametric coordinate along curve
471468
s = np.concatenate(([0.0], np.cumsum(ds)))
472-
stot = s[-1]
469+
s_total = s[-1]
473470

474-
num = int(np.ceil(stot / spcpx))-1
471+
num = int(np.ceil(s_total / spacing_px)) - 1
475472
# Pick parameter values for ticks.
476-
s_tick = np.linspace(spcpx/2, stot-spcpx/2, num)
473+
s_tick = np.linspace(spacing_px/2, s_total - spacing_px/2, num)
477474

478475
# Find points along the parameterized curve
479476
x_tick = np.interp(s_tick, s, x)
@@ -493,7 +490,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
493490
uv[mask] = np.array([0, 0]).T
494491

495492
# Rotate and scale unit vector into tick vector
496-
dxy = np.dot(uv, trans_matrix) * self._length * spcpx
493+
dxy = np.dot(uv, trans_matrix) * self._length * spacing_px
497494

498495
# Build tick endpoints
499496
x_end = x_tick + dxy[:, 0]

0 commit comments

Comments
 (0)