# composite multiple plots into one figure
# using subplot2grid function
import numpy as np
from matplotlib import pyplot as plt
% matplotlib inline
T = np.linspace(-np.pi, np.pi, 256)
grid_size = (4,2)
print(help(plt.subplot2grid)) # Get the docstring of the function subplot2grid
plt.subplot2grid(grid_size, (0,0), rowspan=2, colspan=1)
plt.plot(np.sin(2*T), np.cos(0.5 *T), c='k')
plt.subplot2grid(grid_size, (0,1), rowspan=3, colspan=1)
plt.plot(np.sin(2*T), np.cos(T), c='k')
plt.subplot2grid(grid_size, (2,0), rowspan=1, colspan=1)
plt.plot(np.sin(2*T), np.cos(3*T), c='k')
plt.subplot2grid(grid_size, (3,0), rowspan=1, colspan=2)
plt.plot(np.exp(-T), c='k')
plt.tight_layout() #to prevent plots overlapping
plt.show()
# add titles to the subfigures
import numpy as np
from matplotlib import pyplot as plt
% matplotlib inline
T = np.linspace(-np.pi, np.pi, 256)
grid_size = (4,2)
for i in range(1,5):
axes = plt.subplot2grid(grid_size, (i-1,0), rowspan=1, colspan=4)
axes.get_yaxis().set_visible(False) # remove the y axis ticks
plt.plot(np.sin(i * T), c='k')
plt.title('{}Hz'.format(i))
plt.tight_layout() #to prevent plots overlapping
plt.show()
# composite multiple plots into one figure
# using subplot function
import numpy as np
from matplotlib import pyplot as plt
% matplotlib inline
time = np.linspace(0,4,200)
fig, (ax0,ax1) = plt.subplots(ncols=2)
ax0.plot(time, np.sin(time), c='g')
ax1.plot(time, np.cos(time), c='r')
plt.show()
# To scale the axes we need the pyplot API and the Axes object
import numpy as np
from matplotlib import pyplot as plt
% matplotlib inline
time = np.linspace(0,4,200)
# print(help(plt.axes().set_aspect))
fig, (ax0,ax1) = plt.subplots(ncols=2)
ax0.plot(time, np.sin(time), c='g')
ax0.set_aspect('equal') #the actual ratio
ax1.plot(time, np.cos(time), c='r')
ax1.set_aspect('3') # the hight is three times the width
plt.show()
# Set the aspect ratio of a figure using figsize
import numpy as np
from matplotlib import pyplot as plt
% matplotlib inline
time = np.linspace(0,4,200)
plt.figure(figsize=(10.24,2.56))
plt.plot(time, np.sin(time), c='g')
plt.show()
# Set a limit on the axes
import numpy as np
from matplotlib import pyplot as plt
% matplotlib inline
time = np.linspace(0,4,200)
plt.plot(time, np.sin(time), c='g')
plt.ylim(0,0.5) # limit the y axis
plt.xlim(0,3.5) # limit the x axis
plt.show()
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
X = np.linspace(-6, 6, 1024)
Y = np.sinc(X)
X_detail = np.linspace(-3, 3, 1024)
Y_detail = np.sinc(X_detail)
plt.plot(X, Y, c = 'k')
# set the position of the zoomed up plot
#([bottom left corner (.57,.6), width (0.3), hight(0.2)])
sub_axes = plt.axes([.57, .6, .3, .2])
sub_axes.plot(X_detail, Y_detail, c = 'k')
# remove the Y axis labels
sub_axes.get_yaxis().set_visible(False)
# insert the zoomed up plot
# plt.setp(sub_axes)
plt.show()