matplotlib & pandas

This commit is contained in:
estomm
2020-09-26 22:03:11 +08:00
parent 73cc328c81
commit d31be4f219
599 changed files with 99925 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
# 修复常见的日期困扰
Matplotlib允许您原生地绘制python日期时间实例并且在大多数情况下可以很好地选择刻度位置和字符串格式。 有一些事情没有得到如此优雅的处理这里有一些技巧可以帮助你解决它们。我们将在numpy记录数组中加载一些包含datetime.date对象的样本日期数据
```python
In [63]: datafile = cbook.get_sample_data('goog.npz')
In [64]: r = np.load(datafile)['price_data'].view(np.recarray)
In [65]: r.dtype
Out[65]: dtype([('date', '<M8[D]'), ('', '|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='datetime64[D]')
```
字段日期的NumPy记录数组的dtype是datetime64[D],这意味着它是'day'单位的64位np.datetime64。 虽然这种格式更便于携带但Matplotlib无法原生地绘制此格式。 我们可以通过将日期更改为[datetime.date](https://docs.python.org/3/library/datetime.html#datetime.date)实例来绘制此数据,这可以通过转换为对象数组来实现:
```python
In [67]: r.date.astype('O')
array([datetime.date(2004, 8, 19), datetime.date(2004, 8, 20),
datetime.date(2004, 8, 23), ..., datetime.date(2008, 10, 10),
datetime.date(2008, 10, 13), datetime.date(2008, 10, 14)],
dtype=object)
```
此转换后的数组的dtype现在是对象而是填充了datetime.date实例。
如果您绘制数据,
```python
In [67]: plot(r.date.astype('O'), r.close)
Out[67]: [<matplotlib.lines.Line2D object at 0x92a6b6c>]
```
你会看到x刻度标签都被压扁了。
```python
import matplotlib.cbook as cbook
import matplotlib.dates as mdates
import numpy as np
import matplotlib.pyplot as plt
with cbook.get_sample_data('goog.npz') as datafile:
r = np.load(datafile)['price_data'].view(np.recarray)
# Matplotlib prefers datetime instead of np.datetime64.
date = r.date.astype('O')
fig, ax = plt.subplots()
ax.plot(date, r.close)
ax.set_title('Default date handling can cause overlapping labels')
```
![修复常见的日期困扰示例](https://matplotlib.org/_images/sphx_glr_common_date_problems_001.png)
另一个烦恼是如果您将鼠标悬停在窗口上并在x和y坐标处查看matplotlib工具栏[交互式导航](https://matplotlib.org/users/navigation_toolbar.html#navigation-toolbar)的右下角您会看到x位置的格式与刻度标签的格式相同 例如“2004年12月”。
我们想要的是工具栏中的位置具有更高的精确度,例如,为我们提供鼠标悬停的确切日期。 为了解决第一个问题,我们可以使用[matplotlib.figure.Figure.autofmt_xdate()](https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.autofmt_xdate) 来修复第二个问题我们可以使用ax.fmt_xdata属性该属性可以设置为任何带标量并返回字符串的函数。 matplotlib内置了许多日期格式化程序因此我们将使用其中之一。
```python
fig, ax = plt.subplots()
ax.plot(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
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
ax.set_title('fig.autofmt_xdate fixes the labels')
```
![修复常见的日期困扰2](https://matplotlib.org/_images/sphx_glr_common_date_problems_002.png)
现在当您将鼠标悬停在绘制的数据上时您将在工具栏中看到日期格式字符串如2004-12-01。
```python
plt.show()
```
## 下载这个示例
- [下载python源码: common_date_problems.py](https://matplotlib.org/_downloads/common_date_problems.py)
- [下载Jupyter notebook: common_date_problems.ipynb](https://matplotlib.org/_downloads/common_date_problems.ipynb)

View File

@@ -0,0 +1,46 @@
# 轻松创建子图
在matplotlib的早期版本中如果你想使用pythonic API并创建一个图形实例并从中创建一个子图的网格可能有共享轴它涉及相当数量的样板代码。例如
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(50)
# 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)
```
![轻松创建子图示例](https://matplotlib.org/_images/sphx_glr_create_subplots_001.png)
费尔南多·佩雷斯提供了一个很好的方法来创建子图的一切 ``subplots()``最后注意“s”并为整个群体打开x和y共享。您可以单独打开轴...
```python
# new style method 1; unpack the axes
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax1.plot(x)
```
![轻松创建子图示例2](https://matplotlib.org/_images/sphx_glr_create_subplots_002.png)
或者将它们作为支持numpy索引的numrows x numcolumns对象数组返回
```python
# new style method 2; use an axes array
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
axs[0, 0].plot(x)
plt.show()
```
![轻松创建子图示例3](https://matplotlib.org/_images/sphx_glr_create_subplots_003.png)
## 下载这个示例
- [下载python源码: create_subplots.py](https://matplotlib.org/_downloads/create_subplots.py)
- [下载Jupyter notebook: create_subplots.ipynb](https://matplotlib.org/_downloads/create_subplots.ipynb)

View File

@@ -0,0 +1,121 @@
# 在和Alpha之间填充
[fill_between()](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.fill_between.html#matplotlib.axes.Axes.fill_between)函数在最小和最大边界之间生成阴影区域,这对于说明范围很有用。 它具有非常方便的用于将填充与逻辑范围组合的参数,例如,仅在某个阈值上填充曲线。
在最基本的层面上,``fill_between`` 可用于增强图形的视觉外观。让我们将两个财务时间图与左边的简单线图和右边的实线进行比较。
```python
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cbook as cbook
# load up some sample financial data
with cbook.get_sample_data('goog.npz') as datafile:
r = np.load(datafile)['price_data'].view(np.recarray)
# Matplotlib prefers datetime instead of np.datetime64.
date = r.date.astype('O')
# create two subplots with the shared x and y axes
fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
pricemin = r.close.min()
ax1.plot(date, r.close, lw=2)
ax2.fill_between(date, pricemin, r.close, facecolor='blue', alpha=0.5)
for ax in ax1, ax2:
ax.grid(True)
ax1.set_ylabel('price')
for label in ax2.get_yticklabels():
label.set_visible(False)
fig.suptitle('Google (GOOG) daily closing price')
fig.autofmt_xdate()
```
![在和Alpha之间填充示例](https://matplotlib.org/_images/sphx_glr_fill_between_alpha_001.png)
此处不需要Alpha通道但它可以用于软化颜色以获得更具视觉吸引力的图形。在其他示例中正如我们将在下面看到的alpha通道在功能上非常有用因为阴影区域可以重叠alpha允许您查看两者。请注意postscript格式不支持alpha这是postscript限制而不是matplotlib限制因此在使用alpha时保存PNGPDF或SVG中的数字。
我们的下一个例子计算两个随机游走者群体,它们具有不同的正态分布的均值和标准差,从中得出步骤。我们使用共享区域绘制人口平均位置的+/-一个标准偏差。 这里的alpha通道非常有用而不仅仅是审美。
```python
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(r'random walkers empirical $\mu$ and $\pm \sigma$ interval')
ax.legend(loc='upper left')
ax.set_xlabel('num steps')
ax.set_ylabel('position')
ax.grid()
```
![在和Alpha之间填充示例2](https://matplotlib.org/_images/sphx_glr_fill_between_alpha_002.png)
where关键字参数非常便于突出显示图形的某些区域。其中布尔掩码的长度与xymin和ymax参数的长度相同并且仅填充布尔掩码为True的区域。在下面的示例中我们模拟单个随机游走者并计算人口位置的分析平均值和标准差。总体平均值显示为黑色虚线并且与平均值的正/负一西格玛偏差显示为黄色填充区域。我们使用where掩码X> upper_bound来找到walker在一个sigma边界之上的区域并将该区域遮蔽为蓝色。
```python
Nsteps = 500
t = np.arange(Nsteps)
mu = 0.002
sigma = 0.01
# the steps and position
S = mu + sigma*np.random.randn(Nsteps)
X = S.cumsum()
# the 1 sigma upper and lower analytic population bounds
lower_bound = mu*t - sigma*np.sqrt(t)
upper_bound = mu*t + sigma*np.sqrt(t)
fig, ax = plt.subplots(1)
ax.plot(t, X, lw=2, label='walker position', color='blue')
ax.plot(t, mu*t, lw=1, label='population mean', color='black', ls='--')
ax.fill_between(t, lower_bound, upper_bound, facecolor='yellow', alpha=0.5,
label='1 sigma range')
ax.legend(loc='upper left')
# here we use the where argument to only fill the region where the
# walker is above the population 1 sigma boundary
ax.fill_between(t, upper_bound, X, where=X > upper_bound, facecolor='blue',
alpha=0.5)
ax.set_xlabel('num steps')
ax.set_ylabel('position')
ax.grid()
```
![在和Alpha之间填充示例3](https://matplotlib.org/_images/sphx_glr_fill_between_alpha_003.png)
填充区域的另一个方便用途是突出显示轴的水平或垂直跨度 - 因为matplotlib具有一些辅助函数 [axhspan()](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.axhspan.html#matplotlib.axes.Axes.axhspan) 和[axvspan()](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.axvspan.html#matplotlib.axes.Axes.axvspan) 以及示例[axhspan Demo](https://matplotlib.org/gallery/subplots_axes_and_figures/axhspan_demo.html)。
```python
plt.show()
```
## 下载这个示例
- [下载python源码: fill_between_alpha.py](https://matplotlib.org/_downloads/fill_between_alpha.py)
- [下载Jupyter notebook: fill_between_alpha.ipynb](https://matplotlib.org/_downloads/fill_between_alpha.ipynb)

View File

@@ -0,0 +1,3 @@
# 我们最喜欢的技巧
这是一个简短的教程示例和代码片段的集合说明了一些有用的惯例和技巧以制作更流畅的图形和克服一些matplotlib缺陷。

View File

@@ -0,0 +1,37 @@
# 放置文本框
使用文本框装饰轴时,有两个有用的技巧是将文本放在轴坐标中(请参阅[转换教程](https://matplotlib.org/tutorials/advanced/transforms_tutorial.html)因此文本不会随着x或y限制的变化而移动。 您还可以使用文本的bbox属性用[Patch](https://matplotlib.org/api/_as_gen/matplotlib.patches.Patch.html#matplotlib.patches.Patch)实例包围文本 - bbox关键字参数使用带有Patch属性的键的字典。
![放置文本框](https://matplotlib.org/_images/sphx_glr_placing_text_boxes_001.png)
```python
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(19680801)
fig, ax = plt.subplots()
x = 30*np.random.randn(10000)
mu = x.mean()
median = np.median(x)
sigma = x.std()
textstr = '\n'.join((
r'$\mu=%.2f$' % (mu, ),
r'$\mathrm{median}=%.2f$' % (median, ),
r'$\sigma=%.2f$' % (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)
plt.show()
```
## 下载这个示例
- [下载python源码: placing_text_boxes.py](https://matplotlib.org/_downloads/placing_text_boxes.py)
- [下载Jupyter notebook: placing_text_boxes.ipynb](https://matplotlib.org/_downloads/placing_text_boxes.ipynb)

View File

@@ -0,0 +1,25 @@
# 共享轴限制和视图
制作共享轴的两个或更多个图是常见的,例如,两个子图以时间作为公共轴。 当您平移和缩放其中一个时,您希望另一个随身携带。 为此matplotlib Axes支持sharex和sharey属性。创建[subplot()](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html#matplotlib.pyplot.subplot)或[axes()](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axes.html#matplotlib.pyplot.axes)实例时,可以传入一个关键字,指示要与之共享的轴。
![共享轴限制和视图示例](https://matplotlib.org/_images/sphx_glr_share_axis_lims_views_001.png)
```python
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0, 10, 0.01)
ax1 = plt.subplot(211)
ax1.plot(t, np.sin(2*np.pi*t))
ax2 = plt.subplot(212, sharex=ax1)
ax2.plot(t, np.sin(4*np.pi*t))
plt.show()
```
## 下载这个示例
- [下载python源码: share_axis_lims_views.py](https://matplotlib.org/_downloads/share_axis_lims_views.py)
- [下载Jupyter notebook: share_axis_lims_views.ipynb](https://matplotlib.org/_downloads/share_axis_lims_views.ipynb)

View File

@@ -0,0 +1,38 @@
# 透明、花式的图形
有时您在绘制数据之前就知道数据的样子,并且可能知道例如右上角没有太多数据。然后,您可以安全地创建不覆盖数据的图例:
```python
ax.legend(loc='upper right')
```
其他时候你不知道你的数据在哪里默认的loc ='best'会尝试放置图例:
```python
ax.legend()
```
但是,您的图例可能会与您的数据重叠,在这些情况下,使图例框架透明是很好的。
![透明、花式的图形示例](https://matplotlib.org/_images/sphx_glr_transparent_legends_001.png)
```python
import matplotlib.pyplot as plt
import numpy as np
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(fancybox=True, framealpha=0.5)
ax.set_title('fancy, transparent legends')
plt.show()
```
## 下载这个示例
- [下载python源码: transparent_legends.py](https://matplotlib.org/_downloads/transparent_legends.py)
- [下载Jupyter notebook: transparent_legends.ipynb](https://matplotlib.org/_downloads/transparent_legends.ipynb)