import matplotlib.backends.backend_agg as agg import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() canvas = agg.FigureCanvasAgg(fig) circle = plt.Circle((0.5, 0.5), 0.3, color='red') ax.add_artist(circle) canvas.draw() # update the canvas image = np.frombuffer(canvas.tostring_rgb(), dtype='uint8').reshape(canvas.get_width_height()[::-1] + (3,)) plt.imshow(image) plt.show()
import matplotlib.backends.backend_agg as agg import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() canvas = agg.FigureCanvasAgg(fig) x = np.arange(0, 10) y = np.sin(x) line, = ax.plot(x, y) canvas.draw() image = np.frombuffer(canvas.tostring_rgb(), dtype='uint8').reshape(canvas.get_width_height()[::-1] + (3,)) plt.imshow(image) plt.show()This example creates a `FigureCanvasAgg` object `canvas` that draws a line plot using the `plot` function. The canvas is then updated with the `canvas.draw()` method and converted to a numpy array using `canvas.tostring_rgb()`. Finally, the image is displayed using `plt.imshow()`. Both examples use the `matplotlib` package library to plot and display figures.