Code 401 Class 14 Reading Notes
Matplotlib Tutorial
matplotlip is probably the single most used Python package for 2D-graphics. It provides both a very quick way to visualize data from Python and publication-quality figures in many formats.
IPython is an enhanced interactive Python shell that has lots of interesting features including named inputs and outputs, access to shell commands, improved debugging and much more.
pyplot provides a convenient interface to the matplotlib object-oriented plotting library. It is modeled closely after Matlab(TM). Therefore, the majority of plotting commands in pyplot have Matlab(TM) analogs with similar arguments.
Simple Plot
Matplotlib comes with default setting, you can control the defaults with almost every property: figure size and dpi, line width, color and style, axes, axis and grid properties, text and font properties.
Change colors and line widths:
plt.figure(figsize=(10,6), dpi=80)
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-")
Make space for data in order to clearly see all data points.
plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1, C.max()*1.1)
Set meaningful ticks to show values for sine and cosines
plt.xticks( [-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
plt.yticks([-1, 0, +1])
Set explicit tick labels
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
plt.yticks([-1, 0, +1],
[r'$-1$', r'$0$', r'$+1$'])
Placing spines at the middle of the data set
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
Add a legend
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine")
plt.legend(loc='upper left', frameon=False)
Annotate some points
t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t)], color ='blue', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.cos(t),], 50, color ='blue')
plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
xy=(t, np.sin(t)), xycoords='data',
xytext=(+10, +30), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.plot([t,t],[0,np.sin(t)], color ='red', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.sin(t),], 50, color ='red')
plt.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t, np.cos(t)), xycoords='data',
xytext=(-90, -50), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
Make tick labels larger
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(16)
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65 ))
Figures, Subplots, Axes, and Ticks
Figure: Is the windows in the GUI that has ‘Figure #’ as title. Figures are numbered starting from 1 as opposed to the normal Python way starting from 0.
set_something
method will allow you to set figure properties.
Subplots: you can arrange plots in regular grid. Number of rows and columns and the number of the plot need to be specified. GrisSpec is a great alternate resource to use.
Axes: Similar to subplots, but allow placement of plots at any location in the figure.
Tick Classes
- NullLocator: No ticks.
- IndexLocator: Place a tick on every multiple of some base number of points plotted.
- FixedLocator: Tick locations are fixed
- LinearLocator: Determine the tick locations.
- MultipleLocator: Set a tick on every integer that is multiple of some base.
- AutoLocator: Select no more than n intervals at nice locations.
- LogLocator: Determine the tick locations for log axes.
Animation
Quick References
Things I want to know more about
Excited to plot some data using one of the above examples!