Matplotlib 简明教程
Matplotlib - Environment Setup
Matplotlib 库与各种操作系统和 Python 环境高度兼容。设置 Matplotlib 相对简单,其多功能性使其成为在 Python 中可视化数据的宝贵工具。它包括确保在我们的 Python 环境中安装并配置其行为。
Matplotlib llibrary is highly compatible with various operating systems and Python environments. Setting up Matplotlib is relatively straightforward and its versatility makes it a valuable tool for visualizing data in Python. It involves ensuring that it is installed and configuring its behavior within our Python environment.
以下是设置 Matplotlib 库环境的分步指南。
The below is the step-by-step guide to set the environment of the matplotlib library.
Installation
Matplotlib 通常包含在 Python 发行版(如 Anaconda)中。但是,如果没有安装我们,可以使用 pip 进行安装。以下是安装 matplotlib 库的命令。
Matplotlib is often included in Python distributions like Anaconda. However if it’s not installed we can do so using pip. The following is the command to install the matplotlib library.
pip install matplotlib
Checking the Installation
如果我们想要验证安装是否完成,则打开 Python 解释器或 Jupyter 笔记本并使用以下代码行导入 Matplotlib 库 pyplot 模块。
If we want to verify whether the installation is done or not then open a Python interpreter or a Jupyter Notebook and import Matplotlib library pyplot module by using the below code line.
import matplotlib.pyplot as plt
如果没有发生错误,则安装成功,否则安装过程出现问题。
If no errors occur then the installation is successful otherwise there is a trouble in installation.
Backend Selection
Matplotlib 具有负责渲染绘制的不同“后端”。这些后端可以在不同的环境中显示图形,例如在 Jupyter 笔记本中单独窗口中。
Matplotlib has different "backends" responsible for rendering the plots. These backends can display figures in different environments e.g. in a Jupyter Notebook a separate window etc.
Interactive Backends (Great for Jupyter Notebook)
为了在 Jupyter 笔记本中启用交互式绘图,我们使用魔术命令 %matplotlib。以下是待执行的代码行。
For enabling interactive plotting within Jupyter Notebook, we use the magic command %matplotlib. The below is the code line to be executed.
%matplotlib inline
# or
%matplotlib notebook
%matplotlib inline 命令在笔记本中显示我们的绘图的静态图像,而 %matplotlib notebook 允许交互式绘图,如平移和缩放。
The %matplotlib inline command displays static images of our plot in the notebook while %matplotlib notebook allows interactive plots such as panning and zooming.
Non-Interactive Backend (when not using Jupyter)
当不在 Jupyter 环境中工作时,Matplotlib 可以使用非交互式后端。它会针对我们的系统自动选择合适的后端。我们可以明确设置后端。
When not working within a Jupyter environment then Matplotlib can use non-interactive backends. It automatically selects a suitable backend for our system. We can set a backend explicitly.
import matplotlib
matplotlib.use('Agg') # Backend selection, 'Agg' for non-interactive backend
Configuration and Style
Matplotlib 允许自定义默认设置和样式。我们可以创建一个名为 matplotlibrc
的配置文件来自定义行为。
Matplotlib allows customization of default settings and styles. We can create a configuration file named matplotlibrc
to customize the behavior.
Locating the Configuration File
为了找到我们的 Matplotlib 配置文件所在的位置,我们可以使用 Python 编辑器运行以下代码。
To find where our Matplotlib configuration file is located we can run the below code using the Python editor.
import matplotlib
matplotlib.matplotlib_fname()
Creating/Editing Configuration
为了确保一切都正确设置,我们可以使用 Matplotlib 创建一个简单绘图。
To ensure everything is set up correctly we can create a simple plot using Matplotlib.
import matplotlib.pyplot as plt
x = [i2 for i in range(2,30)]
y = [i3 for i in range(2,30)]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Test Plot')
plt.show()
运行此代码应在选定环境中显示带有折线图的简单绘图。
Running this code should display a simple plot with a line chart in the selected environment.
如果所有用户都未安装 Python 2.7 或 3.4 版本,则需要安装 Microsoft Visual C 2008(64 位或 32 位(用于 Python 2.7))或 Microsoft Visual C 2010(64 位或 32 位(用于 Python 3.4))可再发行包。
Incase Python 2.7 or 3.4 versions are not installed for all users, the Microsoft Visual C 2008 (64 bit or 32 bit forPython 2.7) or Microsoft Visual C 2010 (64 bit or 32 bit for Python 3.4) redistributable packages need to be installed.
如果你在 Mac 上使用 Python 2.7,请执行以下命令 −
If you are using Python 2.7 on a Mac, execute the following command −
xcode-select -install
执行上述命令时,可能会编译一个依赖项 subprocess32。
Upon execution of the above command, the subprocess32 - a dependency, may be compiled.
对于非常古老的 Linux 和 Python 2.7 版本,你可能需要安装 subprocess32 的主版本。
On extremely old versions of Linux and Python 2.7, you may need to install the master version of subprocess32.
Matplotlib 要求大量依赖项 −
Matplotlib requires a large number of dependencies −
-
Python (>= 2.7 or >= 3.4)
-
NumPy
-
setuptools
-
dateutil
-
pyparsing
-
libpng
-
pytz
-
FreeType
-
cycler
-
six
或者,你还可以安装多个程序包以启用更好的用户界面工具包。
Optionally, you can also install a number of packages to enable better user interface toolkits.
-
tk
-
PyQt4
-
PyQt5
-
pygtk
-
wxpython
-
pycairo
-
Tornado
为了更好地支持动画输出格式和图像文件格式,LaTeX 等,你可以安装下列内容 −
For better support of animation output format and image file formats, LaTeX, etc., you can install the following −
-
_mpeg/avconv
-
ImageMagick
-
Pillow (>=2.0)
-
LaTeX and GhostScript (for rendering text with LaTeX).
-
LaTeX and GhostScript (for rendering text with LaTeX).