Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc76370

Browse files
authored
Merge pull request #14797 from tacaswell/doc_agg_buffer_example
DOC: create a Agg figure without pyplot in buffer example
2 parents 81b5944 + 719281b commit bc76370

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

examples/misc/agg_buffer_to_array.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@
77
"""
88
import matplotlib.pyplot as plt
99
import numpy as np
10+
from matplotlib.figure import Figure
11+
from matplotlib.backends.backend_agg import FigureCanvas
1012

11-
# make an agg figure
12-
fig, ax = plt.subplots()
13+
# Create a figure that pyplot does not know about.
14+
fig = Figure()
15+
# attach a non-interactive Agg canvas to the figure
16+
# (as a side-effect of the ``__init__``)
17+
canvas = FigureCanvas(fig)
18+
ax = fig.subplots()
1319
ax.plot([1, 2, 3])
1420
ax.set_title('a simple figure')
15-
fig.canvas.draw()
16-
21+
# Force a draw so we can grab the pixel buffer
22+
canvas.draw()
1723
# grab the pixel buffer and dump it into a numpy array
18-
X = np.array(fig.canvas.renderer.buffer_rgba())
24+
X = np.array(canvas.renderer.buffer_rgba())
1925

2026
# now display the array X as an Axes in a new figure
2127
fig2 = plt.figure()

0 commit comments

Comments
 (0)