You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 4.1.2:线形图
## 绘制线形图
在所有图形中,最简单的应该就是线性方程`y = f (x)` 的可视化了。来看看如何创建这个简单的线形图。要画`Matplotlib`图形时,都需要先创建一个图形`fig` 和一个坐标轴`ax`。创建图形与坐标轴的最简单做法是:
```python
import matplotlib.pyplot as plt#导入模块
plt.style.use('seaborn-whitegrid')#设置matplotlib画图样式
fig = plt.figure()
ax = plt.axes()
```
![](2.jpg)
在`Matplotlib`中,`figure`(`plt.Figure`类的一个实例)可以被看成是个能够容纳各种坐标轴、图形、文字和标签的容器。就像你在图中看到的那样,`axes`(`plt.Axes`类的一个实例)是一个带有刻度和标签的矩形,最终会包含所有可视化的图形元素。在这里我们一般使用变量`fig`表示一个图形实例,用变量`ax`表示一个坐标轴实例。
接下来使用`ax.plot`画图,从简单的正弦曲线开始:
```python
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x))
```
![](3.jpg)
也可以使用`pylab`接口画图,这时图形与坐标轴都在底层执行,执行结果和上图一样:
```python
plt.plot(x, np.sin(x))
```
试想下如果我们重复调用`plot`命令会发生什么,它会在一张图中创建多条线:
```python
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
```
![](4.jpg)
## 设置颜色和风格
在画图的过程中通常对图形的第一次调整是调整它线条的颜色与风格。`plt.plot()`函数可以通过相应的参数设置颜色和风格,修改颜色使用`color`参数,它支持各种颜色值的字符串,具体使用如下:
```python
plt.plot(x, np.sin(x - 0), color='blue') # 标准颜色名称
plt.plot(x, np.sin(x - 1), color='g') # 缩写颜色代码rgbcmyk
plt.plot(x, np.sin(x - 2), color='0.75') # 范围在0~1的灰度值
plt.plot(x, np.sin(x - 3), color='#FFDD44') # 十六进制RRGGBB00~FF
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB元组范围在0~1
plt.plot(x, np.sin(x - 5), color='chartreuse') # HTML颜色名称
```
![](5.jpg)
常用颜色对应值:
| 取值 |颜色 |
| :------------: | :------------: |
|b|blue|
|g|green|
|r|red|
|c|cyan|
|m|magenta|
|y|yellow|
|k|black|
|w|white|
如果不指定颜色,`matplotlib`会为多条线自动循环使用一组默认的颜色。设置样式使用`linestyle`参数:
```python
plt.plot(x, x + 0, linestyle='solid')
plt.plot(x, x + 1, linestyle='dashed')
plt.plot(x, x + 2, linestyle='dashdot')
plt.plot(x, x + 3, linestyle='dotted')
#也可以用下面的简写形式
plt.plot(x, x + 4, linestyle='-') # 实线
plt.plot(x, x + 5, linestyle='--') # 虚线
plt.plot(x, x + 6, linestyle='-.') # 点划线
plt.plot(x, x + 7, linestyle=':') # 实点线
```
![](6.jpg)
还可以将`linestyle`和`color`编码组合起来,作为`plt.plot()`函数的一个非关键字参数使用:
```python
plt.plot(x, x + 0, '-g') # 绿色实线
plt.plot(x, x + 1, '--c') # 青色虚线
plt.plot(x, x + 2, '-.k') # 黑色点划线
plt.plot(x, x + 3, ':r'); # 红色实点线
```
![](7.jpg)
## 设置坐标轴上下限
虽然`matplotlib`会自动为你的图形选择最合适的坐标轴上下限,但是有时自定义坐标轴上下线可能会更好。调整坐标轴上下限最基础的方式是`plt.xlim()`和`plt.ylim()`:
```python
plt.plot(x, np.sin(x))
plt.xlim(-1, 11)
plt.ylim(-1.5, 1.5)
```
![](8.jpg)
如果你想要让坐标轴逆序显示,那么只需要逆序设置坐标轴刻度值就可以了。`matplotlib`还有一个方法是`plt.axis()`。通过传入[`xmin``xmax``ymin``ymax`]对应的值,这样就可以用一行代码设置`x`和`y`的限值:
```python
plt.plot(x, np.sin(x))
plt.axis([-1, 11, -1.5, 1.5])
```
![](9.jpg)
还支持按照图形的内容自动收紧坐标轴,不留空白区域:
```python
plt.plot(x, np.sin(x))
plt.axis('tight')
```
![](10.jpg)
## 设置图形标签
图形标签与坐标轴标题是最简单的标签,设置方法如下:
```python
plt.plot(x, np.sin(x))
plt.title("A Sine Curve")
plt.xlabel("x")
plt.ylabel("sin(x)");
```
![](11.jpg)