In [1]:
# import
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
# generating some data points
X = np.linspace(-np.pi, np.pi, 20, endpoint=True)
C, S = np.cos(X), np.sin(X)
Changing Plot Size
In [3]:
plt.figure(figsize=(8,6))
l1,l2, = plt.plot(X, C, X, S)
plt.legend([l1, l2],["Cos Wave", "Sin Wave"], loc='best')
# adding Grid & Margins
plt.grid(True)
plt.margins(0.2) # 20%
Adding Subplot
subplot(nrow, ncol, plot_no [, axisbg='#476042'])
In [4]:
plt.figure(figsize=(8,6))
## Adding 2 subplots
ax1 = plt.subplot(2, 2, 1)
ax4 = plt.subplot(2, 2, 4, axisbg='#000123')
# plotting in subplot 1
l1,l2, = ax1.plot(X, C, X, S)
ax1.legend([l1, l2],["Cos Wave", "Sin Wave"], loc='best')
# adding Grid & Margins
plt.grid(True)
plt.margins(0.2) # 20%
In [5]:
plt.figure(figsize=(8,6))
## Adding 2 subplots
ax1 = plt.subplot(2, 2, 1)
ax4 = plt.subplot(2, 2, 4, axisbg='#87ceeb')
# plotting in subplot 1
l1,l2, = ax1.plot(X, C, X, S)
ax1.legend([l1, l2],["Cos Wave", "Sin Wave"], loc='best')
# plotting in subplot 4
l1, = ax4.plot(X, C, c='r')
ax4.legend([l1],["Cos Wave"], loc='best')
Out[5]:
Removing ticks
xticks() and set_xticks()
In [6]:
plt.figure(figsize=(8,6))
## Adding 2 subplots
ax1 = plt.subplot(2, 2, 1)
ax4 = plt.subplot(2, 2, 4, axisbg='#87ceeb')
# plotting in subplot 1
l1,l2, = ax1.plot(X, C, X, S)
ax1.legend([l1, l2],["Cos Wave", "Sin Wave"], loc='best')
ax1.set_xticks([]) # can use ax1.xticks([]) as well
ax1.set_yticks([])
# plotting in subplot 4
l1, = ax4.plot(X, C, c='r')
ax4.legend([l1],["Cos Wave"], loc='best')
Out[6]:
Adding all subplots
In [7]:
plt.figure(figsize=(8,6))
## Adding 2 subplots
ax1 = plt.subplot(2, 2, 1)
ax2 = plt.subplot(2, 2, 2)
ax3 = plt.subplot(2, 2, 3)
ax4 = plt.subplot(2, 2, 4, axisbg='#87ceeb')
# plotting in subplot 1
l1,l2, = ax1.plot(X, C, X, S)
ax1.legend([l1, l2],["Cos Wave", "Sin Wave"], loc='best')
ax1.set_xticks([]) # can use ax1.xticks([]) as well
ax1.set_yticks([])
# plotting in subplot 4
l1, = ax4.plot(X, C, c='r')
ax4.legend([l1],["Cos Wave"], loc='best')
Out[7]:
Adding properties in subplots
grid(), margins()
set_title(), set_xlabel(), set_ylabel(), set_xticks(), set_yticks()
In [8]:
plt.figure(figsize=(8,6))
## Adding 2 subplots
ax1 = plt.subplot(2, 2, 1)
ax2 = plt.subplot(2, 2, 2)
ax3 = plt.subplot(2, 2, 3)
ax4 = plt.subplot(2, 2, 4, axisbg='#87ceeb')
# plotting in subplot 1
l1,l2, = ax1.plot(X, C, X, S)
ax1.legend([l1, l2],["Cos Wave", "Sin Wave"], loc='best')
ax1.set_xticks([]) # can use ax1.xticks([]) as well
ax1.set_yticks([])
ax1.set_xlabel("X")
ax1.set_ylabel("Sin and Cos")
ax1.set_title("Waves")
ax1.grid(True)
ax1.margins(0.2)
# plotting in subplot 4
l1, = ax4.plot(X, C, c='r')
ax4.legend([l1],["Cos Wave"], loc='best')
Out[8]:
In [9]:
plt.figure(figsize=(12,9))
## Adding 2 subplots
ax1 = plt.subplot(2, 2, 1)
ax2 = plt.subplot(2, 2, 2, axisbg='lightgrey')
ax3 = plt.subplot(2, 2, 3)
ax4 = plt.subplot(2, 2, 4, axisbg='#87ceeb')
# plotting in subplot 1
l1,l2, = ax1.plot(X, C, X, S)
ax1.legend([l1, l2],["Cos Wave", "Sin Wave"], loc='best')
ax1.grid(b=True, which='major', color='b', linestyle='-.')
ax1.set_xticks([]) # can use ax1.xticks([]) as well
ax1.set_yticks([])
ax1.set_xlabel("X")
ax1.set_ylabel("Sin and Cos")
ax1.set_title("Waves")
ax1.margins(0.2)
# plotting in subplot 2
l1, = ax2.plot(X, S, c='g')
ax2.legend([l1],["Sin Wave"], loc='best')
ax2.set_xlabel("X")
ax2.set_ylabel("Sin")
ax2.set_title("Sin Wave")
ax2.grid(True)
ax2.grid(b=True, which='major', color='b', linestyle='--')
ax2.margins(0.2)
# plotting in subplot 4
l1, = ax4.plot(X, C, c='r')
ax4.legend([l1],["Cos Wave"], loc='best')
ax4.set_xlabel("X")
ax4.set_ylabel("Cos")
ax4.set_title("Cos Wave")
ax4.grid(True)
ax4.margins(0.2)
As you can see, Subplot 1 is not displayed the grid lines because we have removed the x and y ticks, to display the grid we have to put the ticks with the axis.
For minor ticks between majot ticks we have to use "plt.minorticks_on" or "ax.minorticks_on"
In [10]:
plt.figure(figsize=(12,9))
## Adding 2 subplots
ax1 = plt.subplot(2, 2, 1)
ax2 = plt.subplot(2, 2, 2, axisbg='lightgrey')
ax3 = plt.subplot(2, 2, 3)
ax4 = plt.subplot(2, 2, 4, axisbg='#87ceeb')
# plotting in subplot 1
l1,l2, = ax1.plot(X, C, X, S)
ax1.legend([l1, l2],["Cos Wave", "Sin Wave"], loc='best')
ax1.grid(b=True, which='major', color='b', linestyle='-.')
ax1.minorticks_on()
#ax1.set_xticks([]) # can use ax1.xticks([]) as well
#ax1.set_yticks([])
ax1.set_xlabel("X")
ax1.set_ylabel("Sin and Cos")
ax1.set_title("Waves")
ax1.margins(0.2)
# plotting in subplot 2
l1, = ax2.plot(X, S, c='g')
ax2.legend([l1],["Sin Wave"], loc='best')
ax2.set_xlabel("X")
ax2.set_ylabel("Sin")
ax2.set_title("Sin Wave")
ax2.grid(True)
ax2.grid(b=True, which='major', color='b', linestyle='--')
ax2.margins(0.2)
ax3.grid(True, color='b', alpha=0.5, linestyle='dashed', linewidth=0.5, which='both')
# plotting in subplot 4
l1, = ax4.plot(X, C, c='r')
ax4.legend([l1],["Cos Wave"], loc='best')
ax4.set_xlabel("X")
ax4.set_ylabel("Cos")
ax4.set_title("Cos Wave")
ax4.grid(True)
ax4.margins(0.2)
different type of plots in grid
In [11]:
Z = [ (2,1,1), (2,3,4), (2,3,5), (2,3,6) ]
for nrows, ncols, plot_number in Z:
plt.subplot(nrows, ncols, plot_number)
In [12]:
Z = [ (1,2,1), (3,2,2), (3,2,4), (3,2,6) ]
for nrows, ncols, plot_number in Z:
plt.subplot(nrows, ncols, plot_number)
plt.xticks([])
plt.yticks([])
In [13]:
Z = [ (4,2,1),(4,2,2), (4,2,3), (4,2,5), (4,2,(4,6)), (4,1,4)]
plt.subplots_adjust(bottom=0, left=0, top = 0.975, right=1)
for nrows, ncols, plot_number in Z:
plt.subplot(nrows, ncols, plot_number)
plt.xticks([])
plt.yticks([])
In [14]:
Z = [ (4,3,1),(4,3,2), (4,3,4), (4,3,7), (4,3,(5,8)),(4,3,(3,6)),(4,1,4), (4,3,9) ]
plt.subplots_adjust(bottom=0, left=0, top = 0.975, right=1)
for nrows, ncols, plot_number in Z:
plt.subplot(nrows, ncols, plot_number)
plt.text(0.5, 0.5, plot_number)
plt.xticks([])
plt.yticks([])
Plot Range
It's possible to configure the ranges of the axes. This can be done by using the set_ylim and set_xlim methods in the axis object. With axis('tight') we create automatrically "tightly fitted" axes ranges:
In [15]:
fig, axes = plt.subplots(1, 3, figsize=(10, 4))
x = np.arange(0, 5, 0.25)
axes[0].plot(x, x**2, x, x**3)
axes[0].set_title("default axes ranges")
axes[1].plot(x, x**2, x, x**3)
axes[1].axis('tight')
axes[1].set_title("tight axes")
axes[2].plot(x, x**2, x, x**3)
axes[2].set_ylim([0, 60])
axes[2].set_xlim([2, 5])
axes[2].set_title("custom axes range");
Logarithmic Scale
It is also possible to set a logarithmic scale for one or both axes. This functionality is in fact only one application of a more general transformation system in Matplotlib. Each of the axes' scales are set seperately using set_xscale and set_yscale methods which accept one parameter (with the value "log" in this case):
In [16]:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
x = np.arange(0, 5, 0.25)
ax.plot(x, x**2, x, x**3)
ax.set_yscale("log")
plt.show()
Dual Axis
In [17]:
import numpy as np
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
x = np.arange(1,7,0.1)
ax1.plot(x, 2 * np.pi * x, lw=2, color="blue")
ax1.set_ylabel(r"Circumference $(cm)$", fontsize=16, color="blue")
for label in ax1.get_yticklabels():
label.set_color("blue")
ax2 = ax1.twinx()
ax2.plot(x, np.pi * x ** 2, lw=2, color="darkgreen")
ax2.set_ylabel(r"area $(cm^2)$", fontsize=16, color="darkgreen")
for label in ax2.get_yticklabels():
label.set_color("darkgreen")
Saving Figures
In [18]:
fig.savefig("image.jpg", dpi=200)
In [ ]: