The preferred way to set matplotlib figure/axes properties

jarondl picture jarondl · Nov 15, 2012 · Viewed 12.7k times · Source

Say I have a matplotlib axes called ax, and I want to set several of its properties. Currently, I do it like this:

ax.set_yscale('log')
ax.set_xlim([0,10])
ax.set_xlabel('some label')

But it gets tedious after a while. Then I ran into this method:

ax.set(yscale='log', xlim=[0,10], xlabel='some label')

Much more concise, but it seems a bit undocumented. I mean all the documentation says is "A tkstyle set command, pass kwargs to set properties".

What is the preferred or idiomatic way? Is the set method api stable?

Answer

Dima Tisnek picture Dima Tisnek · Dec 3, 2012

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.