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

Skip to content

Some cleanup on TickedStroke #18199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)])

Expand Down
20 changes: 13 additions & 7 deletions examples/misc/tickedstroke_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
</gallery/images_contours_and_fields/contours_in_optimization_demo>`.
"""

###############################################################################
# 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=[
Expand All @@ -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)])
Expand All @@ -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
Expand All @@ -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)])

Expand Down
41 changes: 19 additions & 22 deletions lib/matplotlib/patheffects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()

Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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]
Expand Down