# Python日记（3）——matplotlib基础入门（1）

## Python日记（3）——matplotlib基础入门（1）

(1)简明matplotlib

``````import matplotlib.pyplot as plt
import numpy as np
``````

``````import matplotlib.pyplot as plt
import numpy as np

data = np.arange(10)
plt.plot(data)

plt.show()
``````

(2)图板与子图

``````fig = plt.figure()
``````

``````ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)

``````

ps:更为方便的用法如下：

``````fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].set(title='Upper Left')
axes[0,1].set(title='Upper Right')
axes[1,0].set(title='Lower Left')
axes[1,1].set(title='Lower Right')
``````

``````fig, axes = plt.subplots(2, 3)
``````

``````subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
``````

`wspace``hspace`分别控制的是图片的宽度和高度百分比吗，以用作子图间的间距，下面是个小例子，我将这个间距一直缩小到零。

``````fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)

for i in range(2):
for j in range(2):
axes[i, j].hist(np.random.randn(500), bins=50, color='k', alpha=0.5)