This is a collection of code snippets to cover most common use cases of using matplotlib library
import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(3,2) # fig is Figure instance, axes is 2d list of axes
fig, axes = plt.subplots(1,1)
axes
fig, axes = plt.subplots(1,2)
axes
fig, axes = plt.subplots(2,1)
axes
fig, axes = plt.subplots(2,2)
axes
for ax in axes.flat:
print(ax)
data = np.random.rand(20)*10
fig, axes = plt.subplots(3,2)
axes[0][0].plot(data)
plt.show()
img = np.random.rand(30, 30, 3)
fig, axes = plt.subplots(3,2)
axes[0][0].imshow(img) # changed
plt.show()
fig, axes = plt.subplots(3,2, figsize=(10,10)) # changed
data = np.random.rand(20)*10
axes[0][0].plot(data)
plt.show()
data1 = np.random.rand(20)*10
data2 = np.random.rand(20)*10
data3 = np.random.rand(20)*10
data4 = np.random.rand(20)*10
fig, axes = plt.subplots(1,2)
axes[0].plot(data1)
axes[0].plot(data2) # changed
axes[1].plot(data3) # changed
axes[1].plot(data4) # changed
plt.show()
data1 = np.random.rand(20)*10
data2 = np.random.rand(20)*10
data3 = np.random.rand(20)*10
data4 = np.random.rand(20)*10
fig, axes = plt.subplots(2,1)
axes[0].plot(data1)
axes[0].plot(data2) # changed
axes[1].plot(data3) # changed
axes[1].plot(data4) # changed
plt.show()
data1 = np.random.rand(20)*10
data2 = np.random.rand(20)*10
fig, axes = plt.subplots(1,1)
axes.plot(data1, color='g') # changed
axes.plot(data2, color='r') # changed
plt.show()
data1 = np.random.rand(20)*10
data2 = np.random.rand(20)*10
fig, axes = plt.subplots(2,1)
axes[0].plot(data1, color='g', label='data1') # changed
axes[0].plot(data2, color='r', label='data2') # changed
axes[0].legend() # changed
plt.show()
data1 = np.random.rand(20)*10
data2 = np.random.rand(20)*10
fig, axes = plt.subplots(1,1)
axes.plot(data1, color='g', label='data1')
axes.plot(data2, color='r', label='data2')
axes.set_xticks([]) # changed
axes.set_yticks([]) # changed
axes.legend()
plt.show()
data1 = np.random.rand(20)*10
data2 = np.random.rand(20)*10
fig, axes = plt.subplots(1,1)
axes.plot(data1, color='g', label='data1')
axes.plot(data2, color='r', label='data2')
axes.set_xticks(np.linspace(0, 20, 4, endpoint=False)) # changed
# plt.yticks([])
axes.legend()
plt.show()
data1 = np.random.rand(20)*10
data2 = np.random.rand(20)*10
fig, axes = plt.subplots(1,1)
axes.plot(data1, color='g', label='data1')
axes.plot(data2, color='r', label='data2')
axes.set_xticks(np.linspace(0, 20, 4, endpoint=False))
axes.xaxis.set_minor_locator(plt.MultipleLocator(1)) # changed
axes.legend()
plt.show()
data1 = np.random.rand(20)*10
fig, axes = plt.subplots(1,1)
axes.plot(data1, color='g', label='data1')
axes.set_xticks(np.linspace(0, 20, 4, endpoint=False))
axes.xaxis.set_minor_locator(plt.MultipleLocator(1))
axes.legend()
axes.grid(True, which='both', axis='both') # changed
plt.show()
data1 = np.random.rand(20)*10
fig, axes = plt.subplots(1,1)
axes.xaxis.set_minor_locator(plt.MultipleLocator(1))
axes.plot(data1, color='g', label='data1')
axes.set_xticks(np.linspace(0, 20, 4, endpoint=False))
axes.set_xlim(0, 50) # changed
axes.set_ylim(0, 20) # changed
axes.legend()
axes.grid(True, which='both')
plt.show()