Time for action – using Matplotlib in Pygame
In this recipe we will take the position coordinates of the previous tutorial and make a graph from them. Perform the following steps to do so:
Using a noninteractive backend: In order to integrate Matplotlib with Pygame we need to use a noninteractive backend, otherwise Matplotlib will present us with a GUI window by default. We will import the main Matplotlib module and call the
use
function. This function has to be called immediately after importing the main Matplotlib module and before other Matplotlib modules are imported.import matplotlib as mpl mpl.use("Agg")
Noninteractive plots can be drawn on a Matplotlib canvas. Creating this canvas requires imports, creating a figure and a subplot. We will specify the figure to be 3 x 3 inches large. More details can be found at the end of this section.
import matplotlib.pyplot as plt import matplotlib.backends.backend_agg as agg fig = plt.figure(figsize=[3, 3]) ax = fig.add_subplot(111) canvas...