-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathcanvasagg.py
More file actions
72 lines (58 loc) · 2.38 KB
/
canvasagg.py
File metadata and controls
72 lines (58 loc) · 2.38 KB
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
"""
==============
CanvasAgg demo
==============
This example shows how to use the agg backend directly to create images, which
may be of use to web application developers who want full control over their
code without using the pyplot interface to manage figures, figure closing etc.
.. note::
It is not necessary to avoid using the pyplot interface in order to
create figures without a graphical front-end - simply setting
the backend to "Agg" would be sufficient.
In this example, we show how to save the contents of the agg canvas to a file,
and how to extract them to a numpy array, which can in turn be passed off
to Pillow_. The latter functionality allows e.g. to use Matplotlib inside a
cgi-script *without* needing to write a figure to disk, and to write images in
any format supported by Pillow.
.. _Pillow: https://pillow.readthedocs.io/
.. redirect-from:: /gallery/misc/agg_buffer
.. redirect-from:: /gallery/misc/agg_buffer_to_array
"""
# sphinx_gallery_thumbnail_path = '_static/canvasagg.png'
from PIL import Image
import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
fig = Figure(figsize=(5, 4), dpi=100)
# Do some plotting.
ax = fig.add_subplot()
ax.plot([1, 2, 3])
# Option 1: Save the figure to a file; can also be a file-like object (BytesIO,
# etc.).
fig.savefig("test.png")
# Option 2 (low-level approach to directly save to a numpy array): Manually
# attach a canvas to the figure (pyplot or savefig would automatically do
# it), by instantiating the canvas with the figure as argument; then draw the
# figure, retrieve a memoryview on the renderer buffer, and convert it to a
# numpy array.
canvas = FigureCanvasAgg(fig)
canvas.draw()
rgba = np.asarray(canvas.buffer_rgba())
# ... and pass it to PIL.
im = Image.fromarray(rgba)
# This image can then be saved to any format supported by Pillow, e.g.:
im.save("test.bmp")
# Uncomment this line to display the image using ImageMagick's `display` tool.
# im.show()
# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.backends.backend_agg.FigureCanvasAgg`
# - `matplotlib.figure.Figure`
# - `matplotlib.figure.Figure.add_subplot`
# - `matplotlib.figure.Figure.savefig` / `matplotlib.pyplot.savefig`
# - `matplotlib.axes.Axes.plot` / `matplotlib.pyplot.plot`