The python module matplotlib is a very widely used package. It is based on the plotting in the commercial program Matlab. There are several way to import and use matplotlib. We will use what is called the object-oriented approach. You can see in the cell below how the module is imported in this approach.
The line %matplotlib inline tells jupyter to display the plots in this web browser page.
Run the cell below to read in the numpy and matplotlib modules and set up the inline plots.
To get you started I put in some code to create some data.
import numpy as np
import matplotlib.pyplot as plt
# Now make some data
np.random.seed(20150108) # seed the random number generator so eveyone has the same data
time = np.linspace(0, 2, 101)
volts = 2.5 * np.sin(2 * np.pi * time + 1.0) + np.random.normal(0,0.1,len(time))
vModel = 2.5 * np.sin(2 * np.pi * time + 1.0)
%matplotlibinlineimportnumpyasnpimportmatplotlib.pyplotasplt## Now make some datanp.random.seed(20150108)# seed the random number generator so eveyone has the same datatime=np.linspace(0,2,101)volts=2.5*np.sin(2*np.pi*time+1.0)+np.random.normal(0,0.1,len(time))vModel=2.5*np.sin(2*np.pi*time+1.0)
Quick and Dirty Plots
I hesitate even showing you how to do this, but quick and dirty plots can be made with the one line of code:
Go ahead and run this in the cell below.
[<matplotlib.lines.Line2D at 0x7f5e9f112a30>]
To make a minimally satisfactory plot, you need axis labels, and to change the plot from a line to points. These and many other plot features are easier to do with an object oriented approach. Object oriented means you create an object, then call that object's methods to modify it.
First you need a figure. A figure is the canvas or space onto which the plot is drawn. The basic call to create a figure is
fig = plt.figure()
Run by itself, you will not see anything happen.
Second, you need axes in the figure. The easiest way to do this is running
ax = fig.add_subplot(111)
You can see that fig has a method add_subplot. sub_plot can configure multiple plots in one figure. The odd argument 111 means one row of figures, one column of figures, and these are the axes for the first figure.
Finally, you need your plot. This takes the form
pl1 = ax.plot(time, volts)
Note that each time you create an object, you save a reference to it by the = operator.
One problem with the above plot is that since the data is not smooth, it should be plotted with dots or symbols. Data should almost always be plotted with dots or symbols. matplotlib, like Matlab, dies this with a formatting parameterin the plot command. You can append and optional third parameter after the y array with formatting information about the plot line. This formatting is a very compact string with the line type, the symbol, and the color encoded in a three character string. Here are some examples:
'-k' - plots only a black line. (k means black.)
'.r' - plots only dots in the color red
'--g' - plots only a green dashed line
'ob' - plots only circles in blue
'^:m' - plots triangle connected by a dotted line in magenta
help(plt.plot) is always available with more options and details
Other colors are y and c. Other symbols are v, *, +, x, 's', and d. Another line types is '-.'. Your plot should have a line like
ax.plot(time, volts, '.b')
and another line to plot model as a solid red line.
Adding More Lines to a Plot
Adding another line (or set of symbols) to the plot is just a matter of adding another ax.plot call. Each sucessive call of plot will add another line to the axis.
Copy and paste your previous code in the cell below. Then modify it to plot the data points in blue dots without a line, and the model vModel as a red smooth line.
Saving plots as graphics is a simple call to the method
where the argument dpi=dotsPerInch is optional. The type of file produced is determined by the extension of the file name. Here are the file types I suggest:
.png with dpi=200 - this makes a nice large file you can include in any document. The quality comes out better than if you use a .jpg file.
.svg with not dpi - this is a vector image file. Everything is describes by coordinates and sizes. This file scale to differnt sizes very well. This is a good choice if you are making a poster with large figures.
.pdf with dpi=200 - sometimes this format works better with LaTeX documents.
I usually use .png format files. In the cell below save the previous pair of plots in this format.
BTW, jupyter remembers the previous plot, so you can just run the savefig code.
---------------------------------------------------------------------------NameError Traceback (most recent call last)
<ipython-input-12-f890cc8ad872> in <module>----> 1fig.savefig(Yay2.csv,dpi=dotsPerInch)NameError: name 'Yay2' is not defined
** Double Click Here to edit this hidden cell **
Here is your figure in this web page. Edit the name to match your file name. Then execute this cell by typing <shift><enter>.
Histograms are a powerful way of graphing data that has randomness around some value. It shows how the data are randomly distributed. The x-axis are the data values, and the y-axis is a bar graph of how many times each value occurs.
The code below simulates measuring a lenth of 5 cm ten thousand times with an uncertainty of 0.1 cm.
You might note that this distribution only kind of looks like the normal (or Gaussian) distribrution you have seen before.
Number of Bins
The most common modification to the default hostogram plot is to change the bins on the x-axis. One way is to add more bins by adding a second argument rifht after your data that is an integer setting the number of bins.
Replot the above data with 100 bins. When you do, you should see more small tails in the histogram and it will look more like the classical normal distribution.
Finally, there is an optional argument log=True that make the y-axis logarithmic. In the cell below, plot the above histogram with the log scale turned on.
In [ ]:
Finally you can provide a list of bin boundaries as an array. The following example simulates the rolling of a pair of dice. You know there only 11 possible values from "snake eyes" or 2 to "boxcars" or 12. The default hist doesn't get his right without helping it out. This examples feeds it the histogram boundries.
Finally, this example demonstrates the normed keyword that normalized the numbers so, like a probability density, the sum adds up to 1.0.
In [ ]:
Other Miscellaneous Topics
Here are some other topic you can explore on your own. I go to a web browser and google matplotlib whatever to find things out or refresh my memory.
More Plot Options
lw - line width
ms - marker size
mfc - marker face color
mec - marker edge color
mew - marker edge width
names - 'red', etc. But also X11 colors like 'DodgeBlue'
hex - '#ff8022' in rrggbb format
RGB tuples - like color=(0,0,1), rgb values range from 0 to 1
RGBA tuple - like color=(0,1,.5,.3) where A is transparency