It is so long because I copied a vertically modified slider from the internet.
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | # Vertical Slider Modification # from here: http://www.tagwith.com/question_718868_add-a-vertical-slider-with-matplotlib # and here: http://stackoverflow.com/questions/25934279/add-a-vertical-slider-with-matplotlib from matplotlib.widgets import AxesWidget class VertSlider(AxesWidget): """ A slider representing a floating point range The following attributes are defined *ax* : the slider :class:`matplotlib.axes.Axes` instance *val* : the current slider value *vline* : a :class:`matplotlib.lines.Line2D` instance representing the initial value of the slider *poly* : A :class:`matplotlib.patches.Polygon` instance which is the slider knob *valfmt* : the format string for formatting the slider text *label* : a :class:`matplotlib.text.Text` instance for the slider label *closedmin* : whether the slider is closed on the minimum *closedmax* : whether the slider is closed on the maximum *slidermin* : another slider - if not *None*, this slider must be greater than *slidermin* *slidermax* : another slider - if not *None*, this slider must be less than *slidermax* *dragging* : allow for mouse dragging on slider Call :meth:`on_changed` to connect to the slider event """ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f', closedmin=True, closedmax=True, slidermin=None, slidermax=None, dragging=True, **kwargs): """ Create a slider from *valmin* to *valmax* in axes *ax* *valinit* The slider initial position *label* The slider label *valfmt* Used to format the slider value *closedmin* and *closedmax* Indicate whether the slider interval is closed *slidermin* and *slidermax* Used to constrain the value of this slider to the values of other sliders. additional kwargs are passed on to ``self.poly`` which is the :class:`matplotlib.patches.Rectangle` which draws the slider knob. See the :class:`matplotlib.patches.Rectangle` documentation valid property names (e.g., *facecolor*, *edgecolor*, *alpha*, ...) """ AxesWidget.__init__(self, ax) self.valmin = valmin self.valmax = valmax self.val = valinit self.valinit = valinit self.poly = ax.axhspan(valmin, valinit, 0, 1, **kwargs) self.vline = ax.axhline(valinit, 0, 1, color='r', lw=1) self.valfmt = valfmt ax.set_xticks([]) ax.set_ylim((valmin, valmax)) ax.set_yticks([]) ax.set_navigate(False) self.connect_event('button_press_event', self._update) self.connect_event('button_release_event', self._update) if dragging: self.connect_event('motion_notify_event', self._update) self.label = ax.text(0.5, 1.03, label, transform=ax.transAxes, verticalalignment='center', horizontalalignment='center') self.valtext = ax.text(0.5, -0.03, valfmt % valinit, transform=ax.transAxes, verticalalignment='center', horizontalalignment='center') self.cnt = 0 self.observers = {} self.closedmin = closedmin self.closedmax = closedmax self.slidermin = slidermin self.slidermax = slidermax self.drag_active = False def _update(self, event): """update the slider position""" if self.ignore(event): return if event.button != 1: return if event.name == 'button_press_event' and event.inaxes == self.ax: self.drag_active = True event.canvas.grab_mouse(self.ax) if not self.drag_active: return elif ((event.name == 'button_release_event') or (event.name == 'button_press_event' and event.inaxes != self.ax)): self.drag_active = False event.canvas.release_mouse(self.ax) return val = event.ydata if val <= self.valmin: if not self.closedmin: return val = self.valmin elif val >= self.valmax: if not self.closedmax: return val = self.valmax if self.slidermin is not None and val <= self.slidermin.val: if not self.closedmin: return val = self.slidermin.val if self.slidermax is not None and val >= self.slidermax.val: if not self.closedmax: return val = self.slidermax.val self.set_val(val) def set_val(self, val): xy = self.poly.xy xy[1] = 0, val xy[2] = 1, val self.poly.xy = xy self.valtext.set_text(self.valfmt % val) if self.drawon: self.ax.figure.canvas.draw() self.val = val if not self.eventson: return for cid, func in self.observers.iteritems(): func(val) def on_changed(self, func): """ When the slider value is changed, call *func* with the new slider position A connection id is returned which can be used to disconnect """ cid = self.cnt self.observers[cid] = func self.cnt += 1 return cid def disconnect(self, cid): """remove the observer with connection id *cid*""" try: del self.observers[cid] except KeyError: pass def reset(self): """reset the slider to the initial value if needed""" if (self.val != self.valinit): self.set_val(self.valinit) # OK done with the vertical slider modification. Here is the real script! import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Cursor def get_setup(cx0=-1.0, cy0=0.0, half_width=2.0): global NSTEP, npcx, npcy hwx = half_width hwy = (9./16.) * half_width # 16:9 cx1, cx2 = cx0 - hwx, cx0 + hwx # -3.0, 1.0 # 4 = 16 / 4 cy1, cy2 = cy0 - hwy, cy0 + hwy # -1.125, 1.125 # 2.25 = 9 / 4 #npcx, npcy = 1280/2, 720/2 npcx, npcy = 1280, 720 cxt = np.linspace(cx1, cx2, npcx) cyt = np.linspace(cy1, cy2, npcy) # so cx = cx1 + (ipix/npcx)*(cx2-cx1) cX, cY = np.meshgrid(cxt, cyt) c = cX + j*cY NSTEP = 100 z = np.zeros((NSTEP, npcy, npcx), dtype = 'complex') z[0] = np.complex(0, 0) nout = np.zeros((npcy, npcx)) return z, nout, c, cx1, cx2, cy1, cy2 def f4(z, c): return z**2 + c def go(): global NSTEP print("confirm 1 c[0,0] = ", c[0,0]) for k in range(1, NSTEP): z[k] = f4(z[k-1], c) out = abs(z[k]) > 2. z[k, out] = np.nan nout[out] = k j = np.complex(0, 1) z, nout, c, cx1, cx2, cy1, cy2 = get_setup() go() fig, ax = plt.subplots() plt.subplots_adjust(left=0.2, bottom=0.05, top=0.95, right=0.95) axcolor = 'lightgoldenrodyellow' #slider ax_zoom = plt.axes([0.08, 0.2, 0.04, 0.6], axisbg=axcolor) # added zoomval = 0.0 zoom = 10.**zoomval zoomvalmax = 5 szoom = VertSlider(ax_zoom, 'zoom', 0, zoomvalmax, valinit=zoomval) # added VertSlider icount = 1000 def p(): global icount zac = np.abs(z[-1]) uhoh = np.isnan(zac) A = 2. * zac A[uhoh] = nout[uhoh] icount += 1 plt.imsave('wowow'+str(icount), A) ax.imshow(A) p() def updatezoom(val): global z, nout, c, cx1, cx2, cy1, cy2 global zoom #global icount zoomval = (max(0.0, min(zoomvalmax,szoom.val))) zoom = 10**zoomval cx, cy = 0.5*(cx1 + cx2), 0.5*(cy1 + cy2) hwz = 2.0 / zoom z, nout, c, cx1, cx2, cy1, cy2 = get_setup(cx0=cx, cy0=cy, half_width=hwz) go() p() #icount += 1 #plt.savefig('wow'+str(icount)) plt.draw() #icount = 1 szoom.on_changed(updatezoom) def onclick(event): global z, nout, c, cx1, cx2, cy1, cy2 global npcx, npcy #npcx, npcy = 1280, 720 if event.x > 132: ipx, ipy = event.xdata, event.ydata cx = cx1 + (float(ipx)/float(npcx)) * (cx2-cx1) cy = cy1 + (float(ipy)/float(npcy)) * (cy2-cy1) print " hey! ", event.button, event.x, event.y print ipx, ipy, cx, cy hwz = 2.0 / zoom z, nout, c, cx1, cx2, cy1, cy2 = get_setup(cx0=cx, cy0=cy, half_width=hwz) go() p() plt.draw() # so cx = cx1 + (ipix/npcx)*(cx2-cx1) cid = fig.canvas.mpl_connect('button_press_event', onclick) plt.savefig("wowza") plt.show() |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.