|
| 1 | +#+OPTIONS: ':nil *:t -:t ::t <:t H:2 \n:nil ^:t arch:headline |
| 2 | +#+OPTIONS: author:t c:nil creator:comment d:nil date:t e:t email:nil |
| 3 | +#+OPTIONS: f:t inline:t num:t p:nil pri:nil prop:nil stat:t tags:t |
| 4 | +#+OPTIONS: tasks:t tex:t timestamp:t toc:nil todo:t |:t |
| 5 | + |
| 6 | +#+TITLE: Interactive Matplotlib Tutorial |
| 7 | +#+EMAIL: tcaswell@bnl.gov |
| 8 | +#+DATE: 2017-11-06 |
| 9 | +#+AUTHOR: Thomas A Caswell |
| 10 | +#+DESCRIPTION: |
| 11 | +#+KEYWORDS: |
| 12 | +#+LANGUAGE: en |
| 13 | +#+INFOJS_OPT: view:nil toc:nil ltoc:t mouse:underline buttons:0 path:http://orgmode.org/org-info.js |
| 14 | +#+EXPORT_SELECT_TAGS: export |
| 15 | +#+EXPORT_EXCLUDE_TAGS: noexport |
| 16 | +#+LINK_UP: |
| 17 | +#+LINK_HOME: |
| 18 | + |
| 19 | +#+startup: beamer |
| 20 | +#+LaTeX_CLASS: beamer |
| 21 | +#+LaTeX_CLASS_OPTIONS: [x11names] |
| 22 | +#+LATEX_HEADER: \usemintedstyle{emacs} |
| 23 | +#+BEAMER_HEADER: \institute[BNL]{Brookhaven National Labratory} |
| 24 | +#+latex_header: \setbeamertemplate{navigation symbols}{}%remove navigation symbols |
| 25 | +#+latex_header: \usepackage{multicol} |
| 26 | +#+latex_header: \mode<beamer>{\usetheme{Madrid}} \setbeamertemplate{navigation symbols}{} \usepackage{color} \useoutertheme{noslideno} \useinnertheme{default} |
| 27 | +#+BEAMER_COLOR_THEME: seahorse |
| 28 | +#+BEAMER_THEME: Madrid |
| 29 | +#+BEAMER_INNER_THEME: default |
| 30 | +#+BEAMER_HEADER_EXTRA: \includeonlyframes{current} |
| 31 | + |
| 32 | +#+COLUMNS: %40ITEM %10BEAMER_env(Env) %9BEAMER_envargs(Env Args) %4BEAMER_col(Col) %10BEAMER_extra(Extra) |
| 33 | +#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC |
| 34 | + |
| 35 | + |
| 36 | +* 00 Introduction to OO Matplotlib |
| 37 | +** Parts of a Figure |
| 38 | + |
| 39 | + [[./anatomy.pdf]] |
| 40 | + |
| 41 | +https://matplotlib.org/tutorials/introductory/usage.html#parts-of-a-figure |
| 42 | + |
| 43 | +** Matplotlib Layers |
| 44 | + - =Renderer= :: Internal, handles final 'on screen' or 'on disk' version |
| 45 | + - =Canvas= :: Manages a =Figure= and and a =Renderer= |
| 46 | + - Transforms :: between coord systems {'data', 'axes', 'figure', 'screen'} |
| 47 | + - Artists :: 'middle layer', given a =Renderer= can draw self |
| 48 | + - =Axes= and =Figure= methods :: create =Artists= and add to draw-tree |
| 49 | + - =pyplot= :: create =Artists= and add to draw-tree |
| 50 | + |
| 51 | +\hline |
| 52 | + |
| 53 | + - UI events :: mouse/keyboard events |
| 54 | + |
| 55 | +** =pyplot= |
| 56 | + - =pyplot= is intentionally very close to MATLAB ploting API |
| 57 | + - pro: easy for people switching from MATLAB |
| 58 | + - pro: =pyplot= is much terser |
| 59 | + - con: makes some interesting design choices |
| 60 | + - con: has buckets of global state |
| 61 | + - Everything in =pyplot= implemented in terms of =00= |
| 62 | + |
| 63 | + |
| 64 | +** =Axes= and =Figure= methods |
| 65 | +These classes manage: |
| 66 | + |
| 67 | + - manage the draw tree (hold that thought) |
| 68 | + - manage figure size, dpi, axes layout, view limits, axis scales |
| 69 | + - namespace for plotting functions |
| 70 | + - =ax.plot= |
| 71 | + - =ax.hist= |
| 72 | + - =ax.imshow= |
| 73 | + - ... |
| 74 | + |
| 75 | +https://matplotlib.org/api/axes_api.html |
| 76 | + |
| 77 | +** =Aritsts= |
| 78 | + |
| 79 | + - Everything that you see in the figure is an =Artist= (because it draws |
| 80 | + on the =Canvas=) |
| 81 | + - text, marks, images, spines, background patches |
| 82 | + - responsible for translating internal state -> to =Renderer= method calls |
| 83 | + - can mutate everything and re-draw |
| 84 | + - =obj.set_*= and =obj.get_*= methodsy |
| 85 | + |
| 86 | + |
| 87 | +** draw via visitor pattern |
| 88 | + |
| 89 | +#+BEGIN_SRC python |
| 90 | + class Canvas: |
| 91 | + def draw(self): |
| 92 | + render = self.get_renderer() |
| 93 | + self.figure.draw(renderer) |
| 94 | + |
| 95 | + class Figure: |
| 96 | + def draw(self, renderer): |
| 97 | + self.patch.draw(renderer) |
| 98 | + for artist in self.get_children(): |
| 99 | + artist.draw(renderer) |
| 100 | + |
| 101 | + class Line2D: |
| 102 | + def draw(self, renderer): |
| 103 | + renderer.draw_path(self.verticies, ...) |
| 104 | +#+END_SRC |
| 105 | + |
| 106 | +** =Tansforms= and coordinate systems |
| 107 | + |
| 108 | + |
| 109 | ++-----------+-----------------------------+-----------------------------------+ |
| 110 | +|Coords |Transformation object |Description | |
| 111 | ++-----------+-----------------------------+-----------------------------------+ |
| 112 | +|"data" | ax.transData |The coordinate system for the data,| |
| 113 | +| | |controlled by xlim and ylim. | |
| 114 | ++-----------+-----------------------------+-----------------------------------+ |
| 115 | +|"axes" | ax.transAxes |The coordinate system of the | |
| 116 | +| | |Axes; (0, 0) is bottom left of | |
| 117 | +| | | and (1, 1) is top right | |
| 118 | ++-----------+-----------------------------+-----------------------------------+ |
| 119 | +|"figure" | fig.transFigure |The coordinate system of the | |
| 120 | +| | |Figure; (0, 0) is bottom left | |
| 121 | +| | | and (1, 1) is top right. | |
| 122 | ++-----------+-----------------------------+-----------------------------------+ |
| 123 | +|"display" | IdentityTransform() |The pixel coordinate system of the | |
| 124 | +| | |display; (0, 0) is bottom left | |
| 125 | +| | |and (width, height) is top right | |
| 126 | +| | |in pixels. | |
| 127 | ++-----------+-----------------------------+-----------------------------------+ |
| 128 | + |
| 129 | +https://matplotlib.org/tutorials/advanced/transforms_tutorial.html |
| 130 | + |
| 131 | +** =Canvas= |
| 132 | + - holds a =Figure= instance |
| 133 | + - knows how to make a =Renderer= instance |
| 134 | + - for GUI backends typically uses MI and *is* the native GUI widget |
| 135 | + |
| 136 | +https://matplotlib.org/gallery/index.html#embedding-matplotlib-in-graphical-user-interfaces |
| 137 | + |
| 138 | +** =Renderer= |
| 139 | +Takes low-level data and renders to output |
| 140 | + |
| 141 | +Minimal set of methods: |
| 142 | + |
| 143 | +#+BEGIN_SRC python |
| 144 | + class Renderer: |
| 145 | + def draw_path(self, ...): |
| 146 | + ... |
| 147 | + def draw_image(self, ...): |
| 148 | + ... |
| 149 | + def draw_text(self, ...): |
| 150 | + ... |
| 151 | + def get_text_width_height_descent(self, ...): |
| 152 | + ... |
| 153 | + |
| 154 | +#+END_SRC |
| 155 | + |
| 156 | +May have other methods to allow for optimizations |
| 157 | + |
| 158 | +Typically should not have to know this exists (unless implementing a |
| 159 | +new backend). |
| 160 | + |
| 161 | + |
| 162 | +** UI events on =Canvas= |
| 163 | + |
| 164 | ++-----------------------+----------------------------------------+ |
| 165 | +|Event name | Description | |
| 166 | ++-----------------------+----------------------------------------+ |
| 167 | +|`button_press_event' | mouse button is pressed | |
| 168 | +|`button_release_event' | mouse button is released | |
| 169 | +|`draw_event' | canvas draw (but before screen update) | |
| 170 | +|`key_press_event' | key is pressed | |
| 171 | +|`key_release_event' | key is released | |
| 172 | +|`motion_notify_event' | mouse motion | |
| 173 | +|`pick_event' | an object in the canvas is selected | |
| 174 | +|`resize_event' | figure canvas is resized | |
| 175 | +|`scroll_event' | mouse scroll wheel is rolled | |
| 176 | +|`figure_enter_event' | mouse enters a new figure | |
| 177 | +|`figure_leave_event' | mouse leaves a figure | |
| 178 | +|`axes_enter_event' | mouse enters a new axes | |
| 179 | +|`axes_leave_event' | mouse leaves an axes | |
| 180 | ++-----------------------+----------------------------------------+ |
| 181 | + |
| 182 | + - =cid = canvas.mpl_connect(event_name, callback)= |
| 183 | + |
| 184 | +* 00 Installation |
| 185 | +** installation |
| 186 | + |
| 187 | + =conda create mpl_tut -c anaconda matplotlib pandas pytables ipython python=3.6= |
0 commit comments