Object Oriented Programming and Matplotlib Plotting

This notebook will help you gain better concepts of:

  • Object Oriented Programming by providing a good example of practical application of OOP
  • How to draw stuff using Matplotlib

Everything in Python is an object. Matplotlib is the primary plotting library of Python and it is no exception from the object oriented nature of Python. Everything you see in a Matplotlib plot is an object. One particularly interesting object is matplotlib.patches. We will now discuss this class before we use it in our work.

matplotlib.patches is a collection of classes that make objects that can be drawn as patches on an axis. These include Circles, Polygons, Rectangles, etc. There is a base class called matplotlib.patches.Patch from which all these other classes (circles, polgons, rectangles, etc.) are derived. The matplotlib.patches.Patch is itself derived from a class called matplotlib.artist.Artist. There are a number of classes, other than matplotlib.patches.Patch, that are Artists. These include Lines, Text on plots, etc. The complete hierarchy is shown in the figure below. Class Hierarchy of Artist

The primary mode of display in matplotlib is the figure. A figure can have one or more axes on it which can display stuff in them. Interestingly enough, axes (and figures too!) in matplotlib are also objects which can draw any object of the class matplotlib.artist.Artist which is itself derived from object -- the base class of every object in Python. You can learn more about matplotlib.axes. An artist a is added to an axes object in a figure using the axes' add_artist(a) function.

Thought Assignment

What use of objects and classes do you see here? How can OOP reduce the amount of code and the complexity of the code?

Can you identify why this particular hierarchy will be useful in comparison to doing it through functional programming?


Axes and Axis

Note that matplotlib.axes is different from matplotlib.axis which is, actually, a subclass of matplotlib.artist.Artist and is used to draw the ticks of x and y axis on the axes. To complicate the naming furthere, there is a function of matplotlib.pyplot called axis which is used to set the limits of the matplotlib.axis in a figure. For example, plt.axis([0,1,0,1]) will set the ranges of both the x and y axis from 0 to 1. However, this will have no effect on the plot as we also need to scale the plot to conform to these ranges. This is achieved by executing plt.axis('image') prior to setting the range.

Coding Example

In [7]:
from matplotlib import pyplot as plt
from matplotlib.patches import Polygon

# list of points as tuples making up the Polygon
points = [(0,0),(0.25,0.75),(0.5, 1), (1,0)] 
# Creating a Polygon
myPolygon = Polygon (points,closed = True, color = 'r', fill = True) 
# Making a Figure
plt.figure(0) # 0 is the figue id, it is set automatically if not specified
# Setting the axis right
plt.axis('image') # Now the axis will scale to fit our range automatically
plt.axis([0,1,0,1]) # Now we set the range
plt.grid() # Let's add a grid pattern
# Let's get the current Axes object
ax = plt.gca()
# Now we can add our Polygon to the axex
ax.add_artist(myPolygon)
# Display
plt.show()
print myPolygon
Poly((0, 0) ...)

We get our polygon drawn and printed. The print statement for an object makes a call to a function implementing the printing behavior of the object. This function is called __str__ and it returns a string. In this specific case, the string returned is the word 'Poly' followed by the initial point of the polygon. We can make our own implementation of __str__. An alternate function which gives an unambiguous representation of the object is __repr__. Check this post on Stack Exchange to find the difference between them.

Just to clarify the description of Axes and axis earlier, note that the grid, the ticks, their text and lines are due to the axis. These are drawn just like our polygon on the Axes which is the display area on a Figure.

You can get help on any of these objects if you choose to understand the concepts further.

Assignment

Can you modify the code for making stars to use Polygons?