diff --git a/examples/images_contours_and_fields/contours_in_optimization_demo.py b/examples/images_contours_and_fields/contours_in_optimization_demo.py index ab7fa019602f..866ef0edd24d 100644 --- a/examples/images_contours_and_fields/contours_in_optimization_demo.py +++ b/examples/images_contours_and_fields/contours_in_optimization_demo.py @@ -22,7 +22,7 @@ import numpy as np import matplotlib.pyplot as plt -import matplotlib.patheffects as patheffects +from matplotlib import patheffects fig, ax = plt.subplots(figsize=(6, 6)) @@ -39,22 +39,22 @@ # Evaluate some stuff to plot obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2 g1 = -(3*x1 + x2 - 5.5) -g2 = -(x1 + 2*x2 - 4) +g2 = -(x1 + 2*x2 - 4.5) g3 = 0.8 + x1**-3 - x2 cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16], - colors=('k',)) + colors='black') ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True) -cg1 = ax.contour(x1, x2, g1, [0], colors=('k',)) +cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown') plt.setp(cg1.collections, path_effects=[patheffects.withTickedStroke(angle=135)]) -cg2 = ax.contour(x1, x2, g2, [0], colors=('r',)) +cg2 = ax.contour(x1, x2, g2, [0], colors='orangered') plt.setp(cg2.collections, path_effects=[patheffects.withTickedStroke(angle=60, length=2)]) -cg3 = ax.contour(x1, x2, g3, [0], colors=('b',)) +cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue') plt.setp(cg3.collections, path_effects=[patheffects.withTickedStroke(spacing=7)]) diff --git a/examples/misc/tickedstroke_demo.py b/examples/misc/tickedstroke_demo.py index 2079e40285a4..0e4b00b7d5cb 100644 --- a/examples/misc/tickedstroke_demo.py +++ b/examples/misc/tickedstroke_demo.py @@ -20,13 +20,15 @@ `. """ +############################################################################### +# Applying TickedStroke to paths +# ============================== import matplotlib.patches as patches from matplotlib.path import Path import numpy as np import matplotlib.pyplot as plt import matplotlib.patheffects as patheffects -# Direct to path fig, ax = plt.subplots(figsize=(6, 6)) path = Path.unit_circle() patch = patches.PathPatch(path, facecolor='none', lw=2, path_effects=[ @@ -40,7 +42,8 @@ plt.show() ############################################################################### -# Lines and curves with plot and legend +# Applying TickedStroke to lines +# ============================== fig, ax = plt.subplots(figsize=(6, 6)) ax.plot([0, 1], [0, 1], label="Line", path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)]) @@ -55,6 +58,9 @@ plt.show() ############################################################################### +# Applying TickedStroke to contour plots +# ====================================== +# # Contour plot with objective and constraints. # Curves generated by contour to represent a typical constraint in an # optimization problem should be plotted with angles between zero and @@ -74,22 +80,22 @@ # Evaluate some stuff to plot obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2 g1 = -(3*x1 + x2 - 5.5) -g2 = -(x1 + 2*x2 - 4) +g2 = -(x1 + 2*x2 - 4.5) g3 = 0.8 + x1**-3 - x2 cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16], - colors=('k',)) + colors='black') ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True) -cg1 = ax.contour(x1, x2, g1, [0], colors='black') +cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown') plt.setp(cg1.collections, path_effects=[patheffects.withTickedStroke(angle=135)]) -cg2 = ax.contour(x1, x2, g2, [0], colors='red') +cg2 = ax.contour(x1, x2, g2, [0], colors='orangered') plt.setp(cg2.collections, path_effects=[patheffects.withTickedStroke(angle=60, length=2)]) -cg3 = ax.contour(x1, x2, g3, [0], colors='blue') +cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue') plt.setp(cg3.collections, path_effects=[patheffects.withTickedStroke(spacing=7)]) diff --git a/lib/matplotlib/patheffects.py b/lib/matplotlib/patheffects.py index 76544f561bed..a5f5b7c42e3b 100644 --- a/lib/matplotlib/patheffects.py +++ b/lib/matplotlib/patheffects.py @@ -26,8 +26,8 @@ def __init__(self, offset=(0., 0.)): """ Parameters ---------- - offset : pair of floats - The offset to apply to the path, measured in points. + offset : (float, float), default: (0, 0) + The (x, y) offset to apply to the path, measured in points. """ self._offset = offset @@ -38,10 +38,10 @@ def _offset_transform(self, renderer): def _update_gc(self, gc, new_gc_dict): """ - Update the given GraphicsCollection with the given - dictionary of properties. The keys in the dictionary are used to - identify the appropriate set_ method on the gc. + Update the given GraphicsContext with the given dict of properties. + The keys in the dictionary are used to identify the appropriate + ``set_`` method on the *gc*. """ new_gc_dict = new_gc_dict.copy() @@ -61,7 +61,6 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace=None): Derived should override this method. The arguments are the same as :meth:`matplotlib.backend_bases.RendererBase.draw_path` except the first argument is a renderer. - """ # Get the real renderer, not a PathEffectRenderer. if isinstance(renderer, PathEffectRenderer): @@ -227,8 +226,8 @@ def __init__(self, offset=(2, -2), """ Parameters ---------- - offset : pair of floats - The offset of the shadow in points. + offset : (float, float), default: (2, -2) + The (x, y) offset of the shadow in points. shadow_rgbFace : color The shadow color. alpha : float, default: 0.3 @@ -295,8 +294,8 @@ def __init__(self, offset=(2, -2), """ Parameters ---------- - offset : pair of floats - The offset to apply to the path, in points. + offset : (float, float), default: (2, -2) + The (x, y) offset to apply to the path, in points. shadow_color : color, default: 'black' The shadow color. A value of ``None`` takes the original artist's color @@ -354,8 +353,8 @@ def __init__(self, offset=(0, 0), **kwargs): """ Parameters ---------- - offset : pair of floats - The offset to apply to the path, in points. + offset : (float, float), default: (0, 0) + The (x, y) offset to apply to the path, in points. **kwargs All keyword arguments are passed through to the :class:`~matplotlib.patches.PathPatch` constructor. The @@ -401,8 +400,8 @@ def __init__(self, offset=(0, 0), """ Parameters ---------- - offset : pair of floats, default: (0, 0) - The offset to apply to the path, in points. + offset : (float, float), default: (0, 0) + The (x, y) offset to apply to the path, in points. spacing : float, default: 10.0 The spacing between ticks in points. angle : float, default: 45.0 @@ -430,9 +429,7 @@ def __init__(self, offset=(0, 0), self._gc = kwargs def draw_path(self, renderer, gc, tpath, affine, rgbFace): - """ - Draw the path with updated gc. - """ + """Draw the path with updated gc.""" # Do not modify the input! Use copy instead. gc0 = renderer.new_gc() gc0.copy_properties(gc) @@ -445,7 +442,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace): [np.sin(theta), np.cos(theta)]]) # Convert spacing parameter to pixels. - spcpx = renderer.points_to_pixels(self._spacing) + spacing_px = renderer.points_to_pixels(self._spacing) # Transform before evaluation because to_polygons works at resolution # of one -- assuming it is working in pixel space. @@ -469,11 +466,11 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace): # Build parametric coordinate along curve s = np.concatenate(([0.0], np.cumsum(ds))) - stot = s[-1] + s_total = s[-1] - num = int(np.ceil(stot / spcpx))-1 + num = int(np.ceil(s_total / spacing_px)) - 1 # Pick parameter values for ticks. - s_tick = np.linspace(spcpx/2, stot-spcpx/2, num) + s_tick = np.linspace(spacing_px/2, s_total - spacing_px/2, num) # Find points along the parameterized curve x_tick = np.interp(s_tick, s, x) @@ -493,7 +490,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace): uv[mask] = np.array([0, 0]).T # Rotate and scale unit vector into tick vector - dxy = np.dot(uv, trans_matrix) * self._length * spcpx + dxy = np.dot(uv, trans_matrix) * self._length * spacing_px # Build tick endpoints x_end = x_tick + dxy[:, 0]