In the last two sections we’ve looked at what the Traits package does, and customizing the interface. Today we’ll look at how to do some simple data plots with the Chaco package included with the Enthought distribution.
(Some of you may be wondering whether to use Chaco or Matplotlib for plotting data in python. In my experience, matplotlib is easier to use, but only generate static plots; Chaco is harder to learn, but both more powerful and allows you to interact with the data. As an example, in Chaco you can plot the graph, do live filtering, then drag a selection over some points and get statistics on it; this is not possible for a matplotlib plot.)
Recall us working on an interface for entering participant data for a Age-Happiness study. Now let us revise the premise, and make this a longitudinal study – that is, instead of measuring bob‘s happiness at one point in time, we track his happiness over many years. This requires us to modify our Subject object, so that age and happiness are both Arrays instead of Int or Floats (click on the image to see a larger version):
Line 1,4 are new – they sets up the plotting we’re going to use shortly.
Line 7,8,9 – note that we’ve added in a desc argument. This keyword supplies a description, which is displayed when the user hovers the mouse over the interface.
Line 11, 12 – here we changed up the original age and happiness to the (traited) Arrays. We further specify that these are particular types of arrays that can only contain Int and Floats.
Line 20 – we supply the LiveButtons list to the buttons attribute of View. LiveButtons is a predefined List which gives the “undo/revert/…” set of buttons at the bottom of the interface, which comes in handy when you’ve made a mistake in data entry…
What we see now is the numerical values of age and happiness, when what we really want to see is a graph of how bob‘s happiness trends over age. For a basic plot, we can simply use the ChacoPlotItem object (which we’ve already imported):
In this example, we’ve generated bob‘s happiness from his age and some random noise. (We also discovered that Bob is actually Bobby Hull himself!) What is noteworthy is line 17: this denotes that we want a graph (specifically, of the type ChacoPlotItem), with age as its index (traits parlance for x-axis) and happiness as its value (y-axis).
ChacoPlotItem is a predefined traits object that creates a 2D plot. While it is deprecated, it serves its function as a first look at what Chaco plots can do: with a single line of code, we’ve generated a reasonable graph that supports some interaction. Note that if you drag on the plot, it pans; and if you press ‘z’, the cursor changes to a magnifying glass, which allows you to zoom in. Once zoomed in, ctrl-leftarrow/rightarrow brings you back and forth different zoom levels.
While ChacoPlotItem let us do plenty of customization (try adding a type=’scatter’ to the arguments), where the Chaco package really shines is when different bits are connected together. In the next installment we’ll look at recreating this plot with a live moving average filter, where you can adjust the window size of the filter and see the effect updated immediately in the graph.