Numpy 简明教程
NumPy - Matplotlib
Matplotlib 是 Python 的绘图库。它与 NumPy 一起使用,以提供一个作为 MatLab 的有效开源替代方案的环境。它还可与诸如 PyQt 和 wxPython 的图形工具包一起使用。
Matplotlib is a plotting library for Python. It is used along with NumPy to provide an environment that is an effective open source alternative for MatLab. It can also be used with graphics toolkits like PyQt and wxPython.
Matplotlib 模块最早是由 John D. Hunter 编写的。自 2012 年以来,Michael Droettboom 一直是其主要开发者。当前,Matplotlib ver. 1.5.1 是可用的稳定版本。该软件包既以二进制发行版形式提供,又以源代码形式在 www.matplotlib.org 上提供。
Matplotlib module was first written by John D. Hunter. Since 2012, Michael Droettboom is the principal developer. Currently, Matplotlib ver. 1.5.1 is the stable version available. The package is available in binary distribution as well as in the source code form on www.matplotlib.org.
传统上,通过添加以下语句将包导入 Python 脚本:
Conventionally, the package is imported into the Python script by adding the following statement −
from matplotlib import pyplot as plt
pyplot() 是 matplotlib 库中最重要函数,用于绘制 2D 数据。以下脚本绘制方程式 y = 2x + 5
Here pyplot() is the most important function in matplotlib library, which is used to plot 2D data. The following script plots the equation y = 2x + 5
Example
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() function 创建为 x axis 上的值。 y axis 上的相应值存储在另一个 ndarray object y 中。使用 matplotlib 包的 pyplot 子模块的 plot() 函数绘制这些值。
An ndarray object x is created from np.arange() function as the values on the x axis. The corresponding values on the y axis are stored in another ndarray object y. These values are plotted using plot() function of pyplot submodule of matplotlib package.
通过 show() 函数显示图形表示。
The graphical representation is displayed by show() function.
以上代码应会产生以下输出-
The above code should produce the following output −
可以使用格式字符串将离散值添加到 plot() 函数中,来代替线性图。可以使用以下格式化字符。
Instead of the linear graph, the values can be displayed discretely by adding a format string to the plot() function. Following formatting characters can be used.
Sr.No. |
Character & Description |
1 |
'-' Solid line style |
2 |
'--' Dashed line style |
3 |
'-.' Dash-dot line style |
4 |
':' Dotted line style |
5 |
'.' Point marker |
6 |
',' Pixel marker |
7 |
'o' Circle marker |
8 |
'v' Triangle_down marker |
9 |
'^' Triangle_up marker |
10 |
'<' Triangle_left marker |
11 |
'>' Triangle_right marker |
12 |
'1' Tri_down marker |
13 |
'2' Tri_up marker |
14 |
'3' Tri_left marker |
15 |
'4' Tri_right marker |
16 |
's' Square marker |
17 |
'p' Pentagon marker |
18 |
''* Star marker |
19 |
'h' Hexagon1 marker |
20 |
'H' Hexagon2 marker |
21 |
'+' Plus marker |
22 |
'x' X marker |
23 |
'D' Diamond marker |
24 |
'd' Thin_diamond marker |
25 |
*' |
'* Vline marker |
26 |
还定义了以下颜色缩写。
The following color abbreviations are also defined.
Character |
Color |
'b' |
Blue |
'g' |
Green |
'r' |
Red |
'c' |
Cyan |
'm' |
Magenta |
'y' |
Yellow |
'k' |
Black |
'w' |
White |
要显示代表点的圆形(而不是上面示例中的线条),请使用 “ob” 作为 plot() 函数中的格式字符串。
To display the circles representing points, instead of the line in the above example, use “ob” as the format string in plot() function.
Example
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()
以上代码应会产生以下输出-
The above code should produce the following output −
Sine Wave Plot
以下脚本使用 matplotlib 生成 sine wave plot 。
The following script produces the sine wave plot using matplotlib.
subplot()
subplot() 函数允许您在同一张图中绘制不同的内容。在以下脚本中,绘制的是 sine 和 cosine values 。
The subplot() function allows you to plot different things in the same figure. In the following script, sine and cosine values are plotted.
Example
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()
以上代码应会产生以下输出-
The above code should produce the following output −
bar()
pyplot submodule 提供 bar() 函数来生成条形图。以下示例生成两个组的 x 和 y 数组的条形图。
The pyplot submodule provides bar() function to generate bar graphs. The following example produces the bar graph of two sets of x and y arrays.
Example
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()
此代码应产生以下输出-
This code should produce the following output −