Plot a line segment
x= np.linspace(0, 100) #create a continuous range of x
y= 0.5 * x + 5 #calculate the y
fig = plt.figure(1, figsize=(8, 8))
plt.plot(x, y) #simply plot it
plt.show()
add annotation
ax = fig.add_subplot(111) # add a sub plot
ax.annotate('whatever you want to say', xy=(0.5, 0.5), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.1), )
#######multiple sub plots##############
import numpy as np
import matplotlib.pyplot as plt
imgs = np.random.randn(4, 3, 3)
fig = plt.figure()
for i in range(imgs.shape[0]):
img = imgs[i]
ax = plt.subplot(3, 3, i + 1)
plt.tight_layout()
ax.set_title('Sample #{}'.format(i))
plt.imshow(img)
plt.show()
plot two y axies and custom legends
Two y axies require two ax to plot. Each ax can have its own legend, but the two legends are not combined by default.
It needs a custom legend to show both in a block
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot([1,2], [3,4], label='abc', color ='red')
ax2.plot([3,4], [2,3], label='efg', color = 'blue')
legend_elements = [
Line2D([0], [0], color="red", lw=4, label="abc"),
Line2D([0], [0], color="blue", lw=2, label="efg"),
]
plt.legend(handles=legend_elements, loc=(0.8, 0.8))
plt.show()
Matplotlib subplots with different widths
To get 4 subplots in the first row
and 1 subplot in the second row spanning the full width
and 1 subplot in the third row spanning the full width
| | | | |
| |
| |
The plt.subplot adds a subplot to the plotting.
The 3 number parameters are nrows, ncols and index.
The subplot function parameters are (num of rows, num of cols, index)
for ax1 to ax4, they are the top subplots with 4 cols, so ncols = 4, the index is 1 to 4 for each of them.
There are totally 3 rows in the whole figure, so nrows = 3 for all the subplots.
for ax5 and ax6, it has only one colum, so ncols = 1. The index seems to be row index, so it is 2 and 3.
plt.figure(figsize=(15, 9))
ax1 = plt.subplot(3,4,1)
ax2 = plt.subplot(3,4,2)
ax3 = plt.subplot(3,4,3)
ax4 = plt.subplot(3,4,4)
ax5 = plt.subplot(3,1,2)
ax6 = plt.subplot(3,1,3)
axes = [ax1, ax2, ax3, ax4, ax5, ax6]
A more complex example, 3 rows with 3 cols in first row, 1 col in second row and 2 cols in thrid row.
The index is 1, 2, 3 ... 4, 5 except the 1-col row uses row number as index
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(11.69, 8.27))
x, y = range(10), np.random.randint(0,100,10)
ax1 = plt.subplot(3,3,1) #1st row, 1st cell
ax1.plot(x, y, color='red')
ax2 = plt.subplot(3,3,2) #1st row, 2nd cell
ax2.plot(x, y, color='blue')
ax3 = plt.subplot(3,3,3) #1st row, 3rd cell
ax3.plot(x, y, color='blue')
ax4 = plt.subplot(3,1,2) #2nd row, 4th cell, but index = 2 (2nd row)
ax4.plot(x, y, color='green')
ax5 = plt.subplot(3,2,5) #3rd row, 5th cell, index=5
ax5.plot(x, y, color='cyan')
ax6 = plt.subplot(3,2,6) #3rd row, 6th cell, index=6
ax6.plot(x, y, color='purple')
plt.show()
Let change it a bit, 3 rows with 2 cols in first row, 1 column in second row and 3 cols in third row.
If we still use the index 1,2, for first two cols, 2 ror second row, and 4, 5, 6 for last 3 cols, then the plot is wrong with 2 nd and 3rd rows swapped.
The solution is using the grid index. The max cols is 3 and max rows is 3 here, so the index is based on a 3 x 3 grid.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(11.69, 8.27))
x, y = range(10), np.random.randint(0,100,10)
ax1 = plt.subplot(3,2,1)
ax1.plot(x, y, color='red')
ax2 = plt.subplot(3,2,2)
ax2.plot(x, y, color='blue')
ax3 = plt.subplot(3,1,2)
ax3.plot(x, y, color='green')
ax4 = plt.subplot(3,3,7) # use 7, 8, 9 for the last three cols in the 3rd row
ax4.plot(x, y, color='cyan')
ax5 = plt.subplot(3,3,8)
ax5.plot(x, y, color='purple')
ax6 = plt.subplot(3,3,9)
ax6.plot(x, y, color='black')
plt.show()
Contour graph
X,Y coordinates with value z. The XY doesn't have to be a mesh but any shape. So triangulation is built in.
Plot a contour for the x, y range
xy = np.random.randint(low=0, high=10, size=(20, 2))
z = np.random.rand(len(xy))
p = plt.tricontourf(xy[:, 0], xy[:, 1], z)
plt.colorbar(p)