Python Data Science 简明教程

Python Data Science - Matplotlib

What is Matplotlib?

Matplotlib 是一个 Python 库,用于通过使用 Python 脚本创建 2D 图形和绘图。它有一个名为 pyplot 的模块,它通过提供控制线样式、字体属性、格式化轴等的功能,简化了绘图。它支持各种各样的图形和绘图,即:直方图、条形图、功率谱、误差图等。它与 NumPy 一起使用,提供了一个有效的 MatLab 开源替代环境。它还可以与 PyQt 和 wxPython 等图形工具包一起使用。

Matplotlib is a python library used to create 2D graphs and plots by using python scripts. It has a module named pyplot which makes things easy for plotting by providing feature to control line styles, font properties, formatting axes etc. It supports a very wide variety of graphs and plots namely - histogram, bar charts, power spectra, error charts etc. 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.

传统上,通过添加以下语句将包导入 Python 脚本:

Conventionally, the package is imported into the Python script by adding the following statement −

from matplotlib import pyplot as plt

Matplotlib Example

以下脚本使用 matplotlib 生成 sine wave plot

The following script produces the sine wave plot using matplotlib.

Example

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()

它的 output 如下所示 −

Its output is as follows −

sine wave

我们在后续章节中将看到大量关于如何在数据科学工作中使用 Python 的 Matplotlib 库的示例。

We will see lots of examples on using Matplotlib library of python in Data science work in the next chapters.