|
1 | 1 | """
|
2 |
| -================= |
3 |
| -Blitting tutorial |
4 |
| -================= |
| 2 | +================================== |
| 3 | +Faster rendering by using blitting |
| 4 | +================================== |
5 | 5 |
|
6 |
| -'Blitting' is a `standard technique |
| 6 | +*Blitting* is a `standard technique |
7 | 7 | <https://en.wikipedia.org/wiki/Bit_blit>`__ in raster graphics that,
|
8 | 8 | in the context of Matplotlib, can be used to (drastically) improve
|
9 | 9 | performance of interactive figures. For example, the
|
10 | 10 | :mod:`~.animation` and :mod:`~.widgets` modules use blitting
|
11 | 11 | internally. Here, we demonstrate how to implement your own blitting, outside
|
12 | 12 | of these classes.
|
13 | 13 |
|
14 |
| -The source of the performance gains is simply not re-doing work we do |
15 |
| -not have to. If the limits of an Axes have not changed, then there is |
16 |
| -no need to re-draw all of the ticks and tick-labels (particularly |
17 |
| -because text is one of the more expensive things to render). |
| 14 | +Blitting speeds up repetitive drawing by rendering all non-changing |
| 15 | +graphic elements into a background image once. Then, for every draw, only the |
| 16 | +changing elements need to be drawn onto this background. For example, |
| 17 | +if the limits of an Axes have not changed, we can render the empty Axes |
| 18 | +including all ticks and labels once, and only draw the changing data later. |
18 | 19 |
|
19 |
| -The procedure to save our work is roughly: |
| 20 | +The strategy is |
20 | 21 |
|
21 |
| -- draw the figure, but exclude any artists marked as 'animated' |
22 |
| -- save a copy of the RBGA buffer |
| 22 | +- Prepare the constant background: |
23 | 23 |
|
24 |
| -In the future, to update the 'animated' artists we |
| 24 | + - Draw the figure, but exclude all artists that you want to animate by |
| 25 | + marking them as *animated* (see `.Artist.set_animated`). |
| 26 | + - Save a copy of the RBGA buffer. |
25 | 27 |
|
26 |
| -- restore our copy of the RGBA buffer |
27 |
| -- redraw only the animated artists |
28 |
| -- show the resulting image on the screen |
| 28 | +- Render the individual images: |
29 | 29 |
|
30 |
| -thus saving us from having to re-draw everything which is _not_ |
31 |
| -animated. One consequence of this procedure is that your animated |
32 |
| -artists are always drawn at a higher z-order than the static artists. |
| 30 | + - Restore the copy of the RGBA buffer. |
| 31 | + - Redraw the animated artists using `.Axes.draw_artist` / |
| 32 | + `.Figure.draw_artist`. |
| 33 | + - Show the resulting image on the screen. |
| 34 | +
|
| 35 | +One consequence of this procedure is that your animated artists are always |
| 36 | +drawn on top of the static artists. |
33 | 37 |
|
34 | 38 | Not all backends support blitting. You can check if a given canvas does via
|
35 | 39 | the `.FigureCanvasBase.supports_blit` property.
|
|
0 commit comments