Viewing your weather forecast without a browser
PDL::Graphics::Prima is a Perl plotting library written using PDL and the Prima GUI toolkit. It is targeted at PDL users with the hope of one day becoming the standard plotting library for PDL. PDL::Graphics::Prima provides a complete plotting widget, but one of the great aspects of the library is that it also provides a very simple interface for building one-off plots called PDL::Graphics::Prima::Simple. In this post, I use Mojolicious to pull down some weather data and plot it using PDL::Graphics::Prima. For the initial pass, the initial plotting code is just 1 line. :-)
Check out the full script at this gist. If you want to run this on your own machine, you should install PDL::Graphics::Prima on your machine (which will install PDL, which will take about 10 minutes). Then install Mojolicious or revise the script to use your user agent of choice. This is a complete but rather lengthy script, so for now turn your attention to lines 41-42, which reads thus:
Try running the script and give it your zip-code and see what it gives you. I particularly enjoy doing this for zip codes of people I know, and Beverly Hills, since I know that one off the top of my head, too.
Of course, it might be nice to add axis labels and such. There are two ways to do that. The first way is to get the window and plot object that are returned from line_plot in list context, modify the plot object, and execute the window:
Unlike the line_plot command in void context, this function call does not display the plot (and block the script) as soon as it is called. Instead it returns the plot object and the window that will display that plot which you can run later. Or, you can accumulate a collection of such windows and call Prima->run to view them all at once. That will kick off the Prima event loop, which is unfortunate since you will need to interrupt it with CTRL-C or use some other strategy in order to break out of it. There are other, better ways to handle this, but that discussion will have to wait for later. In the meantime, it's best to stick with one window, or do your plotting in the PDL shell.
Another approach is to specify all of this in a single plot() command:
The advantage of this approach is that it brings you much closer to the deeper PDL::Graphics::Prima widget interface, and thus to building full GUI data analysis applications. That will also have to wait for another post. :-)
Enjoy!
Leave a comment