How it works...

One of the crucial ideas to grasp with the object-oriented approach is that each plotting element has both getter and setter methods. The getter methods all begin with get_ and either retrieve a specific property or retrieve other plotting objects. For instance, ax.get_yscale() retrieves the type of scale that the y axis is plotted with as a string (default is linear), while ax.get_xticklabels() retrieves a list of matplotlib text objects that each have their own getter and setter methods. Setter methods modify a specific property or an entire group of objects. A lot of matplotlib boils down to latching onto a specific plotting element and then examining and modifying it via the getter and setter methods.

It might be useful to make an analogy of the matplotlib hierarchy as a home. The home and all of its contents would be the Figure. Each individual room would be the Axes and the contents of the room would be the artists.

The easiest way to begin using the object-oriented interface is with the pyplot module, which is commonly aliased plt, as done in step 1. Step 2 shows one of the most common methods to initiate the object-oriented approach. The plt.subplots function creates a single Figure, along with a grid of Axes objects. The first two parameters, nrows and ncols, define a uniform grid of Axes objects. For example, plt.subplots(2,4) creates eight total Axes objects of the same size inside one Figure.

The plt.subplots function is somewhat of an oddity in that it returns a two-item tuple. The first element is the Figure, and the second element is the Axes object. This tuple gets unpacked as two distinct variables, fig and ax. If you are not accustomed to tuple unpacking, it may help to see step 2 written like this:

>>> plot_objects = plt.subplots(nrows=1, ncols=1)
>>> type(plot_objects)
tuple

>>> fig = plot_objects[0]
>>> ax = plot_objects[1]

If you create more than one Axes with plt.subplots , then the second item in the tuple is a NumPy array containing all the Axes. Let's demonstrate that here:

>>> plot_objects = plt.subplots(2, 4)

The plot_objects variable is a tuple containing a Figure as its first element and a Numpy array as its second:

>>> plot_objects[1]
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x133b70a20>, <matplotlib.axes._subplots.AxesSubplot object at 0x135d6f9e8>, <matplotlib.axes._subplots.AxesSubplot object at 0x1310e4668>, <matplotlib.axes._subplots.AxesSubplot object at 0x133565ac8>], [<matplotlib.axes._subplots.AxesSubplot object at 0x133f67898>, <matplotlib.axes._subplots.AxesSubplot object at 0x1326d30b8>, <matplotlib.axes._subplots.AxesSubplot object at 0x1335d5eb8>, <matplotlib.axes._subplots.AxesSubplot object at 0x133f78f28>]], dtype=object)

Step 3 verifies that we indeed have Figure and Axes objects referenced by the appropriate variables. In step 4, we come across the first example of getter and setter methods. Matplotlib defaults all Figures to 6 inches in width by 4 inches in height, which is not the actual size of it on the screen, but would be the exact size if you saved the Figure to a file.

Step 5 shows that, in addition to the getter method, you can sometimes directly access another plotting object by its attribute. Often, there exist both an attribute and a getter method to retrieve the same object. For instance, look at these examples:

>>> fig.axes == fig.get_axes()
True

>>> ax.xaxis == ax.get_xaxis()
True

>>> ax.yaxis == ax.get_yaxis()
True

Many artists have a facecolor property that can be set to cover the entire surface one particular color, as in step 7. In step 8, the get_children method can be used to get a better understanding of the object hierarchy. A list of all the objects directly below the Axes is returned. It is possible to select all of the objects from this list and start using the setter methods to modify properties, but this isn't customary. We usually collect our objects directly from the attributes or getter methods.

Often, when retrieving a plotting object, they will be returned in a container like a list or a dictionary. This is what happens when collecting the spines in step 9. You will have to select the individual objects from their respective containers in order to use the getter or setter methods on them, as done in step 10. It is also common to use a for-loop to iterate through each of them one at a time.

Step 11 adds grid lines in a peculiar way. We would expect there to be a get_grid and set_grid method, but instead, there is just a grid method, which accepts a boolean as the first argument to turn on/off the grid lines. Each axis has both major and minor ticks, though by default the minor ticks are turned off. The which parameter is used to select which type of tick has a grid line.

Notice that the first three lines of step 11 select the xaxis attribute and call methods from it, while the last three lines call equivalent methods directly from the Axes object itself. This second set of methods is a convenience provided by matplotlib to save a few keystrokes. Normally, most objects can only set their own properties, not those of their children. Many of the axis-level properties are not able to be set from the Axes, but in this step, some are. Either method is acceptable.

When adding the grid lines with the first line in step 11, we set the properties linewidth, color, and linestyle. These are all properties of a matplotlib line, formally a Line2D object. You can view all of the available properties here: http://bit.ly/2kE6MiG. The set_ticks method accepts a sequence of floats and draws tick marks for only those locations. Using an empty list will completely remove all ticks.

Each axis may be labeled with some text, for which matplotlib formally uses a Text object. Only a few of all the available text properties (http://bit.ly/2yXIZfP) are changed. The set_yticklabels Axes method takes in a list of strings to use as the labels for each of the ticks. You may set any number of text properties along with it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.5.201