As you start plotting and generating visual output, you'll find that you're using more and more of the "helper" functions, functions that don't necessarily plot the data, rather control the graph behavior and arrange labels just the way you want them.
So far we've used two methods to modify a plot behavior. One was using dedicated functions such as axis(), xlim(), and ylim() to control the plot ranges. The other method you've seen was passing arguments to functions, for example, the rotation argument in the text() function.
A third method is available, one that makes use of the object-oriented design of matplot-lib. It involves two functions, setp() and getp(), which retrieve and set a matplotlib object's parameters. The benefit of using setp() and getp() is that automation is easily achieved.
Up to this point we've suppressed the output from matplotlib so that the interactive scripts are easier to follow. We now turn to looking at those outputs. Whenever you issue a plot() command, matplotlib returns a list of matplotlib objects. This is important; the return value from calling plot() is a list of objects, not the matplotlib object itself, even if you only have one line to plot.
>>> from pylab import * >>> p = plot(arange(5)) »> type(p) <type 'list'> »> type(p[o])
The function setp(matobj) prints a list of properties you can set for matobj, where matobj is a matplotlib object. The function accepts either a list of matplotlib objects or just one object:
If you're not sure of what values a parameter can take, issue setp(p, 'param'):
>>> setp(p[o], 'visible') visible: [True | False]
So to hide the plot, you could issue
or to set the label associated with a line, issue
>>> setp(p[o], label='Linel') >>> legend()
The function setp() also accepts lists of matplotlib objects, in which case all the matplot-lib objects in the list will be set.
■ Note To query acceptable parameters, enclose the parameter to be queried in quotes: setp(p, 'linewidth'). To set a parameter value, do not include the quotes, but do use an assignment: setp(p, linewidth=4).
Similarly, to retrieve values, use the getp() function. The function getp() is a little less forgiving in that it requires one matplotlib object, not a list of objects.
Was this article helpful?