Skip to content

Commit 0887e2a

Browse files
authored
Merge pull request #11726 from anntzer/canvasagg-examples
Consolidate agg-buffer examples.
2 parents cb47f2d + 870f851 commit 0887e2a

File tree

7 files changed

+80
-190
lines changed

7 files changed

+80
-190
lines changed

.flake8

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ per-file-ignores =
170170
examples/lines_bars_and_markers/joinstyle.py: E402
171171
examples/lines_bars_and_markers/scatter_piecharts.py: E402
172172
examples/lines_bars_and_markers/span_regions.py: E402
173-
examples/misc/agg_oo_sgskip.py: E402
173+
examples/misc/agg_buffer.py: E402
174174
examples/misc/anchored_artists.py: E501
175175
examples/misc/contour_manual.py: E501
176176
examples/misc/font_indexing.py: E501
@@ -263,6 +263,7 @@ per-file-ignores =
263263
examples/text_labels_and_annotations/tex_demo.py: E402
264264
examples/text_labels_and_annotations/watermark_text.py: E402
265265
examples/ticks_and_spines/auto_ticks.py: E501
266+
examples/user_interfaces/canvasagg.py: E402
266267
examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py: E402
267268
examples/user_interfaces/embedding_in_gtk3_sgskip.py: E402
268269
examples/user_interfaces/embedding_in_qt_sgskip.py: E402

doc/faq/howto_faq.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ For more on configuring your backend, see
662662

663663
Alternatively, you can avoid pylab/pyplot altogether, which will give
664664
you a little more control, by calling the API directly as shown in
665-
:doc:`/gallery/misc/agg_oo_sgskip`.
665+
:doc:`/gallery/user_interfaces/canvasagg`.
666666

667667
You can either generate hardcopy on the filesystem by calling savefig::
668668

examples/misc/agg_buffer.py

+8-19
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,23 @@
99

1010
import numpy as np
1111

12-
import matplotlib.pyplot as plt
1312
from matplotlib.backends.backend_agg import FigureCanvasAgg
14-
15-
16-
try:
17-
from PIL import Image
18-
except ImportError:
19-
raise SystemExit("Pillow must be installed to run this example")
13+
import matplotlib.pyplot as plt
2014

2115
plt.plot([1, 2, 3])
2216

2317
canvas = plt.get_current_fig_manager().canvas
2418

2519
agg = canvas.switch_backends(FigureCanvasAgg)
2620
agg.draw()
27-
s = agg.tostring_rgb()
28-
29-
# get the width and the height to resize the matrix
30-
l, b, w, h = agg.figure.bbox.bounds
31-
w, h = int(w), int(h)
21+
s, (width, height) = agg.print_to_buffer()
3222

33-
X = np.fromstring(s, np.uint8).reshape((h, w, 3))
23+
# Convert to a NumPy array.
24+
X = np.fromstring(s, np.uint8).reshape((height, width, 4))
3425

35-
try:
36-
im = Image.fromstring("RGB", (w, h), s)
37-
except Exception:
38-
im = Image.frombytes("RGB", (w, h), s)
26+
# Pass off to PIL.
27+
from PIL import Image
28+
im = Image.frombytes("RGBA", (width, height), s)
3929

40-
# Uncomment this line to display the image using ImageMagick's
41-
# `display` tool.
30+
# Uncomment this line to display the image using ImageMagick's `display` tool.
4231
# im.show()

examples/misc/agg_oo_sgskip.py

-45
This file was deleted.

examples/misc/webapp_demo_sgskip.py

-64
This file was deleted.

examples/user_interfaces/canvasagg.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
==============
3+
CanvasAgg demo
4+
==============
5+
6+
This example shows how to use the agg backend directly to create images, which
7+
may be of use to web application developers who want full control over their
8+
code without using the pyplot interface to manage figures, figure closing etc.
9+
10+
.. note::
11+
12+
It is not necessary to avoid using the pyplot interface in order to
13+
create figures without a graphical front-end - simply setting
14+
the backend to "Agg" would be sufficient.
15+
16+
In this example, we show how to save the contents of the agg canvas to a file,
17+
and how to extract them to a string, which can in turn be passed off to PIL or
18+
put in a numpy array. The latter functionality allows e.g. to use Matplotlib
19+
inside a cgi-script *without* needing to write a figure to disk.
20+
"""
21+
22+
from matplotlib.backends.backend_agg import FigureCanvasAgg
23+
from matplotlib.figure import Figure
24+
import numpy as np
25+
26+
fig = Figure(figsize=(5, 4), dpi=100)
27+
# A canvas must be manually attached to the figure (pyplot would automatically
28+
# do it). This is done by instantiating the canvas with the figure as
29+
# argument.
30+
canvas = FigureCanvasAgg(fig)
31+
32+
# Do some plotting.
33+
ax = fig.add_subplot(111)
34+
ax.plot([1, 2, 3])
35+
36+
# Option 1: Save the figure to a file; can also be a file-like object (BytesIO,
37+
# etc.).
38+
fig.savefig("test.png")
39+
40+
# Option 2: Save the figure to a string.
41+
canvas.draw()
42+
s, (width, height) = canvas.print_to_buffer()
43+
44+
# Option 2a: Convert to a NumPy array.
45+
X = np.fromstring(s, np.uint8).reshape((height, width, 4))
46+
47+
# Option 2b: Pass off to PIL.
48+
from PIL import Image
49+
im = Image.frombytes("RGBA", (width, height), s)
50+
51+
# Uncomment this line to display the image using ImageMagick's `display` tool.
52+
# im.show()
53+
54+
#############################################################################
55+
#
56+
# ------------
57+
#
58+
# References
59+
# """"""""""
60+
#
61+
# The use of the following functions, methods, classes and modules is shown
62+
# in this example:
63+
64+
import matplotlib
65+
matplotlib.backends.backend_agg.FigureCanvasAgg
66+
matplotlib.figure.Figure
67+
matplotlib.figure.Figure.add_subplot
68+
matplotlib.figure.Figure.savefig
69+
matplotlib.axes.Axes.plot

examples/user_interfaces/histogram_demo_canvasagg_sgskip.py

-60
This file was deleted.

0 commit comments

Comments
 (0)