common use cases for matplotlib (object API)¶
This is a collection of code snippets to cover most common use cases of using matplotlib library
In [1]:
import numpy as np
1. Initialize ¶
In [ ]:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(3,2) # fig is Figure instance, axes is 2d list of axes
1.a Iterate over axes ¶
In [24]:
fig, axes = plt.subplots(1,1)
axes
Out[24]:
In [25]:
fig, axes = plt.subplots(1,2)
axes
Out[25]:
In [26]:
fig, axes = plt.subplots(2,1)
axes
Out[26]:
In [27]:
fig, axes = plt.subplots(2,2)
axes
Out[27]:
In [7]:
for ax in axes.flat:
print(ax)
In [ ]:
2. Plot some data ¶
In [10]:
data = np.random.rand(20)*10
fig, axes = plt.subplots(3,2)
axes[0][0].plot(data)
plt.show()
3. show an image ¶
In [12]:
img = np.random.rand(30, 30, 3)
fig, axes = plt.subplots(3,2)
axes[0][0].imshow(img) # changed
plt.show()
4. plot size ¶
In [15]:
fig, axes = plt.subplots(3,2, figsize=(10,10)) # changed
data = np.random.rand(20)*10
axes[0][0].plot(data)
plt.show()
5. multiple data plotting ¶
In [19]:
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()
In [21]:
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()
6. set plot color ¶
In [23]:
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()
7. plot legend ¶
In [29]:
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()
8. axis turn on/off ¶
In [34]:
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()
9. major ticks ¶
In [35]:
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()
10. minor ticks ¶
In [37]:
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()
11. grid ¶
In [47]:
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()
12. Axis limits ¶
In [43]:
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()
In [ ]:
In [ ]: