Skip to content

Commit d20645a

Browse files
committedJan 25, 2017
8.2
1 parent a72e7ea commit d20645a

File tree

1 file changed

+114
-4
lines changed

1 file changed

+114
-4
lines changed
 

‎8.2.md

+114-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# 我们最喜欢的秘籍
22

3+
> 原文:[Our Favorite Recipes](http://matplotlib.org/users/recipes.html)
4+
5+
> 译者:[飞龙](https://github.com/)
6+
7+
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
8+
39
这里是一个简短的教程,示例和代码片段的集合,展示了一些有用的经验和技巧,来制作更精美的图像,并克服一些 matplotlib 的缺陷。
410

511
## 共享轴限制和视图
612

7-
通常用于使两个或更多绘图共享一个轴,例如,两个子绘图具有时间作为公共轴。 当你平移和缩放一个绘图,你想让另一个绘图一起移动。 为了方便这一点,matplotlib 轴支持`sharex``sharey`属性。 创建`subplot()``axes()`实例时,您可以传入一个关键字,表明要共享的轴。
13+
通常用于使两个或更多绘图共享一个轴,例如,两个子绘图具有时间作为公共轴。 当你平移和缩放一个绘图,你想让另一个绘图一起移动。 为了方便这一点,matplotlib 轴支持`sharex``sharey`属性。 创建`subplot()``axes()`实例时,你可以传入一个关键字,表明要共享的轴。
814

915
```py
1016
In [96]: t = np.arange(0, 10, 0.01)
@@ -34,7 +40,7 @@ ax3 = fig.add_subplot(224, sharex=ax1, sharey=ax1)
3440
```
3541

3642

37-
Fernando Perez 提供了一个很好的顶级方法,来一次性创建`subplots()`(注意末尾的`s`),并为所有子图开启`x``y`共享。 您可以单独解构来获取轴域
43+
Fernando Perez 提供了一个很好的顶级方法,来一次性创建`subplots()`(注意末尾的`s`),并为所有子图开启`x``y`共享。 你可以单独解构来获取轴域
3844

3945
```py
4046
# new style method 1; unpack the axes
@@ -83,7 +89,7 @@ Out[67]: [<matplotlib.lines.Line2D object at 0x92a6b6c>]
8389

8490
![](http://matplotlib.org/_images/recipes-2.png)
8591

86-
另一个麻烦是,如果你将鼠标悬停在窗口上,并在 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 有一些内置的日期格式化器,所以我们将使用其中的一个。
92+
另一个麻烦是,如果你将鼠标悬停在窗口上,并在 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 有一些内置的日期格式化器,所以我们将使用其中的一个。
8793

8894
```py
8995
plt.close('all')
@@ -102,7 +108,111 @@ plt.title('fig.autofmt_xdate fixes the labels')
102108

103109
![](http://matplotlib.org/_images/recipes-3.png)
104110

105-
现在,当您将鼠标悬停在绘制的数据上,您将在工具栏中看到如`2004-12-01`的日期格式字符串。
111+
现在,当你将鼠标悬停在绘制的数据上,你将在工具栏中看到如`2004-12-01`的日期格式字符串。
106112

107113
## 透明度填充
108114

115+
`fill_between()`函数在最小和最大边界之间生成阴影区域,用于展示范围。 它有一个非常方便的参数,将填充范围与逻辑范围组合,例如,以便仅填充超过某个阈值的曲线。=
116+
117+
基本上,`fill_between`可以用来增强图形的视觉外观。 让我们比较两个财务-时间图表,左边是一个简单的线框图,右边是一个填充图。
118+
119+
![](http://matplotlib.org/_images/recipes-4.png)
120+
121+
Alpha 通道在这里不是必需的,但它可以用来软化颜色,创建更具视觉吸引力的绘图。 在其他示例中,我们将在下面看到,Alpha 通道在功能上有用,因为阴影区域可以重叠,Alpha 允许你同时看到两者。 注意,postscript 格式不支持 alpha(这是一个 postscript 限制,而不是一个 matplotlib 限制),因此,当使用 alpha 时,将你的数字保存在 PNG,PDF 或 SVG 中。
122+
123+
我们的下一个例子是计算随机漫步的两个群体,它们具有不同的正态分布平均值和标准差,足迹会从中绘制。我们使用共享区域来绘制群体的平均位置的加/减一个标准差。 这里的 Alpha 通道是有用的,不只是为了审美。
124+
125+
```py
126+
import matplotlib.pyplot as plt
127+
import numpy as np
128+
129+
Nsteps, Nwalkers = 100, 250
130+
t = np.arange(Nsteps)
131+
132+
# an (Nsteps x Nwalkers) array of random walk steps
133+
S1 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers)
134+
S2 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers)
135+
136+
# an (Nsteps x Nwalkers) array of random walker positions
137+
X1 = S1.cumsum(axis=0)
138+
X2 = S2.cumsum(axis=0)
139+
140+
141+
# Nsteps length arrays empirical means and standard deviations of both
142+
# populations over time
143+
mu1 = X1.mean(axis=1)
144+
sigma1 = X1.std(axis=1)
145+
mu2 = X2.mean(axis=1)
146+
sigma2 = X2.std(axis=1)
147+
148+
# plot it!
149+
fig, ax = plt.subplots(1)
150+
ax.plot(t, mu1, lw=2, label='mean population 1', color='blue')
151+
ax.plot(t, mu2, lw=2, label='mean population 2', color='yellow')
152+
ax.fill_between(t, mu1+sigma1, mu1-sigma1, facecolor='blue', alpha=0.5)
153+
ax.fill_between(t, mu2+sigma2, mu2-sigma2, facecolor='yellow', alpha=0.5)
154+
ax.set_title('random walkers empirical $\mu$ and $\pm \sigma$ interval')
155+
ax.legend(loc='upper left')
156+
ax.set_xlabel('num steps')
157+
ax.set_ylabel('position')
158+
ax.grid()
159+
```
160+
161+
![](http://matplotlib.org/_images/recipes-5.png)
162+
163+
`where`关键字参数非常方便地用于突出显示图形的某些区域。 其中使用与`x``ymin``ymax`参数相同长度的布尔掩码,并且只填充布尔掩码为`True`的区域。 在下面的例子中,我们模拟一个随机漫步者,并计算人口位置的分析平均值和标准差。 群体平均值显示为黑色虚线,并且平均值的加/减一个标准差显示为黄色填充区域。 我们使用`where=X>upper_bound`找到漫步者在一个标准差边界之上的区域,并将该区域变成蓝色。
164+
165+
![](http://matplotlib.org/_images/recipes-6.png)
166+
167+
## 透明、花式图例
168+
169+
有时你在绘制数据之前就知道你的数据是什么样的,并且可能知道例如右上角没有太多数据。 然后,你可以安全地创建不覆盖你的数据的图例:
170+
171+
```py
172+
ax.legend(loc='upper right')
173+
```
174+
175+
其他时候你不知道你的数据在哪里,而`loc ='best'`将尝试和放置图例:
176+
177+
```py
178+
ax.legend(loc='best')
179+
```
180+
181+
但仍然,你的图例可能会覆盖你的数据,在这些情况下,使图例框架透明非常不错。
182+
183+
```py
184+
np.random.seed(1234)
185+
fig, ax = plt.subplots(1)
186+
ax.plot(np.random.randn(300), 'o-', label='normal distribution')
187+
ax.plot(np.random.rand(300), 's-', label='uniform distribution')
188+
ax.set_ylim(-3, 3)
189+
ax.legend(loc='best', fancybox=True, framealpha=0.5)
190+
191+
ax.set_title('fancy, transparent legends')
192+
```
193+
194+
![](http://matplotlib.org/_images/recipes-7.png)
195+
196+
## 放置文本框
197+
198+
当使用文本框装饰轴时,两个有用的技巧是将文本放置在轴域坐标中(请参见[转换教程](http://matplotlib.org/users/transforms_tutorial.html#transforms-tutorial)),因此文本不会随着 x 或 y 轴的变化而移动。 你还可以使用文本的`bbox`属性,用`Patch`实例包围文本 - `bbox`关键字参数接受字典,字典带有`Patch`属性的键。
199+
200+
```py
201+
np.random.seed(1234)
202+
fig, ax = plt.subplots(1)
203+
x = 30*np.random.randn(10000)
204+
mu = x.mean()
205+
median = np.median(x)
206+
sigma = x.std()
207+
textstr = '$\mu=%.2f$\n$\mathrm{median}=%.2f$\n$\sigma=%.2f$'%(mu, median, sigma)
208+
209+
ax.hist(x, 50)
210+
# these are matplotlib.patch.Patch properties
211+
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
212+
213+
# place a text box in upper left in axes coords
214+
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
215+
verticalalignment='top', bbox=props)
216+
```
217+
218+
![](http://matplotlib.org/_images/recipes-8.png)

0 commit comments

Comments
 (0)