## Tuesday, September 6, 2016

### Tips for Matplotlib Users

Matplotlib is the plotting library that is commonly used with Python. However the documentation for matplotlib, though extensive is quite difficult to understand and navigate. Many of the most common things one might do when plotting are either not well explained or buried deep in the documentation. Here are some tips for the common things that I tend to do. More will be added as time goes by.

1. Saving a matplotlib plot as a PDF file:

import pylab as plt
a = [1,2,3,4]
b = [2,4,6,8]
plt.plot (a, b)
plt.savefig ('myplot.pdf')

2. Setting the x and y axes labels and the main title:

import pylab as plt
a = [1,2,3,4]; b = [2,4,6,8]
plt.plot (a, b)
plt.xlabel ('Time', fontsize=16)
plt.ylabel ('Variable')
plt.title ('My Experiment')
plt.show()

3. Set x and y axes limits

import pylab as plt
a = [1,2,3,4]; b = [2,4,6,8]
plt.plot (a, b)
plt.xlim ((0, 2))
plt.ylim ((-5, 5))
plt.show()

4. Set the physical size of the plot to 8 inches by 6 inches

import pylab as plt
a = [1,2,3,4]; b = [2,4,6,8]
plt.figure(figsize=(8,6))
plt.plot (a, b)
plt.show()

5. Setting the color and width of plotted lines

import pylab as plt
a = [1,2,3,4]; b = [2,4,6,8]
plt.figure(figsize=(8,6))
plt.plot(a, b, color="blue", linewidth=2.5)
plt.show()

7. Set the x and y axis labels and graph title

import pylab as plt
a = [1,2,3,4]; b = [2,4,6,8]
plt.plot(a, b, color="blue", linewidth=2.5)
plt.show()