Plotting with Matplotlib

So, I was looking for a good plotting library in Python. There are a number of choices – that’s the best part. So many options!

There is Chaco!

There is MathGL!

There is PyQwt!

There is PyX!

And then, there is Matplotlib. And, then I stopped looking. Before settling onto MPL, I read stuff on each of the libraries mentioned above. Chaco is a very good library but documentation is not its strongest point. MathGL suffers from the same. PyQwt does not have the biggest following and development going on. PyX was less recommended.

And Matplotlib – well – lets just say it was the overwhelming winner. Simply because, it has a voluminous documentation, lots of sample codes, lots of forums discussing it…and Scipy and Numpy tutorials use it for their plotting purposes! I even read somewhere that Scipy + Numpy + Matplotlib + Mayavi (a 3d plotting library) is a MATLAB-killer. Now, that is a huge thing to say, considering MATLAB is the leader as far as scientific computation is concerned.

I have an experiment in mind. It is not the most innovative one. But, it is relevant to what I propose to accomplish with Python. I will hook up a potentiometer to my Arduino, convert the analog signal to digital and transmit the data serially to my Python script which will (hopefully) plot the voltage at that instant. But, for that, I first need to know how to use Matplotlib. So, I did some studying and learnt how to generate data, plot data, manipulate a few properties, add text to the plots – and last, but definitely not the least – update the plots at run-time.

Here’s a simple code that will plot the quadratic equation “x^2 – 4*x + 1”, marks the critical points i.e. the minimum and the x-axis cutting points, annotates them, the title of the plot uses the super-scripted form of x^2 and some other small features.

The code is well-commented. So, I don’t think it should be a problem picking it up.

import numpy as np # standard library imports - nothing special here
import matplotlib.pyplot as plt # same as above

a = np.arange(-6,10,0.1) # create an array having the numbers -6,-5.9,-5.8,...,-0.1,0,0.1,...,.9.8,9.9,10

def evalf(arg): # define function evalf to evaluate f(x) at x=x
 return arg**2 - 4*arg + 1

b = evalf(a)

fig = plt.figure() # hold the matplotlib in a  variable
sp = fig.add_subplot(111) # add a subplot to the figure - 111 means 1 row, 1 column, 1st subplot 1 is current
sp.grid('True') # enable the grid
plt.xlabel('value of x -->') # labeling the x-axis
plt.ylabel('value of f(x) -->') # labeling the y-axis
plt.title(r'$f(x) = x^2 - 4x + 1$') # giving the title - accepts <a title="TeX wiki" href="http://en.wikipedia.org/wiki/TeX" target="_blank">TeX</a> also

myplot = sp.plot(a,b,'b') # hold the plot in a variable
myplot2 = sp.plot(2,evalf(2),'ro',3.732,evalf(3.732),'yo',0.268,evalf(0.268),'yo') # plotting critical points
sp.annotate('Minimum',(2,evalf(2)),xytext=(1.05,10),arrowprops=dict(facecolor='black', shrink=0.05))
sp.annotate('X-crossing 1',(0.268,evalf(0.268)),xytext=(-5,0),arrowprops=dict(facecolor='green', shrink=0.05))
sp.annotate('X-crossing 2',(3.732,evalf(3.732)),xytext=(6.5,0),arrowprops=dict(facecolor='green', shrink=0.05))

# annotation of all three points was achieved using the above three functions - text, point to be annotated, point for# text to be written and properties of the arrow mark used

plt.show() # after everything - just show the plot!

This is the result of this code-snippet.

Plot using Matplotlib
Please note the title and the annotations

The plot is not yet a real-time plot. It is a static plot with fixed X and Y data. To make it real-time, we have to constantly change its X and Y data in a loop and re-plot. I shall post about this after I am done with my activity.

2 thoughts on “Plotting with Matplotlib

Leave a comment