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?
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.
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.
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.
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 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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With