# 我們最喜歡的秘籍
> 原文:[Our Favorite Recipes](http://matplotlib.org/users/recipes.html)
> 譯者:[飛龍](https://github.com/)
> 協議:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
這里是一個簡短的教程,示例和代碼片段的集合,展示了一些有用的經驗和技巧,來制作更精美的圖像,并克服一些 matplotlib 的缺陷。
## 共享軸限制和視圖
通常用于使兩個或更多繪圖共享一個軸,例如,兩個子繪圖具有時間作為公共軸。 當你平移和縮放一個繪圖,你想讓另一個繪圖一起移動。 為了方便這一點,matplotlib 軸支持`sharex`和`sharey`屬性。 創建`subplot()`或`axes()`實例時,你可以傳入一個關鍵字,表明要共享的軸。
```py
In [96]: t = np.arange(0, 10, 0.01)
In [97]: ax1 = plt.subplot(211)
In [98]: ax1.plot(t, np.sin(2*np.pi*t))
Out[98]: [<matplotlib.lines.Line2D object at 0x98719ec>]
In [99]: ax2 = plt.subplot(212, sharex=ax1)
In [100]: ax2.plot(t, np.sin(4*np.pi*t))
Out[100]: [<matplotlib.lines.Line2D object at 0xb7d8fec>]
```
## 輕松創建子圖
在 matplotlib 的早期版本中,如果你想使用 pythonic API 并創建一個`figure`實例,并從中創建一個`subplots`網格,而且可能帶有共享軸,它涉及大量的樣板代碼。 例如:
```py
# old style
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(223, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(224, sharex=ax1, sharey=ax1)
```
Fernando Perez 提供了一個很好的頂級方法,來一次性創建`subplots()`(注意末尾的`s`),并為所有子圖開啟`x`和`y`共享。 你可以單獨解構來獲取軸域:
```py
# new style method 1; unpack the axes
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax1.plot(x)
```
或將它們作為行數乘列數的對象數組返回,支持 numpy 索引:
```py
# new style method 2; use an axes array
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
axs[0,0].plot(x)
```
## 修復常見的日期問題
matplotlib 允許你本地繪制 python datetime 實例,并且在大多數情況下,可以很好地挑選刻度位置和字符串格式。 但有幾件事情它不能妥善處理,這里有一些技巧,用于幫助你解決他們。 我們將在`numpy`記錄數組中加載一些包含`datetime.date`對象的示例日期數據:
```py
In [63]: datafile = cbook.get_sample_data('goog.npy')
In [64]: r = np.load(datafile).view(np.recarray)
In [65]: r.dtype
Out[65]: dtype([('date', '|O4'), ('', '|V4'), ('open', '<f8'),
('high', '<f8'), ('low', '<f8'), ('close', '<f8'),
('volume', '<i8'), ('adj_close', '<f8')])
In [66]: r.date
Out[66]:
array([2004-08-19, 2004-08-20, 2004-08-23, ..., 2008-10-10, 2008-10-13,
2008-10-14], dtype=object)
```
字段日期的`numpy`記錄數組的`dtype`是`| O4`,這意味著它是一個 4 字節的 python 對象指針; 在這種情況下,對象是`datetime.date`實例,當我們在 ipython 終端窗口中打印一些樣本時,我們可以看到。
如果你繪制數據,
```py
In [67]: plot(r.date, r.close)
Out[67]: [<matplotlib.lines.Line2D object at 0x92a6b6c>]
```
你會看到 x 軸標簽重合到一起。

另一個麻煩是,如果你將鼠標懸停在窗口上,并在 x 和 y 坐標處查看 matplotlib 工具欄([交互式導航](http://matplotlib.org/users/navigation_toolbar.html#navigation-toolbar))的右下角,你會看到 x 位置的格式與刻度標簽的格式相同, 例如,『Dec 2004』。 我們想要的是工具欄中的位置具有更高的精確度,例如,鼠標懸停在上面時給我們確切的日期。 為了解決第一個問題,我們可以使用[`matplotlib.figure.Figure.autofmt_xdate()`](http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.autofmt_xdate)。修復第二個問題,我們可以使用`ax.fmt_xdata`屬性,該屬性可以設置為任何接受標量并返回字符串的函數。 matplotlib 有一些內置的日期格式化器,所以我們將使用其中的一個。
```py
plt.close('all')
fig, ax = plt.subplots(1)
ax.plot(r.date, r.close)
# rotate and align the tick labels so they look better
fig.autofmt_xdate()
# use a more precise date string for the x axis locations in the
# toolbar
import matplotlib.dates as mdates
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
plt.title('fig.autofmt_xdate fixes the labels')
```

現在,當你將鼠標懸停在繪制的數據上,你將在工具欄中看到如`2004-12-01`的日期格式字符串。
## 透明度填充
`fill_between()`函數在最小和最大邊界之間生成陰影區域,用于展示范圍。 它有一個非常方便的參數,將填充范圍與邏輯范圍組合,例如,以便僅填充超過某個閾值的曲線。=
基本上,`fill_between`可以用來增強圖形的視覺外觀。 讓我們比較兩個財務-時間圖表,左邊是一個簡單的線框圖,右邊是一個填充圖。

Alpha 通道在這里不是必需的,但它可以用來軟化顏色,創建更具視覺吸引力的繪圖。 在其他示例中,我們將在下面看到,Alpha 通道在功能上有用,因為陰影區域可以重疊,Alpha 允許你同時看到兩者。 注意,postscript 格式不支持 alpha(這是一個 postscript 限制,而不是一個 matplotlib 限制),因此,當使用 alpha 時,將你的數字保存在 PNG,PDF 或 SVG 中。
我們的下一個例子是計算隨機漫步的兩個群體,它們具有不同的正態分布平均值和標準差,足跡會從中繪制。我們使用共享區域來繪制群體的平均位置的加/減一個標準差。 這里的 Alpha 通道是有用的,不只是為了審美。
```py
import matplotlib.pyplot as plt
import numpy as np
Nsteps, Nwalkers = 100, 250
t = np.arange(Nsteps)
# an (Nsteps x Nwalkers) array of random walk steps
S1 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers)
S2 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers)
# an (Nsteps x Nwalkers) array of random walker positions
X1 = S1.cumsum(axis=0)
X2 = S2.cumsum(axis=0)
# Nsteps length arrays empirical means and standard deviations of both
# populations over time
mu1 = X1.mean(axis=1)
sigma1 = X1.std(axis=1)
mu2 = X2.mean(axis=1)
sigma2 = X2.std(axis=1)
# plot it!
fig, ax = plt.subplots(1)
ax.plot(t, mu1, lw=2, label='mean population 1', color='blue')
ax.plot(t, mu2, lw=2, label='mean population 2', color='yellow')
ax.fill_between(t, mu1+sigma1, mu1-sigma1, facecolor='blue', alpha=0.5)
ax.fill_between(t, mu2+sigma2, mu2-sigma2, facecolor='yellow', alpha=0.5)
ax.set_title('random walkers empirical $\mu$ and $\pm \sigma$ interval')
ax.legend(loc='upper left')
ax.set_xlabel('num steps')
ax.set_ylabel('position')
ax.grid()
```

`where`關鍵字參數非常方便地用于突出顯示圖形的某些區域。 其中使用與`x`,`ymin`和`ymax`參數相同長度的布爾掩碼,并且只填充布爾掩碼為`True`的區域。 在下面的例子中,我們模擬一個隨機漫步者,并計算人口位置的分析平均值和標準差。 群體平均值顯示為黑色虛線,并且平均值的加/減一個標準差顯示為黃色填充區域。 我們使用`where=X>upper_bound`找到漫步者在一個標準差邊界之上的區域,并將該區域變成藍色。

## 透明、花式圖例
有時你在繪制數據之前就知道你的數據是什么樣的,并且可能知道例如右上角沒有太多數據。 然后,你可以安全地創建不覆蓋你的數據的圖例:
```py
ax.legend(loc='upper right')
```
其他時候你不知道你的數據在哪里,而`loc ='best'`將嘗試和放置圖例:
```py
ax.legend(loc='best')
```
但仍然,你的圖例可能會覆蓋你的數據,在這些情況下,使圖例框架透明非常不錯。
```py
np.random.seed(1234)
fig, ax = plt.subplots(1)
ax.plot(np.random.randn(300), 'o-', label='normal distribution')
ax.plot(np.random.rand(300), 's-', label='uniform distribution')
ax.set_ylim(-3, 3)
ax.legend(loc='best', fancybox=True, framealpha=0.5)
ax.set_title('fancy, transparent legends')
```

## 放置文本框
當使用文本框裝飾軸時,兩個有用的技巧是將文本放置在軸域坐標中(請參見[變換教程](http://matplotlib.org/users/transforms_tutorial.html#transforms-tutorial)),因此文本不會隨著 x 或 y 軸的變化而移動。 你還可以使用文本的`bbox`屬性,用`Patch`實例包圍文本 - `bbox`關鍵字參數接受字典,字典的鍵是補丁的屬性。
```py
np.random.seed(1234)
fig, ax = plt.subplots(1)
x = 30*np.random.randn(10000)
mu = x.mean()
median = np.median(x)
sigma = x.std()
textstr = '$\mu=%.2f$\n$\mathrm{median}=%.2f$\n$\sigma=%.2f$'%(mu, median, sigma)
ax.hist(x, 50)
# these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
# place a text box in upper left in axes coords
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=props)
```
