distribution.py 4.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from scipy import interpolate
from pylab import *

Dots = list()
Dotsx = list()
Dotsy = list()

#######################################################################################################
# Interpolations

def spline_inter(k,x,y):
    tck = interpolate.splrep(x,y,s=0)
    return ( interpolate.splev(k,tck,der=0) )

def linear_inter(k,x,y):
    f = interp1d(x,y)
    return f(k)

#def stairs_inter(x,y):

#######################################################################################################


class DraggableDot:
    lock = None  # only one can be animated at a time
    def __init__(self, circle):
        self.circle = circle
        self.press = None
        self.background = None
        self.x0,self.y0 = self.circle.center

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.circle.figure.canvas.mpl_connect(
            'button_press_event', self.on_press)
        self.cidrelease = self.circle.figure.canvas.mpl_connect(
            'button_release_event', self.on_release)
        self.cidmotion = self.circle.figure.canvas.mpl_connect(
            'motion_notify_event', self.on_motion)

    def on_press(self, event):
        'on button press we will see if the mouse is over us and store some data'
        if event.inaxes != self.circle.axes: return
        if DraggableDot.lock is not None: return
        contains, attrd = self.circle.contains(event)
        if not contains: return
        #print 'event contains', self.circle.xy
        self.x0, self.y0 = self.circle.center
        self.press = self.x0, self.y0, event.xdata, event.ydata
        DraggableDot.lock = self
	print Dots, Dotsx, Dotsy

        # draw everything but the selected circleangle and store the pixel buffer
        canvas = self.circle.figure.canvas
        axes = self.circle.axes
        self.circle.set_animated(True)
        canvas.draw()
        self.background = canvas.copy_from_bbox(self.circle.axes.bbox)

        # now redraw just the circle
        axes.draw_artist(self.circle)

        # and blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_motion(self, event):
        'on motion we will move the circle if the mouse is over us'
        if DraggableDot.lock is not self:
            return
        if event.inaxes != self.circle.axes: return
        x0, y0, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        self.circle.set_center=(x0,y0)

        canvas = self.circle.figure.canvas
        axes = self.circle.axes
        # restore the background region
        canvas.restore_region(self.background)

        # redraw just the current circleangle
#
        canvas.move(self.circle,-1,-1)
        axes.draw_artist(self.circle)

        # blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_release(self, event):
        'on release we reset the press data'
        if DraggableDot.lock is not self:
            return

        self.press = None
        DraggableDot.lock = None

        # turn off the circle animation property and reset the background
        self.circle.set_animated(False)
        self.background = None

        # redraw the full figure
        self.circle.figure.canvas.draw()

        for i in range(len(Dotsy)):
            Dotsy[i] = event.ydata

    def disconnect(self):
        'disconnect all the stored connection ids'
        self.circle.figure.canvas.mpl_disconnect(self.cidpress)
        self.circle.figure.canvas.mpl_disconnect(self.cidrelease)
        self.circle.figure.canvas.mpl_disconnect(self.cidmotion)

drs = []
fig = plt.figure()
ax = fig.add_subplot(111)
for a in arange(0,50,5):
    temp = ax.add_patch( matplotlib.patches.Circle((a,1), radius=.5)  )#ax.bar(arange(0,55,5), (1 + 0*arange(0,55,5) ) )
    Dots.append(temp)
    dr = DraggableDot(Dots[a/5])
    dr.connect()
    drs.append(dr) 
    Dotsx.insert(a/5,a)
    Dotsy.insert(a/5,1)
    
    
x=arange(0,50,0.1)
#plt.plot(x, linear_inter(x, listay))

ylim([0,50])
xlim([0,50])

plt.show()