The preferred way to set matplotlib figure/axes properties

Pyplot tutorial appears to recommend ax.set_xxx() functions, but also mentions .setp(xxx=).

On the other hand, .set(xxx=) function is not used and .setp(xxx=), while documented, is not used in any examples (Pyplot API).

I understand it that matplotlib supports both imperative programming style and matlab-like style. Reason being the target user bases -- those already familiar with Python and those who used Matlab before.

I conclude that matplotlib recommended api is .set_xxx().

A quick check through the gallery confirms this.

Edit: it appears there are examples of both in the gallery now.

Similar duality exists for keyword arguments to plot functions, except that imperative API is not that obvious.


I usually use the pyplot (interactive) interface for matplotlib, which exposes the setp function (which I use very often).

matplotlib supports the use of setp() (“set property”) and getp() to set and get object properties, as well as to do introspection on the object.

Which lets you do things just like your set function above, only you pass the object to set as a parameter:

>>> x = arange(0,1.0,0.01)
>>> y1 = sin(2*pi*x)
>>> y2 = sin(4*pi*x)
>>> lines = plot(x, y1, x, y2)
>>> setp(lines, linewidth=2, color='r')

I'm guessing the setp function actually calls the passed object's set function which works to locate and set the kwargs.

So my thoughts are, that despite the lack of documentation the object's set method is the idiomatic approach for setting multiple parameters via kwargs instead of line by line.

My 2 cents.


As a super-late followup to this question:

ax.set_xlim(4) and ax.set(xlim=4) are ultimately the same thing, so do what you like. ax.set(kwarg=foo) calls ax.set_kwarg(foo) via some string manipulation, as you can see if you look at the source. The various setter functions are mostly one-liners, as well, but exist to allow for the introspective setp to work.