-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic-hypotrochoid.py
199 lines (173 loc) · 7.31 KB
/
dynamic-hypotrochoid.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import numpy as np
import matplotlib.pyplot as plt
import scipy.special
import warnings
warnings.filterwarnings(category=RuntimeWarning, action="ignore")
# configure plotting preferences
# http://wiki.scipy.org/Cookbook/Matplotlib/LaTeX_Examples
pts_per_inch=72.27 # this is a latex constant, don't change it.
text_width_in_pts=300.0 # write "\the\textwidth" (or "\showthe\columnwidth" for a 2 collumn text)
# inside a figure environment in latex, the result will be on the dvi/pdf next to the figure. See url above.
text_width_in_inches=text_width_in_pts/pts_per_inch
golden_ratio=0.618 # make rectangles with a nice proportion
inverse_latex_scale=2 # figure.png or figure.eps will be intentionally larger, because it is prettier
# when compiling latex code, use \includegraphics[scale=(1/inverse_latex_scale)]{figure}
fig_proportion = (1.0) # we want the figure to occupy 2/3 (for example) of the text width
csize=inverse_latex_scale*fig_proportion*text_width_in_inches
fig_ratio = 1.0
fig_size=(1.0*csize,fig_ratio*csize) # always 1.0 on the first argument
fig=plt.figure(1,figsize=fig_size) # figsize accepts only inches. if you rather think in cm, change the code yourself.
fig.clf()
class Positions:
pass
pos = Positions()
pos.top = 0.90
pos.bottom = 0.10
pos.left = 0.10
pos.right = 0.90
pos.hspace = 0.00
pos.wspace = 0.02
fig.subplots_adjust(top=pos.top,bottom=pos.bottom,left=pos.left,right=pos.right,hspace=0.00,wspace=0.03)
ax=fig.add_subplot(111)
text_size=inverse_latex_scale*12 # find out the fontsize of your latex text, and put it here
tick_size=inverse_latex_scale*8
# learn how to configure: http://matplotlib.sourceforge.net/users/customizing.html
params = {'backend': 'ps',
'axes.labelsize': text_size,
#'axes.linewidth' : 0,
'text.fontsize': text_size,
'legend.fontsize': text_size,
'legend.handlelength': 2.5,
'legend.borderaxespad': 0,
'xtick.labelsize': tick_size,
'ytick.labelsize': tick_size,
'font.family':'serif',
'font.size': text_size,
'font.serif':['Times'], # Times, Palatino, New Century Schoolbook, Bookman, Computer Modern Roman
'ps.usedistiller': 'xpdf',
#'text.latex.preamble' : [ '\usepackage{mathptmx}'], # include here any neede package for latex; times roman fonts for math
'text.latex.preamble' : [r'\usepackage{amsmath}'],
#'mathtext.fontset': 'stixsans',
'text.usetex': True,
'figure.figsize': fig_size}
plt.rcParams.update(params)
plt.hold('on')
# define functions
def beta_func(a, b):
e1 = scipy.special.gamma(a)
e2 = scipy.special.gamma(b)
e3 = scipy.special.gamma(a + b)
return (e1*e2)/e3
beta_dist = lambda x,a,b: (1.0/beta_func(a,b)) * x**(a-1.0) * (1-x)**(b-1.0)
def lissajous(t,a,b):
x = 1.0 * np.sin(a*t + delta)
y = 1.0 * np.sin(b*t)
return np.array([x,y])
def hypotrochoid(t,k,l):
"""https://en.wikipedia.org/wiki/Spirograph"""
x = (1.0-k)*np.cos(t) + k*l*np.cos((1.0-k)*t/k)
y = (1.0-k)*np.sin(t) - k*l*np.sin((1.0-k)*t/k)
return np.array([x,y])
periods = 10.0
R = 1.0
x = np.linspace(0,1,101)
t = np.linspace(0,2.0*np.pi*periods,501)
delta = 0.0
# Gamma = np.linspace(0,3.9,101)
# ax.plot(Gamma,np.ones(len(Gamma)),color='black',ls='-',lw=2)
# ax.plot(Gamma,Gamma+1,color='black',ls='--',lw=2)
ax.set_xlabel(r"$k = $ inner radius / outer radius")
ax.set_ylabel(r"$l$")
pos.x_min = 0.0
pos.x_max = 2.0
pos.y_min = 0.0
pos.y_max = 2.0
pos.x_scaling = 0.3
pos.y_scaling = 0.3
ax.axis([pos.x_min,pos.x_max,pos.y_min,pos.y_max])
ax.set_xticks(np.arange(0,2.5,0.2))
ax.set_yticks(np.arange(0,2.5,0.2))
class MouseBeta(object):
def __init__(self, ax, **kwargs):
self.ax = ax
self.x = np.linspace(0,1,101)
self.t = np.linspace(0,2.0*np.pi*periods,501)
# self.curve = lambda a,b: beta_dist(self.x,a+1.0,b)
self.curve = lambda k,l: hypotrochoid(self.t,k,l)
# self.scaling = [pos.x_scaling,pos.y_scaling]
self.line, = self.ax.plot(self.curve(0,0)[0]*pos.x_scaling,
self.curve(0,0)[1]*pos.y_scaling,
visible=False, **kwargs)
def shape(self,a,b):
return a*self.x + b*self.x**2
def show_beta(self, event):
if event.inaxes == self.ax:
c = self.curve(event.xdata,event.ydata)
rescale = 1.0 #np.max([R, R + event.xdata])
self.line.set_data(event.xdata+(c[0]/rescale-0*pos.x_scaling)*pos.x_scaling,
event.ydata+(c[1]/rescale-0*pos.y_scaling)*pos.y_scaling)
self.line.set_visible(True)
else:
self.line.set_visible(False)
plt.draw()
def insert_window(ax,pos,pair,x):
Ystretch = 1.0
sizeX = 2.0*pos.x_scaling
sizeY = 2.0*pos.y_scaling
sizeY_stretched = sizeY*Ystretch
ax2sizeX = (sizeX/(pos.x_max-pos.x_min)) * (pos.right-pos.left)
ax2sizeY = (sizeY/(pos.y_max-pos.y_min)) * (pos.top-pos.bottom)
# ax2sizeY = (sizeY_stretched/(pos.y_max-pos.y_min)) * (pos.top-pos.bottom)
pos_x_window = ((pair[0]-pos.x_min)/(pos.x_max-pos.x_min)) * (pos.right-pos.left) + pos.left - ax2sizeX/2.0
pos_y_window = ((pair[1]-pos.y_min)/(pos.y_max-pos.y_min)) * (pos.top-pos.bottom) + pos.bottom - ax2sizeY/fig_ratio/2.0/Ystretch
ax2 = fig.add_axes([pos_x_window, pos_y_window, ax2sizeX, ax2sizeY/fig_ratio],frameon=False)
curve = hypotrochoid(t,pair[0], pair[1])
ax2.axis([0,1,0,Ystretch])
ax2.plot(curve[0],curve[1])
# ax2.fill_between(x, curve, y2=0, edgecolor='None', color='blue',alpha=0.3)
ax2.set_xticks([])
ax2.set_yticks([])
ax2.set_xlim([-1,1])
ax2.set_ylim([-1,1])
return ax2
pos.pt_x = 1.0
pos.pt_y = 1.0
pairs = []
ax.grid(True)
pos.ax_number = 0
pos.ax_vector = []
# Function to be called when mouse is clicked
def on_click(event):
if event.xdata<pos.x_max and event.xdata>pos.x_min and \
event.ydata<pos.y_max and event.ydata>pos.y_min:
pos.ax_number += 1
pos.ax_vector.append(insert_window(ax,pos,(event.xdata,event.ydata),x))
fig.canvas.draw()
def erase_axis(event):
if pos.ax_number > 0:
# print('you pressed', event.key, event.xdata, event.ydata)
if event.key == ' ':
fig.delaxes(pos.ax_vector[pos.ax_number-1])
pos.ax_vector = pos.ax_vector[:-1]
pos.ax_number -= 1
if event.key == 'a':
while pos.ax_number > 0:
fig.delaxes(pos.ax_vector[pos.ax_number-1])
pos.ax_vector = pos.ax_vector[:-1]
pos.ax_number -= 1
plt.draw()
def dynamic_shape(event):
if event.xdata<pos.x_max and event.xdata>pos.x_min and \
event.ydata<pos.y_max and event.ydata>pos.y_min:
plt.title("x={:f}, y={:f}".format(event.xdata,event.ydata))
fig.canvas.draw()
# click on the figure to draw insets
# press spacebar to erase last inset
# press 'a' to erase all insets
# Connect the click function to the button press event
fig.canvas.mpl_connect('button_press_event', on_click)
fig.canvas.mpl_connect('key_press_event', erase_axis)
betashape = MouseBeta(ax, color='blue', lw=3)
fig.canvas.mpl_connect('motion_notify_event', betashape.show_beta)
# fig.canvas.mpl_connect('motion_notify_event', dynamic_shape)
plt.show()