{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Zoom in using MagnifyCamera\n\nDemonstrates use of special Camera subclasses to implement a (flashy) \ndata-exploration tool.\n\nHere we use MagnifyCamera to allow the user to zoom in on a particular \nregion of data, while also keeping the entire data set visible for reference.\n\nThe MagnifyCamera classes are responsible for inserting MagnifyTransform\nat the transform of each viewbox scene, while also updating those transforms\nto respond to user input.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nimport vispy.scene\nfrom vispy.scene import visuals\nfrom vispy.scene.cameras import MagnifyCamera, Magnify1DCamera\nfrom vispy.visuals.transforms import STTransform\nfrom vispy.util import filter \n\n# \n# Make a canvas and partition it into 3 viewboxes.\n#\ncanvas = vispy.scene.SceneCanvas(keys='interactive', show=True)\ncanvas._send_hover_events = True # temporary workaround\ngrid = canvas.central_widget.add_grid()\n\nvb1 = grid.add_view(row=0, col=0, col_span=2)\nvb2 = grid.add_view(row=1, col=0)\nvb3 = grid.add_view(row=1, col=1)\n\n#\n# Top viewbox: Show a plot line containing fine structure with a 1D \n# magnification transform.\n#\n\n\npos = np.empty((100000, 2))\npos[:, 0] = np.arange(100000)\npos[:, 1] = np.random.normal(size=100000, loc=50, scale=10)\npos[:, 1] = filter.gaussian_filter(pos[:, 1], 20)\npos[:, 1] += np.random.normal(size=100000, loc=0, scale=2)\npos[:, 1][pos[:, 1] > 55] += 100\npos[:, 1] = filter.gaussian_filter(pos[:, 1], 2)\nline = visuals.Line(pos, color='white', parent=vb1.scene)\nline.transform = STTransform(translate=(0, 0, -0.1))\n\ngrid1 = visuals.GridLines(parent=vb1.scene)\n\nvb1.camera = Magnify1DCamera(mag=4, size_factor=0.6, radius_ratio=0.6)\nvb1.camera.rect = 0, 30, 100000, 100\n\n#\n# Bottom-left viewbox: Image with circular magnification lens.\n#\nsize = (100, 100)\n\nimg_data = np.random.normal(size=size+(3,), loc=58, scale=20).astype(np.ubyte)\nimage = visuals.Image(img_data, parent=vb2.scene, method='impostor')\nvb2.camera = MagnifyCamera(mag=3, size_factor=0.3, radius_ratio=0.6)\nvb2.camera.rect = (-10, -10, size[0]+20, size[1]+20) \n\n#\n# Bottom-right viewbox: Scatter plot with many clusters of varying scale.\n#\n\n\ncenters = np.random.normal(size=(50, 2))\npos = np.random.normal(size=(100000, 2), scale=0.2)\nindexes = np.random.normal(size=100000, loc=centers.shape[0]/2., \n scale=centers.shape[0]/3.)\nindexes = np.clip(indexes, 0, centers.shape[0]-1).astype(int)\nscales = 10**(np.linspace(-2, 0.5, centers.shape[0]))[indexes][:, np.newaxis]\npos *= scales\npos += centers[indexes]\n\nscatter = visuals.Markers()\nscatter.set_data(pos, edge_color=None, face_color=(1, 1, 1, 0.3), size=5)\nvb3.add(scatter)\n\ngrid2 = visuals.GridLines(parent=vb3.scene)\nvb3.camera = MagnifyCamera(mag=3, size_factor=0.3, radius_ratio=0.9)\nvb3.camera.rect = (-5, -5, 10, 10)\n\n# Add helpful text\ntext1 = visuals.Text(\"mouse wheel = magnify\", pos=(100, 15), font_size=14, \n color='white', parent=canvas.scene)\ntext2 = visuals.Text(\"right button = zoom\", pos=(100, 30), font_size=14, \n color='white', parent=canvas.scene)\n\n\nif __name__ == '__main__':\n import sys\n if sys.flags.interactive != 1:\n vispy.app.run()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 0 }