Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The preferred way to set matplotlib figure/axes properties

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?

like image 803
jarondl Avatar asked Nov 15 '12 14:11

jarondl


People also ask

What are the methods to adjust axis limits Matplotlib?

By using xlim() and ylim() methods In matplotlib, to set or get X-axis and Y-axis limits, use xlim() and ylim() methods, accordingly. These methods define the limits for respective axes if arguments are passed, and if no arguments are passed, we obtain a range of the respective axes.

How to set the figure for axes in Matplotlib?

The Axes.set_figure () function in axes module of matplotlib library is used to set the Figure for this Axes. Syntax: Axes.set_figure (self, fig) Parameters: This method accepts only one parameter. fig : This parameter is the Figure instance.

What are the default settings of a Matplotlib plot?

You can control the defaults of almost every property in Matplotlib: figure size and DPI, line width, color and style, axes, axis and grid properties, text and font properties and so on.

What is Matplotlib in Python?

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

How to set x-axis values in Matplotlib?

How to Set X-Axis Values in Matplotlib You can use the following syntax to set the x-axis values for a plot in Matplotlib: #specify x-axis locations x_ticks = [2, 4, 6, 8, 10] #specify x-axis labels x_labels = ['A', 'B', 'C', 'D', 'E'] #add x-axis values to plot plt.xticks(ticks=x_ticks, labels=x_labels)


1 Answers

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.

like image 140
Dima Tisnek Avatar answered Oct 09 '22 08:10

Dima Tisnek