up cp3
This commit is contained in:
@@ -10,7 +10,6 @@ kernelspec:
|
||||
# 第三回:布局格式定方圆
|
||||
|
||||
|
||||
|
||||
```{code-cell} ipython3
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
@@ -23,7 +22,7 @@ plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
|
||||
|
||||
### 1. 使用 `plt.subplots` 绘制均匀状态下的子图
|
||||
|
||||
返回元素分别是画布和子图构成的列表,第一个数字为行,第二个为列
|
||||
返回元素分别是画布和子图构成的列表,第一个数字为行,第二个为列,不传入时默认值都为1
|
||||
|
||||
`figsize` 参数可以指定整个画布的大小
|
||||
|
||||
@@ -47,8 +46,34 @@ fig.tight_layout()
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
`subplots`是基于OO模式的写法,显式创建一个或多个axes对象,然后在对应的子图对象上进行绘图操作。
|
||||
还有种方式是使用`subplot`这样基于pyplot模式的写法,每次在指定位置新建一个子图,并且之后的绘图操作都会指向当前子图,本质上`subplot`也是`Figure.add_subplot`的一种封装。
|
||||
|
||||
在调用`subplot`时一般需要传入三位数字,分别代表总行数,总列数,当前子图的index
|
||||
|
||||
|
||||
```{code-cell} ipython3
|
||||
plt.figure()
|
||||
# 子图1
|
||||
plt.subplot(2,2,1)
|
||||
plt.plot([1,2], 'r')
|
||||
# 子图2
|
||||
plt.subplot(2,2,2)
|
||||
plt.plot([1,2], 'b')
|
||||
#子图3
|
||||
plt.subplot(224) # 当三位数都小于10时,可以省略中间的逗号,这行命令等价于plt.subplot(2,2,4)
|
||||
plt.plot([1,2], 'g');
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
除了常规的直角坐标系,也可以通过`projection`方法创建极坐标系下的图表
|
||||
|
||||
@@ -66,6 +91,11 @@ plt.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75);
|
||||
```
|
||||
|
||||
|
||||
```{admonition} 练一练
|
||||
<p>请思考如何用极坐标系画出类似的玫瑰图</p>
|
||||
<img src="http://www.hinews.cn/news/pic/003/205/569/00320556959_f01764d0.jpg" width="300" align="bottom" />
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -92,7 +122,9 @@ fig.tight_layout()
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
在上面的例子中出现了 `spec[i, j]` 的用法,事实上通过切片就可以实现子图的合并而达到跨图的共能
|
||||
@@ -121,39 +153,14 @@ fig.tight_layout()
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 二、子图上的方法
|
||||
|
||||
在 `ax` 对象上定义了和 `plt` 类似的图形绘制函数,常用的有: `plot, hist, scatter, bar, barh, pie`
|
||||
|
||||
|
||||
```{code-cell} ipython3
|
||||
fig, ax = plt.subplots(figsize=(4,3))
|
||||
ax.plot([1,2],[2,1]);
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
```{code-cell} ipython3
|
||||
fig, ax = plt.subplots(figsize=(4,3))
|
||||
ax.hist(np.random.randn(1000));
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
补充介绍一些子图上的方法
|
||||
|
||||
常用直线的画法为: `axhline, axvline, axline` (水平、垂直、任意方向)
|
||||
|
||||
@@ -166,91 +173,64 @@ ax.axline([0.3,0.3],[0.7,0.7]);
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
使用 `grid` 可以加灰色网格
|
||||
|
||||
|
||||
```{code-cell} ipython3
|
||||
fig, ax = plt.subplots(figsize=(4,3))
|
||||
ax.grid(True)
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
使用 `set_xscale, set_title, set_xlabel` 分别可以设置坐标轴的规度(指对数坐标等)、标题、轴名
|
||||
使用 `set_xscale` 可以设置坐标轴的规度(指对数坐标等)
|
||||
|
||||
|
||||
```{code-cell} ipython3
|
||||
fig, axs = plt.subplots(1, 2, figsize=(10, 4))
|
||||
fig.suptitle('大标题', size=20)
|
||||
for j in range(2):
|
||||
axs[j].plot(list('abcd'), [10**i for i in range(4)])
|
||||
if j==0:
|
||||
axs[j].set_yscale('log')
|
||||
axs[j].set_title('子标题1')
|
||||
axs[j].set_ylabel('对数坐标')
|
||||
else:
|
||||
axs[j].set_title('子标题1')
|
||||
axs[j].set_ylabel('普通坐标')
|
||||
pass
|
||||
fig.tight_layout()
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 思考题
|
||||
|
||||
- 墨尔本1981年至1990年的每月温度情况
|
||||
|
||||
数据集来自github仓库下data/layout_ex1.csv
|
||||
请利用数据,画出如下的图:
|
||||
|
||||
<img src="https://s1.ax1x.com/2020/11/01/BwvCse.png" width="800" align="bottom" />
|
||||
|
||||
|
||||
|
||||
与一般的 `plt` 方法类似, `legend, annotate, arrow, text` 对象也可以进行相应的绘制
|
||||
- 画出数据的散点图和边际分布
|
||||
|
||||
用 `np.random.randn(2, 150)` 生成一组二维数据,使用两种非均匀子图的分割方法,做出该数据对应的散点图和边际分布图
|
||||
|
||||
```{code-cell} ipython3
|
||||
fig, ax = plt.subplots()
|
||||
ax.arrow(0, 0, 1, 1, head_width=0.03, head_length=0.05, facecolor='red', edgecolor='blue')
|
||||
ax.text(x=0, y=0,s='这是一段文字', fontsize=16, rotation=70, rotation_mode='anchor', color='green')
|
||||
ax.annotate('这是中点', xy=(0.5, 0.5), xytext=(0.8, 0.2), arrowprops=dict(facecolor='yellow', edgecolor='black'), fontsize=16);
|
||||
```
|
||||
<img src="https://s1.ax1x.com/2020/11/01/B0pEnS.png" width="400" height="400" align="bottom" />
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
```{code-cell} ipython3
|
||||
fig, ax = plt.subplots()
|
||||
ax.plot([1,2],[2,1],label="line1")
|
||||
ax.plot([1,1],[1,2],label="line1")
|
||||
ax.legend(loc=1);
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
其中,图例的 `loc` 参数如下:
|
||||
|
||||
| string | code |
|
||||
| ---- | ---- |
|
||||
| best | 0 |
|
||||
| upper right | 1 |
|
||||
| upper left | 2 |
|
||||
| lower left | 3 |
|
||||
| lower right | 4 |
|
||||
| right | 5 |
|
||||
| center left | 6 |
|
||||
| center right | 7 |
|
||||
| lower center | 8 |
|
||||
| upper center | 9 |
|
||||
| center | 10 |
|
||||
|
||||
|
||||
## 参考资料
|
||||
|
||||
[1.matplotlib官网布局使用指南](https://matplotlib.org/stable/tutorials/intermediate/gridspec.html#sphx-glr-tutorials-intermediate-gridspec-py)
|
||||
Reference in New Issue
Block a user