| I'm telling you loosen up my buttons baby But you keep fronting Saying what you going do to me But I ain't seen nothing | |
| --The Pussycat Dolls, "Buttons" (2005) |
What are widgets? They are doohickeys, you know, thingamajiggers or whatchamacallits. Whatever they are, they are the visual tools by which a user can interact with your application. Humans intuitively interact with their environment through physical objects. It is perfectly natural for us to want to have knobs, buttons, and other contraptions to get things done. Up to this point, most of our interactivity has been through the keyboard or using default interactivity via the mouse. We have not added any knobs or buttons to our application in order to extend its functionality.
A word of warning before we proceed. This chapter will often exhibit feature creep. This will be necessary in order to demonstrate the features that Matplotlib provides; however, it does not mean that one should pack every single possible widget...
A design principle adopted early on by Matplotlib was one of interoperability. It shouldn't matter which GUI you are using; everything should just simply work. Therefore, Matplotlib provides a basic set of widgets that are entirely implemented using Matplotlib's interactivity framework. All of the built-in widgets are implemented using nothing more than what we have covered in this book so far. While these widgets may not be the most aesthetically pleasing ones, they will work in any interactive environment that you use for regular plotting.
Conceptually, many widgets emit specialized events that can have callbacks attached by developers. However, the mechanism to attach these callbacks is much more direct than the more generalized event handling that we worked with back in Chapter 2, Using Events and Callbacks. This makes working with widgets accessible to inexperienced programmers who are yet to learn how to create a GUI application.
Indeed, given that the primary audience...
While Matplotlib likes to follow a "batteries included" philosophy for all things related to plotting, there are times when a desired feature just doesn't generalize well enough for it to be a standard feature. Alternatively, a feature may require other specialty packages that do not make sense to have as a dependency for Matplotlib. For these situations, there is a growing library of packages that provide additional interactive features on top of your standard Matplotlib application.
The mpldatacursor package (https://pypi.python.org/pypi/mpldatacursor) provides a number of useful interactive tools, primarily focusing on making it very easy to annotate a plot. To use this tool, create your plots as normal. Then you can add a data cursor to one or more Axes
object (and can optionally specify which Artist
objects the cursor is valid for). Then, click on something in your plot and an annotation will appear, describing what you clicked on.
Interactivity takes many forms. Widgets are visible elements that are primarily interacted with via the mouse. In this chapter, we covered all of Matplotlib's GUI-neutral widgets. Widgets such as sliders, checkboxes, and radio buttons were all integrated into our application's event framework. Doing so allowed for multiple ways to update the state of the application and for the state of the widgets to be updated as well. Widgets such as Lasso
and Button
were added as input mechanisms, providing the means to modify the data that the application displays. Other widgets were also covered but not included into our main application such as RectangleSelector
and SpanSelector
. A few ways of using these widgets were demonstrated, from selecting data to measuring distances. We even went over how to create a specialized Axes
subclass that operates seamlessly with the rest of Matplotlib for the purpose of extending the data display of the figure window.
Finally, we went over a variety of third...