Graphical displays with Python and Pygame

The dataFile class

I wrote the dataFile class to help deal with comma-separated numeric text files. Once you initialize the class, you can use the loadFile function to read the file, providing the file name and the character used as a separator. Next, it loads all of the columns, each into its own list. After that, minor data processing is performed to remove end-of-line characters.

Once the file is loaded getColumn retrieves the requested column. Optionally, you can provide a data type to return, if you don't want the default string. No checking is performed to see whether the value can actually be returned in the type requested.

The debug function printTable prints the first 25 rows of the data table to the terminal, although it could be modified to display the graphed data instead. Starting at line 119, all of the classes are initialized, and functions are called to generate the graph shown in Figure 11.

Figure 11: The line graph from lineGraph.py.

Lines 119 and 120 create the dataFile class and loads in the data. Lines 122 and 123 are the Pygame initialization that I've used in the other examples. Line 125 initializes the graph class and provides the screen variable, and chart.setupGraph() on line 126 draws the graph framework (axes, grid, etc.).

I'm using data from a temperature and humidity logger at my office. The file it exports has the date in one column and the time in a second column. Two more columns include the temperature and humidity.

Lines 128-132 combine the two columns into a single string by using the getColumn function of the dataFile class. Then, each label is added to a labels list. Line 134 draws the chart across the entire range of 8,000 data points.

The graphLine function also creates minimum and maximum data values. Once that is complete, line 135 can use the extents to calculate and draw the scale on the graph. When I flip the display (line 136), the graph displays!

Conclusion

In this article, I have explored most of the drawing and image functions of Pygame. Each example was written specifically to emphasize the graphical aspects being discussed. In working with these examples, you might find that writing a game of checkers, for example, actually works better with a different program flow.

I hope that the examples I have shown here have provided you with a starting point for your own programs, so you can explore and determine the best flow for your specific application. The examples given here should help get you started creating your own graphical displays.

Additionally, because Python and Pygame are available on all of the major platforms, anything you develop is portable, whether you develop in Python, or create a standalone shell script to cover your needs.

Have fun, and start designing your graphical world!

Infos

  1. Pygame: http://www.pygame.org/news.html
  2. Image courtesy of ddpavumba/FreeDigitalPhotos.net
  3. Image courtesy of jannoon028/FreeDigitalPhotos.net
  4. Image courtesy of Stuart Miles/FreeDigitalPhotos.net

Der Autor

Scott Sumner is the Producer/Educator at the Mayborn Science Theater. He uses Python and Pygame to develop exhibits and visualizations. When he's not busy working in the theater, he can be found playing with Lionel trains or fiddling with electronics of one sort or another.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content