Skip to content
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

pep8ify examples (part2) #3488

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions examples/color/color_cycle_demo.py
Expand Up @@ -17,7 +17,7 @@
yy = np.transpose([np.sin(x + phi) for phi in offsets])

plt.rc('lines', linewidth=4)
fig, (ax0, ax1) = plt.subplots(nrows=2)
fig, (ax0, ax1) = plt.subplots(nrows=2)

plt.rc('axes', color_cycle=['r', 'g', 'b', 'y'])
ax0.plot(yy)
Expand All @@ -30,5 +30,3 @@
# Tweak spacing between subplots to prevent labels from overlapping
plt.subplots_adjust(hspace=0.3)
plt.show()


1 change: 1 addition & 0 deletions examples/color/colormaps_reference.py
Expand Up @@ -59,6 +59,7 @@
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))


def plot_color_gradients(cmap_category, cmap_list):
fig, axes = plt.subplots(nrows=nrows)
fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)
Expand Down
1 change: 1 addition & 0 deletions examples/event_handling/close_event.py
@@ -1,6 +1,7 @@
from __future__ import print_function
import matplotlib.pyplot as plt


def handle_close(evt):
print('Closed Figure!')

Expand Down
55 changes: 30 additions & 25 deletions examples/event_handling/data_browser.py
Expand Up @@ -7,59 +7,65 @@ class PointBrowser:
generated the point will be shown in the lower axes. Use the 'n'
and 'p' keys to browse through the next and previous points
"""

def __init__(self):
self.lastind = 0

self.text = ax.text(0.05, 0.95, 'selected: none',
transform=ax.transAxes, va='top')
self.selected, = ax.plot([xs[0]], [ys[0]], 'o', ms=12, alpha=0.4,
color='yellow', visible=False)
self.selected, = ax.plot([xs[0]], [ys[0]], 'o', ms=12, alpha=0.4,
color='yellow', visible=False)

def onpress(self, event):
if self.lastind is None: return
if event.key not in ('n', 'p'): return
if event.key=='n': inc = 1
else: inc = -1

if self.lastind is None:
return
if event.key not in ('n', 'p'):
return
if event.key == 'n':
inc = 1
else:
inc = -1

self.lastind += inc
self.lastind = np.clip(self.lastind, 0, len(xs)-1)
self.lastind = np.clip(self.lastind, 0, len(xs) - 1)
self.update()

def onpick(self, event):

if event.artist!=line: return True

N = len(event.ind)
if not N: return True
if event.artist != line:
return True

# the click locations
x = event.mouseevent.xdata
y = event.mouseevent.ydata
N = len(event.ind)
if not N:
return True

# the click locations
x = event.mouseevent.xdata
y = event.mouseevent.ydata

distances = np.hypot(x-xs[event.ind], y-ys[event.ind])
indmin = distances.argmin()
dataind = event.ind[indmin]
distances = np.hypot(x - xs[event.ind], y - ys[event.ind])
indmin = distances.argmin()
dataind = event.ind[indmin]

self.lastind = dataind
self.update()
self.lastind = dataind
self.update()

def update(self):
if self.lastind is None: return
if self.lastind is None:
return

dataind = self.lastind

ax2.cla()
ax2.plot(X[dataind])

ax2.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]),
transform=ax2.transAxes, va='top')
ax2.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f' % (
xs[dataind], ys[dataind]), transform=ax2.transAxes, va='top')
ax2.set_ylim(-0.5, 1.5)
self.selected.set_visible(True)
self.selected.set_data(xs[dataind], ys[dataind])

self.text.set_text('selected: %d'%dataind)
self.text.set_text('selected: %d' % dataind)
fig.canvas.draw()


Expand All @@ -80,4 +86,3 @@ def update(self):
fig.canvas.mpl_connect('key_press_event', browser.onpress)

plt.show()

6 changes: 4 additions & 2 deletions examples/event_handling/figure_axes_enter_leave.py
Expand Up @@ -5,21 +5,25 @@
from __future__ import print_function
import matplotlib.pyplot as plt


def enter_axes(event):
print('enter_axes', event.inaxes)
event.inaxes.patch.set_facecolor('yellow')
event.canvas.draw()


def leave_axes(event):
print('leave_axes', event.inaxes)
event.inaxes.patch.set_facecolor('white')
event.canvas.draw()


def enter_figure(event):
print('enter_figure', event.canvas.figure)
event.canvas.figure.patch.set_facecolor('red')
event.canvas.draw()


def leave_figure(event):
print('leave_figure', event.canvas.figure)
event.canvas.figure.patch.set_facecolor('grey')
Expand All @@ -42,5 +46,3 @@ def leave_figure(event):
fig2.canvas.mpl_connect('axes_leave_event', leave_axes)

plt.show()


10 changes: 5 additions & 5 deletions examples/event_handling/idle_and_timeout.py
Expand Up @@ -16,13 +16,15 @@
line2, = ax.plot(y2)

N = 100


def on_idle(event):
on_idle.count +=1
on_idle.count += 1
print('idle', on_idle.count)
line1.set_ydata(np.sin(2*np.pi*t*(N-on_idle.count)/float(N)))
line1.set_ydata(np.sin(2*np.pi*t*(N - on_idle.count)/float(N)))
event.canvas.draw()
# test boolean return removal
if on_idle.count==N:
if on_idle.count == N:
return False
return True
on_idle.cid = None
Expand All @@ -31,5 +33,3 @@ def on_idle(event):
fig.canvas.mpl_connect('idle_event', on_idle)

plt.show()


2 changes: 1 addition & 1 deletion examples/event_handling/keypress_demo.py
Expand Up @@ -12,7 +12,7 @@
def press(event):
print('press', event.key)
sys.stdout.flush()
if event.key=='x':
if event.key == 'x':
visible = xl.get_visible()
xl.set_visible(not visible)
fig.canvas.draw()
Expand Down
19 changes: 13 additions & 6 deletions examples/event_handling/lasso_demo.py
Expand Up @@ -15,14 +15,18 @@
from numpy import nonzero
from numpy.random import rand


class Datum(object):
colorin = colorConverter.to_rgba('red')
colorout = colorConverter.to_rgba('blue')

def __init__(self, x, y, include=False):
self.x = x
self.y = y
if include: self.color = self.colorin
else: self.color = self.colorout
if include:
self.color = self.colorin
else:
self.color = self.colorout


class LassoManager(object):
Expand Down Expand Up @@ -61,17 +65,20 @@ def callback(self, verts):
del self.lasso

def onpress(self, event):
if self.canvas.widgetlock.locked(): return
if event.inaxes is None: return
self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
if self.canvas.widgetlock.locked():
return
if event.inaxes is None:
return
self.lasso = Lasso(
event.inaxes, (event.xdata, event.ydata), self.callback)
# acquire a lock on the widget drawing
self.canvas.widgetlock(self.lasso)

if __name__ == '__main__':

data = [Datum(*xy) for xy in rand(100, 2)]

ax = plt.axes(xlim=(0,1), ylim=(0,1), autoscale_on=False)
ax = plt.axes(xlim=(0, 1), ylim=(0, 1), autoscale_on=False)
lman = LassoManager(ax, data)

plt.show()
61 changes: 31 additions & 30 deletions examples/event_handling/looking_glass.py
Expand Up @@ -4,43 +4,44 @@
x, y = np.random.rand(2, 200)

fig, ax = plt.subplots()
circ = patches.Circle( (0.5, 0.5), 0.25, alpha=0.8, fc='yellow')
circ = patches.Circle((0.5, 0.5), 0.25, alpha=0.8, fc='yellow')
ax.add_patch(circ)


ax.plot(x, y, alpha=0.2)
line, = ax.plot(x, y, alpha=1.0, clip_path=circ)


class EventHandler:
def __init__(self):
fig.canvas.mpl_connect('button_press_event', self.onpress)
fig.canvas.mpl_connect('button_release_event', self.onrelease)
fig.canvas.mpl_connect('motion_notify_event', self.onmove)
self.x0, self.y0 = circ.center
self.pressevent = None

def onpress(self, event):
if event.inaxes!=ax:
return

if not circ.contains(event)[0]:
return

self.pressevent = event

def onrelease(self, event):
self.pressevent = None
self.x0, self.y0 = circ.center

def onmove(self, event):
if self.pressevent is None or event.inaxes!=self.pressevent.inaxes:
return

dx = event.xdata - self.pressevent.xdata
dy = event.ydata - self.pressevent.ydata
circ.center = self.x0 + dx, self.y0 + dy
line.set_clip_path(circ)
fig.canvas.draw()
def __init__(self):
fig.canvas.mpl_connect('button_press_event', self.onpress)
fig.canvas.mpl_connect('button_release_event', self.onrelease)
fig.canvas.mpl_connect('motion_notify_event', self.onmove)
self.x0, self.y0 = circ.center
self.pressevent = None

def onpress(self, event):
if event.inaxes != ax:
return

if not circ.contains(event)[0]:
return

self.pressevent = event

def onrelease(self, event):
self.pressevent = None
self.x0, self.y0 = circ.center

def onmove(self, event):
if self.pressevent is None or event.inaxes != self.pressevent.inaxes:
return

dx = event.xdata - self.pressevent.xdata
dy = event.ydata - self.pressevent.ydata
circ.center = self.x0 + dx, self.y0 + dy
line.set_clip_path(circ)
fig.canvas.draw()

handler = EventHandler()
plt.show()