- NumPy 教程
- NumPy - 主页
- NumPy - 简介
- NumPy - 环境
- NumPy - Ndarray 对象
- NumPy - 数据类型
- NumPy - 数组属性
- NumPy - 数组创建例程
- NumPy - 来自现有数据的数组
- 来自数值范围的数组
- NumPy - 索引和切片
- NumPy - 高级索引
- NumPy - 广播
- NumPy - 迭代数组
- NumPy - 数组操作
- NumPy - 二元运算符
- NumPy - 字符串函数
- NumPy - 数学函数
- NumPy - 算术运算
- NumPy - 统计函数
- 排序、搜索和计数功能
- NumPy - 字节交换
- NumPy - 副本和视图
- NumPy - 矩阵库
- NumPy - 线性代数
- NumPy-Matplotlib
- NumPy - 使用 Matplotlib 绘制直方图
- NumPy - 使用 NumPy 进行 I/O
- NumPy 有用资源
- NumPy - 快速指南
- NumPy - 有用的资源
- NumPy - 讨论
NumPy-Matplotlib
Matplotlib 是一个 Python 绘图库。它与 NumPy 一起使用,提供一个环境,作为 MatLab 的有效开源替代方案。它还可以与 PyQt 和 wxPython 等图形工具包一起使用。
Matplotlib 模块首先由 John D. Hunter 编写。自 2012 年起,Michael Droettboom 担任主要开发人员。目前,Matplotlib 版本。1.5.1 是可用的稳定版本。该软件包以二进制发行版以及www.matplotlib.org上的源代码形式提供。
按照惯例,通过添加以下语句将包导入到 Python 脚本中 -
from matplotlib import pyplot as plt
这里pyplot()是 matplotlib 库中最重要的函数,用于绘制 2D 数据。以下脚本绘制了方程y = 2x + 5
例子
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
plt.show()
ndarray 对象 x 是从np.arange() 函数创建的,作为x 轴上的值。y 轴上的相应值存储在另一个ndarray 对象 y中。这些值是使用matplotlib 包的 pyplot 子模块的plot()函数绘制的。
图形表示由show()函数显示。
上面的代码应该产生以下输出 -
通过向plot()函数添加格式字符串,可以离散显示值,而不是线性图。可以使用以下格式字符。
| 先生。 | 特征及描述 |
|---|---|
| 1 | '-' 实线样式 |
| 2 | '--' 虚线样式 |
| 3 | '-。' 点划线样式 |
| 4 | ':' 虚线样式 |
| 5 | '.' 点标记 |
| 6 | ',' 像素标记 |
| 7 | 'o' 圆形标记 |
| 8 | 'v' Triangle_down 标记 |
| 9 | '^' 向上三角形标记 |
| 10 | '<' Triangle_left 标记 |
| 11 | '>' Triangle_right 标记 |
| 12 | ‘1’ Tri_down 标记 |
| 13 | ‘2’ Tri_up 标记 |
| 14 | ‘3’ Tri_left 标记 |
| 15 | ‘4’ 三右标记 |
| 16 | 的 方形标记 |
| 17 号 | 'p' 五角大楼标记 |
| 18 | '*' 星标 |
| 19 | 'H' Hexagon1 标记 |
| 20 | 'H' Hexagon2 标记 |
| 21 | '+' 加号标记 |
| 22 | 'X' X标记 |
| 23 | 'D' 钻石标记 |
| 24 | 'd' Thin_diamond 标记 |
| 25 | '|' V线标记 |
| 26 | '_' 水平线标记 |
还定义了以下颜色缩写。
| 特点 | 颜色 |
|---|---|
| 'b' | 蓝色的 |
| 'G' | 绿色的 |
| 'r' | 红色的 |
| 'C' | 青色 |
| '我' | 品红 |
| '你' | 黄色的 |
| 'k' | 黑色的 |
| 'w' | 白色的 |
要显示代表点的圆,而不是上例中的线,请在plot()函数中使用“ob”作为格式字符串。
例子
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y,"ob")
plt.show()
上面的代码应该产生以下输出 -
正弦波图
以下脚本使用 matplotlib 生成正弦波图。
例子
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.title("sine wave form")
# Plot the points using matplotlib
plt.plot(x, y)
plt.show()
子图()
subplot() 函数允许您在同一个图中绘制不同的内容。在以下脚本中,绘制了正弦值和余弦值。
例子
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
plt.show()
上面的代码应该产生以下输出 -
酒吧()
pyplot子模块提供bar()函数来生成条形图。以下示例生成两组x和y数组的条形图。
例子
from matplotlib import pyplot as plt
x = [5,8,10]
y = [12,16,6]
x2 = [6,9,11]
y2 = [6,15,7]
plt.bar(x, y, align = 'center')
plt.bar(x2, y2, color = 'g', align = 'center')
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
该代码应产生以下输出 -
