Integrating a Plot in a Pyglet Application

Learn via video courses
Topics Covered

Overview

Python is a very dynamic programming language, as it can integrate with our computer hardware effectively. With Python, we can handle our drives, save and load files from discs, and do much more.

A lot of libraries are used to utilize the graphic hardware of our computers. For example, the Pyglet library is very popular in Python. In this article, we will learn the basics of the Pyglet library in Python, along with some ways to integrate Matplotlib in our Pyglet applications.

Introduction

As we discussed earlier in this article, Python is home to many different libraries for various functions like Machine Learning, Web Development, and Gaming. Gaming is one of the most popular domains when it comes to Python and many libraries are used to create games in Python; Pyglet and Pygame.

Pyglet and Pygame are comparable, however, Pyglet excels above Pygame in areas like functionality and object-oriented programming because Pyglet offers more of these capabilities.

In this article, we will discuss the Pyglet library in Python and integrate it with Matplotlib.

What is Pyglet?

A well-coded Python library called Pyglet allows you to use OpenGL on any platform. OpenGL is a cross-platform and cross-language API (Application Programming Interface) used for rendering two-dimensional and three-dimensional vector graphics in Python. So you can make the most of your computer's graphic hardware using Pyglet.

From attractive and fancy transition effects to 3-D graphics rendering, anything is possible using the Pyglet library in Python.

Integration of Matplotlib Plot in Pyglet Application

Matplotlib is a dynamic and multi-purpose library in Python that allows us to create meaningful and descriptive plots and visualizations. Using Matplotlib and Pyglet, we can create dynamic visualizations and games that the user can manipulate according to his/her needs.

Since Pyglet doesn't have widgets like libraries such as Tkinter and wxWidgets, our code will render the image in the console and be viewed in full-screen mode.

Output:

Integration of Matplotlib Plot in Pyglet Application

Note: This window will be displayed in full-screen mode, which will take up the full surface of your working screen.

How does it work?

The FigureCanvasAgg object provided by Matplotlib (matplollib.backends.backend_agg specifically) has a constructor, that takes the figure as input and renders the result into a file. The print_raw() function puts the raw pixel data into our file. As part of the IO module, StringIO helps us to create an in-memory file that will be rendered.

After these processes, we finally use the Pyglet Image object to retrieve and create our image. Then, finally, we have to specify parameters like height, width and colour scheme of our image that will be rendered using Pyglet.

Code Examples

  • Using Pyglet and Matplotlib to render a snowflaked-polygon

    Just as we rendered a curve line on a canvas using Matplotlib and Pyglet, we will render a snowflaked polygon on a canvas.

Output:

FigureCanvasAgg

Note: Full-screen mode will display this window, which will take up the entire working screen.

Conclusion

  • In this article, we covered Pyglet; A well-written Python library that allows you to use OpenGL on any platform. You can make the most of your computer's graphic hardware using Pyglet (and OpenGL).
  • Apart from this, we learned ways to integrate Matplotlib with Pyglet; which allows us to design interactive games and visualizations that the user can alter to suit his or her needs.