After Width: | Height: | Size: 80 KiB |
After Width: | Height: | Size: 58 KiB |
After Width: | Height: | Size: 28 KiB |
After Width: | Height: | Size: 31 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 45 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 43 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 8.3 KiB |
After Width: | Height: | Size: 8.7 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 27 KiB |
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 45 KiB |
After Width: | Height: | Size: 23 KiB |
@ -0,0 +1,30 @@
|
||||
# 4.1.6:手动创建子图
|
||||
|
||||
## plt.axes创建子图
|
||||
|
||||
前面已经介绍过`plt.axes`函数,这个函数默认配置是创建一个标准的坐标轴,填满整张图。它还有一个可选的参数,由图形坐标系统的四个值构成。这四个值表示为坐标系的[底坐标、左坐标、宽度、高度],数值的取值范围为左下角为`0`,右上角为`1`。
|
||||
|
||||
下面演示在右上角创建一个画中画:
|
||||
```python
|
||||
x1 = plt.axes() # 默认坐标轴
|
||||
ax2 = plt.axes([0.65, 0.65, 0.2, 0.2])
|
||||
```
|
||||
|
||||

|
||||
|
||||
## fig.add_axes()创建子图
|
||||
面向对象画图接口中类似的命令由`fig.add_axes()`。用这个命令创建两个竖直排列的坐标轴:
|
||||
```python
|
||||
fig = plt.figure()
|
||||
ax1 = fig.add_axes([0.1, 0.5, 0.8, 0.4],
|
||||
xticklabels=[], ylim=(-1.2, 1.2))
|
||||
ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.4],
|
||||
ylim=(-1.2, 1.2))
|
||||
x = np.linspace(0, 10)
|
||||
ax1.plot(np.sin(x))
|
||||
ax2.plot(np.cos(x));
|
||||
```
|
||||
|
||||

