In [88]:
import numpy as np
import scipy as sp
import sympy
# Pylab combines the pyplot functionality (for plotting) with the numpy
# functionality (for mathematics and for working with arrays) in a single namespace
# aims to provide a closer MATLAB feel (the easy way). Note that his approach
# should only be used when doing some interactive quick and dirty data inspection.
# DO NOT USE THIS FOR SCRIPTS
#from pylab import *
# the convienient Matplotib plotting interface pyplot (the tidy/right way)
# use this for building scripts. The examples here will all use pyplot.
import matplotlib.pyplot as plt
# for using the matplotlib API directly (the hard and verbose way)
# use this when building applications, and/or backends
import matplotlib as mpl
In [89]:
# activate pop up plots
#%matplotlib qt
# or change to inline plots
%matplotlib inline
In [90]:
# some sample data
x = np.arange(-10,10,0.1)
In [91]:
page_width_cm = 13
dpi = 200
inch = 2.54 # inch in cm
# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=12) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20) # axes labels
# If you don’t need LaTeX, don’t use it. It is slower to plot, and text
# looks just fine without. If you need it, e.g. for symbols, then use it.
#plt.rc('text', usetex=True) #<- P-E: Doesn't work on my Mac
In [92]:
# create a figure instance, note that figure size is given in inches!
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,6))
# set the big title (note aligment relative to figure)
fig.suptitle("suptitle 16, figure alignment", fontsize=16)
# actual plotting
ax.plot(x, x**2, label="label 12")
# set axes title (note aligment relative to axes)
ax.set_title("title 14, axes alignment", fontsize=14)
# axes labels
ax.set_xlabel('xlabel 12')
ax.set_ylabel(r'$y_{\alpha}$ 12', fontsize=8)
# legend
ax.legend(fontsize=12, loc="best")
# saving the figure in different formats
fig.savefig('figure-%03i.png' % dpi, dpi=dpi)
fig.savefig('figure.svg')
fig.savefig('figure.eps')
In [93]:
# following steps are only relevant when using figures as pop up windows (with %matplotlib qt)
# to update a figure with has been modified
fig.canvas.draw()
# show a figure
fig.show()
In [94]:
plt.plot(x,x**2)
#Write code to show grid in plot here
plt.grid()
In [95]:
plt.plot(x,x**2, label="label 12")
plt.legend(fontsize=12, loc="upper center")
Out[95]:
In [96]:
stride = max( int(len(x) / 10), 1)
plt.plot(x,x**2, 'o-', markevery=stride)
Out[96]:
In [97]:
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16,6))
ax[0].plot(x,x**2)
ax[1].plot(x,x**2)
Out[97]:
In [98]:
X, Y = np.meshgrid(x,x)
In [99]:
Z=X**2+Y**2
CS = plt.contour(X, Y, Z)
In [99]:
In [100]:
fig, ax1 = plt.subplots()
ax1.plot(x,x**2)
ax2 = ax1.twinx()
ax2.plot(x,x**4, 'r')
plt.show()
In [101]:
plt.plot(x,x**2)
plt.axvline(5)
plt.axhline(50)
Out[101]:
In [102]:
import datetime
dates = np.array([datetime.datetime.now() + datetime.timedelta(days=i) for i in xrange(24)])
fig, ax = plt.subplots(nrows=1, ncols=1)
ax.plot(dates,range(0,24))
fig.autofmt_xdate()
In [180]:
x=np.linspace(0,5*np.pi,1000)
y=np.sin(x)
yrand=y+np.random.normal(0,0.3,1000)
fig, ax = plt.subplots(nrows=1, ncols=1)
ax.plot(x,y,linewidth=4.0,linestyle='dashed',color='black',label="y = sin(x)")
ax.plot(x,yrand,'b.',markersize=5)
plt.ylim([-1.5,1.5])
n=10
cmap = mpl.cm.Accent
for i in range(1,n+1):
coef = np.polyfit(x, y, i)
polynomial = np.poly1d(coef)
yFit = polynomial(x)
labelFit="deg "+str(i-1)
ax.plot(x,yFit,linewidth=2.0,color=cmap(i / float(n)),label=labelFit)
plt.legend(fontsize=12, bbox_to_anchor=(1.02, 1), loc=2)
Out[180]:
In [ ]: