Skip to content

Consolidate agg-buffer examples. #11726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ per-file-ignores =
examples/lines_bars_and_markers/joinstyle.py: E402
examples/lines_bars_and_markers/scatter_piecharts.py: E402
examples/lines_bars_and_markers/span_regions.py: E402
examples/misc/agg_oo_sgskip.py: E402
examples/misc/agg_buffer.py: E402
examples/misc/anchored_artists.py: E501
examples/misc/contour_manual.py: E501
examples/misc/font_indexing.py: E501
Expand Down Expand Up @@ -263,6 +263,7 @@ per-file-ignores =
examples/text_labels_and_annotations/tex_demo.py: E402
examples/text_labels_and_annotations/watermark_text.py: E402
examples/ticks_and_spines/auto_ticks.py: E501
examples/user_interfaces/canvasagg.py: E402
examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py: E402
examples/user_interfaces/embedding_in_gtk3_sgskip.py: E402
examples/user_interfaces/embedding_in_qt_sgskip.py: E402
Expand Down
2 changes: 1 addition & 1 deletion doc/faq/howto_faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ For more on configuring your backend, see

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

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

Expand Down
27 changes: 8 additions & 19 deletions examples/misc/agg_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,23 @@

import numpy as np

import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg


try:
from PIL import Image
except ImportError:
raise SystemExit("Pillow must be installed to run this example")
import matplotlib.pyplot as plt

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

canvas = plt.get_current_fig_manager().canvas

agg = canvas.switch_backends(FigureCanvasAgg)
agg.draw()
s = agg.tostring_rgb()

# get the width and the height to resize the matrix
l, b, w, h = agg.figure.bbox.bounds
w, h = int(w), int(h)
s, (width, height) = agg.print_to_buffer()

X = np.fromstring(s, np.uint8).reshape((h, w, 3))
# Convert to a NumPy array.
X = np.fromstring(s, np.uint8).reshape((height, width, 4))

try:
im = Image.fromstring("RGB", (w, h), s)
except Exception:
im = Image.frombytes("RGB", (w, h), s)
# Pass off to PIL.
from PIL import Image
im = Image.frombytes("RGBA", (width, height), s)

# Uncomment this line to display the image using ImageMagick's
# `display` tool.
# Uncomment this line to display the image using ImageMagick's `display` tool.
# im.show()
45 changes: 0 additions & 45 deletions examples/misc/agg_oo_sgskip.py

This file was deleted.

64 changes: 0 additions & 64 deletions examples/misc/webapp_demo_sgskip.py

This file was deleted.

69 changes: 69 additions & 0 deletions examples/user_interfaces/canvasagg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
==============
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 string, which can in turn be passed off to PIL or
put in a numpy array. The latter functionality allows e.g. to use Matplotlib
inside a cgi-script *without* needing to write a figure to disk.
"""

from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
import numpy as np

fig = Figure(figsize=(5, 4), dpi=100)
# A canvas must be manually attached to the figure (pyplot would automatically
# do it). This is done by instantiating the canvas with the figure as
# argument.
canvas = FigureCanvasAgg(fig)

# Do some plotting.
ax = fig.add_subplot(111)
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: Save the figure to a string.
canvas.draw()
s, (width, height) = canvas.print_to_buffer()

# Option 2a: Convert to a NumPy array.
X = np.fromstring(s, np.uint8).reshape((height, width, 4))

# Option 2b: Pass off to PIL.
from PIL import Image
im = Image.frombytes("RGBA", (width, height), s)

# Uncomment this line to display the image using ImageMagick's `display` tool.
# im.show()

#############################################################################
#
# ------------
#
# References
# """"""""""
#
# The use of the following functions, methods, classes and modules is shown
# in this example:

import matplotlib
matplotlib.backends.backend_agg.FigureCanvasAgg
matplotlib.figure.Figure
matplotlib.figure.Figure.add_subplot
matplotlib.figure.Figure.savefig
matplotlib.axes.Axes.plot
60 changes: 0 additions & 60 deletions examples/user_interfaces/histogram_demo_canvasagg_sgskip.py

This file was deleted.