|
||||
|
||||
可以看到两个紧挨着的坐标轴:上子图的(起点`y`坐标为`0.5`位置)与下子图`x`轴刻度是对应的(起点 y 坐标为 0.1,高度为 0.4)。
|
After Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 67 KiB |
After Width: | Height: | Size: 40 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 39 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 4.9 KiB |
After Width: | Height: | Size: 35 KiB |
After Width: | Height: | Size: 155 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 9.8 KiB |
After Width: | Height: | Size: 108 KiB |
After Width: | Height: | Size: 7.9 KiB |
After Width: | Height: | Size: 52 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 29 KiB |
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 52 KiB |
After Width: | Height: | Size: 67 KiB |
After Width: | Height: | Size: 67 KiB |
After Width: | Height: | Size: 67 KiB |
After Width: | Height: | Size: 80 KiB |
After Width: | Height: | Size: 7.1 KiB |
After Width: | Height: | Size: 60 KiB |
After Width: | Height: | Size: 86 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 85 KiB |
@ -0,0 +1,119 @@
|
||||
# 4.2.3:自定义坐标刻度
|
||||
|
||||
## 主次要刻度
|
||||
学习前最好先对`matplotlib`图形的对象层级有深入了解。`matplotlib`的`figure`对象是一个盛放图形元素的包围盒。可以将每个`matplotlib`对象都看成是子对象的容器,每个`figure`都包含`axes`对象,每个`axes`对象又包含其他表示图形内容的对象,比如`xaxis/yaxis`,每个属性包含构成坐标轴的线条、刻度和标签的全部属性。
|
||||
|
||||
每一个坐标轴都有主次要刻度,主要刻度要比次要刻度更大更显著,而次要刻度往往更小。
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
ax = plt.axes(xscale='log', yscale='log')
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
可以看到主要刻度都显示为一个较大的刻度线和标签,而次要刻度都显示为一个较小的可读性,不显示标签。
|
||||
|
||||
## 隐藏刻度与标签
|
||||
最常用的刻度/标签格式化操作可能就是隐藏刻度与标签了,可以通过`plt.NullLocator()`和`plt.NullFormatter()`实现。
|
||||
|
||||
示例如下:
|
||||
```python
|
||||
ax = plt.axes()
|
||||
ax.plot(np.random.rand(50))
|
||||
ax.yaxis.set_major_locator(plt.NullLocator())
|
||||
ax.xaxis.set_major_formatter(plt.NullFormatter())
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
这里`x`轴的标签隐藏了但是保留了刻度线,`y`轴的刻度和标签都隐藏了。有的图片中都不需要刻度线,比如下面这张包含人脸的图形:
|
||||
|
||||
```python
|
||||
fig, ax = plt.subplots(5, 5, figsize=(5, 5))
|
||||
fig.subplots_adjust(hspace=0, wspace=0)
|
||||
# 从scikit-learn获取一些人脸照片数据
|
||||
from sklearn.datasets import fetch_olivetti_faces
|
||||
faces = fetch_olivetti_faces().images
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
ax[i, j].xaxis.set_major_locator(plt.NullLocator())
|
||||
ax[i, j].yaxis.set_major_locator(plt.NullLocator())
|
||||
ax[i, j].imshow(faces[10 * i + j], cmap="bone")
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 花哨的刻度格式
|
||||
|
||||
`matplotlib`默认的刻度格式可以满足大部分的需求。虽然默认配置已经很不错了,但是有时候可能需要更多的功能,比如正弦曲线和余弦曲线,默认情况下刻度为整数,如果将刻度与网格线画在`π`的倍数上图形会更加自然,可以通过设置一个`multipleLocator`来实现将刻度放在你提供的数值倍数上:
|
||||
|
||||
```python
|
||||
fig, ax = plt.subplots()
|
||||
x = np.linspace(0, 3 * np.pi, 1000)
|
||||
ax.plot(x, np.sin(x), lw=3, label='Sine')
|
||||
ax.plot(x, np.cos(x), lw=3, label='Cosine')
|
||||
# 设置网格、图例和坐标轴上下限
|
||||
ax.grid(True)
|
||||
ax.legend(frameon=False)
|
||||
ax.axis('equal')
|
||||
ax.set_xlim(0, 3 * np.pi)
|
||||
|
||||
ax.xaxis.set_major_locator(plt.MultipleLocator(np.pi / 2))
|
||||
ax.xaxis.set_minor_locator(plt.MultipleLocator(np.pi / 4))
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
`matplotlib`还支持用数学符号来做刻度,在数学表达式两侧加上美元符号`$`,这样就可以方便地显示数学符号和数学公式。可以用`plt.FuncFormatter`来实现,用一个自定义函数设置不同刻度标签的显示:
|
||||
```python
|
||||
def format_func(value, tick_number):
|
||||
# 找到π/2的倍数刻度
|
||||
N = int(np.round(2 * value / np.pi))
|
||||
if N == 0:
|
||||
return "0"
|
||||
elif N == 1:
|
||||
return r"$\pi/2$"
|
||||
elif N == 2:
|
||||
return r"$\pi$"
|
||||
elif N % 2 > 0:
|
||||
return r"${0}\pi/2$".format(N)
|
||||
else:
|
||||
return r"${0}\pi$".format(N // 2)
|
||||
ax.xaxis.set_major_formatter(plt.FuncFormatter(format_func))
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 格式生成器与定位器
|
||||
|
||||
前面已经介绍了一些格式生成器和定位器,这里再用表格简单总结一些内置的格式生成器和定位器:
|
||||
|
||||
| 定位器 |描述 |
|
||||
| :------------: | :------------: |
|
||||
|NullLocator |无刻度 |
|
||||
| FixedLocator | 刻度位置固定 |
|
||||
| IndexLocator | 用索引作为定位器 |
|
||||
| LinearLocator | 从min 到max 均匀分布刻度 |
|
||||
| LogLocator |从min 到max 按对数分布刻度 |
|
||||
|MultipleLocator |刻度和范围都是基数的倍数 |
|
||||
|MaxNLocator |为最大刻度找到最优位置 |
|
||||
|AutoMinorLocator | 次要刻度的定位器 |
|
||||
|
||||
|格式生成器 |描述 |
|
||||
| :------------: | :------------: |
|
||||
|NullFormatter |刻度上无标签 |
|
||||
|IndexFormatter | 将一组标签设置为字符串 |
|
||||
|FixedFormatter |手动为刻度设置标签 |
|
||||
|FuncFormatter |用自定义函数设置标签 |
|
||||
|FormatStrFormatter |为每个刻度值设置字符串格式 |
|
||||
|ScalarFormatter |为标量值设置标签 |
|
||||
|LogFormatter |对数坐标轴的默认格式生成器 |
|
||||
|
||||
|
@ -0,0 +1,117 @@
|
||||
# 4.2.4:配置文件与样式表
|
||||
|
||||
## 配置图形
|
||||
|
||||
我们可以通过修个单个图形配置,使得最终图形比原来的图形更好看。可以为每个单独的图形进行个性化设置。这里我们通过手动调整,将`matplotlib`土到掉渣的默认直方图修改成美图:
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
plt.style.use('classic')
|
||||
import numpy as np
|
||||
|
||||
x = np.random.randn(1000)
|
||||
plt.hist(x);
|
||||
|
||||
# 用灰色背景
|
||||
ax = plt.axes(facecolor='#E6E6E6')
|
||||
ax.set_axisbelow(True)
|
||||
|
||||
# 画上白色的网格线
|
||||
plt.grid(color='w', linestyle='solid')
|
||||
|
||||
# 隐藏坐标轴的线条
|
||||
for spine in ax.spines.values():
|
||||
spine.set_visible(False)
|
||||
ax.xaxis.tick_bottom()
|
||||
ax.yaxis.tick_left()
|
||||
|
||||
# 弱化刻度与标签
|
||||
ax.tick_params(colors='gray', direction='out')
|
||||
for tick in ax.get_xticklabels():
|
||||
tick.set_color('gray')
|
||||
for tick in ax.get_yticklabels():
|
||||
tick.set_color('gray')
|
||||
|
||||
# 设置频次直方图轮廓色与填充色
|
||||
ax.hist(x, edgecolor='#E6E6E6', color='#EE6666')
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 修改默认配置rcParams
|
||||
|
||||
通过手动配置确实能达到我们想要的效果,但是如有很多个图形,我们肯定不希望对每一个图都这样手动配置一番。`matplotlib`作为一个强大的工具当然有方法可以让我们只配置一次默认图形,就可以应用到所有图形上。这个方法就是通过修改默认配置`rcParams`。`matplotlib`在每次加载的时候,都会定义一个运行时配置`rc`,其中包含了我们创建的图形元素的默认风格。
|
||||
|
||||
```python
|
||||
from matplotlib import cycler
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
colors = cycler('color',['#EE6666', '#3388BB', '#9988DD','#EECC55', '#88BB44', '#FFBBBB'])
|
||||
|
||||
plt.rc('axes', facecolor='#E6E6E6', edgecolor='none',axisbelow=True, grid=True, prop_cycle=colors)
|
||||
|
||||
plt.rc('grid', color='w', linestyle='solid')
|
||||
|
||||
plt.rc('xtick', direction='out', color='gray')
|
||||
|
||||
plt.rc('ytick', direction='out', color='gray')
|
||||
|
||||
plt.rc('patch', edgecolor='#E6E6E6')
|
||||
|
||||
plt.rc('lines', linewidth=2)
|
||||
|
||||
x = np.random.randn(1000)
|
||||
|
||||
plt.hist(x)#画直方图
|
||||
plt.show()
|
||||
|
||||
for i in range(4):
|
||||
plt.plot(np.random.rand(10))#折线图
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
所有`rc`设置都存储在一个名为`matplotlib.rcParams`的类字典变量中,可以通过这个变量来查看我们的配置。`rc`的第一个参数是希望自定义的对象,如`figure`、`axes`、`grid`等。其后可以跟上一系列的关键字参数。
|
||||
|
||||
## 样式表
|
||||
|
||||
`matplotlib`从`1.4`版本中增加了一个非常好用的`style`模块,里面包含了大量的新式默认样式表,还支持创建和打包自己的风格。通过`plt.style.available`命令可以看到所有可用的风格。
|
||||
```python
|
||||
plt.style.available[:5]#查看前5个风格样式
|
||||
'''
|
||||
输出:['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight']
|
||||
'''
|
||||
```
|
||||
|
||||
使用某种样式表的基本方法为`plt.style.use('stylename')`,这样就改变后面代码的所有风格。支持组合样式,通过传递样式列表可以轻松组合这些样式。如果需要,也可以使用风格上下文管理器临时更换风格:
|
||||
|
||||
```python
|
||||
with plt.style.context('stylename'):
|
||||
make_a_plot()
|
||||
```
|
||||
首先创建一个画两种基本图形的函数:
|
||||
```python
|
||||
def hist_and_lines():
|
||||
np.random.seed(0)
|
||||
fig, ax = plt.subplots(1, 2, figsize=(11, 4))
|
||||
ax[0].hist(np.random.randn(1000))
|
||||
for i in range(3):
|
||||
ax[1].plot(np.random.rand(10))
|
||||
ax[1].legend(['a', 'b', 'c'], loc='lower left')
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
再通过修改风格绘制图形:
|
||||
```python
|
||||
with plt.style.context('fivethirtyeight'):
|
||||
hist_and_lines()
|
||||
```
|
||||
|
||||

|
||||
|