How to do it...

  1. To get started with matplotlib using the object-oriented approach, you will need to import the pyplot module and alias plt:
>>> import matplotlib.pyplot as plt
  1. Typically, when using the object-oriented approach, we will create a Figure and one or more Axes objects. Let's use the subplots function to create a Figure with a single Axes:
>>> fig, ax = plt.subplots(nrows=1, ncols=1)
  1. The subplots function returns a two-item tuple object containing the Figure and one or more Axes objects (here it is just one), which is unpacked into the variables fig and ax. From here on out, we will directly use these objects by calling methods in a normal object-oriented approach. Let's take a look at the type of each of these objects to ensure that we are actually working with a Figure and an Axes:
>>> type(fig)
matplotlib.figure.Figure

>>> type(ax)
matplotlib.axes._subplots.AxesSubplot
  1. Although you will be calling more Axes than Figure methods, you might still need to interact with them. Let's find the size of the Figure and then enlarge it:
>>> fig.get_size_inches()
array([ 6., 4.])

>>> fig.set_size_inches(14, 4)
>>> fig
  1. Before we start plotting, let's examine the matplotlib hierarchy. You can collect all the Axes of the Figure with the axes attribute:
>>> fig.axes
[<matplotlib.axes._subplots.AxesSubplot at 0x112705ba8>]
  1. This command returns a list of all the Axes objects. However, we already have our Axes object stored in the ax variable. Let's verify that they are actually the same object:
>>> fig.axes[0] is ax
True
  1. To help visibly differentiate the Figure from the Axes, we can give each one a unique facecolor. Matplotlib accepts a variety of different input types for color. Approximately 140 HTML colors are supported by their string name (see this list: http://bit.ly/2y52UtO). You may also use a string containing a float from zero to one to represent shades of gray:
>>> fig.set_facecolor('.9')
>>> ax.set_facecolor('.7')
>>> fig
  1. Now that we have differentiated between the Figure and the Axes, let's take a look at all of the immediate children of the Axes with the get_children method:
>>> ax_children = ax.get_children()
>>> ax_children
[<matplotlib.spines.Spine at 0x11145b358>, <matplotlib.spines.Spine at 0x11145b0f0>, <matplotlib.spines.Spine at 0x11145ae80>, <matplotlib.spines.Spine at 0x11145ac50>, <matplotlib.axis.XAxis at 0x11145aa90>, <matplotlib.axis.YAxis at 0x110fa8d30>, ...]
  1. Every basic plot has four spines and two axis objects. The spines represent the data boundaries and are the four physical lines that you see bordering the darker gray rectangle (the Axes). The x and y axis objects contain more plotting objects such as the ticks and their labels and the label of the entire axis. We can select the spines from this list, but that isn't generally how it's done. We can access them directly with the spines attribute:
>>> spines = ax.spines
>>> spines
OrderedDict([('left', <matplotlib.spines.Spine at 0x11279e320>), ('right', <matplotlib.spines.Spine at 0x11279e0b8>), ('bottom', <matplotlib.spines.Spine at 0x11279e048>), ('top', <matplotlib.spines.Spine at 0x1127eb5c0>)])
  1. The spines are contained in an ordered dictionary. Let's select the left spine and change its position and width so that it is more prominent and also make the bottom spine invisible:
>>> spine_left = spines['left']
>>> spine_left.set_position(('outward', -100))
>>> spine_left.set_linewidth(5)

>>> spine_bottom = spines['bottom']
>>> spine_bottom.set_visible(False)
>>> fig
  1. Now, let's focus on the axis objects. We can access each axis directly through the xaxis and yaxis attributes. Some axis properties are also available directly with the Axes object. In this step, we change some properties of each axis in both manners:
>>> ax.xaxis.grid(True, which='major', linewidth=2,
color='black', linestyle='--')
>>> ax.xaxis.set_ticks([.2, .4, .55, .93])
>>> ax.xaxis.set_label_text('X Axis', family='Verdana', fontsize=15)

>>> ax.set_ylabel('Y Axis', family='Calibri', fontsize=20)
>>> ax.set_yticks([.1, .9])
>>> ax.set_yticklabels(['point 1', 'point 9'], rotation=45)
>>> fig
..................Content has been hidden....................

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