|
| 1 | +# 安装 |
| 2 | + |
| 3 | +> 原文:[Installing](http://matplotlib.org/users/installing.html) |
| 4 | +
|
| 5 | +> 译者:[飞龙](https://github.com/) |
| 6 | +
|
| 7 | +> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) |
| 8 | +
|
| 9 | +有许多安装 matplotlib 的不同方法,最好的方法取决于您使用的操作系统,已经安装的内容以及如何使用它。 为了避免涉及本页上的所有细节(和潜在的复杂性),有几个方便的选项。 |
| 10 | + |
| 11 | +## 安装预构建包 |
| 12 | + |
| 13 | +### 多数平台:Python 科学分发包 |
| 14 | + |
| 15 | +第一个选项是使用已经内置 matplotlib 的预打包的 Python 分发包。 `Continuum.io` Python 分发包(Anaconda 或 miniconda)和 Enthought 分发包(Canopy)都是“在 Windows,OSX 和主流 Linux 平台开箱即用并正常工作”的出色选择。 这两个分发包包括 matplotlib 和许多其他有用的工具。 |
| 16 | + |
| 17 | +### Linux:使用你的包管理器 |
| 18 | + |
| 19 | +如果你是用 Linux,你可能更倾向于使用包管理器。matplotlib 是用于多数主流 Linux 发行版的包。 |
| 20 | + |
| 21 | ++ Debian / Ubuntu:`sudo apt-get install python-matplotlib` |
| 22 | ++ Fedora / Redhat:`sudo yum install python-matplotlib` |
| 23 | + |
| 24 | +Mac OSX:使用`pip` |
| 25 | + |
| 26 | +如果你使用 MacOS,你可以使用 Python 标准安装程序[`pip`](https://pypi.python.org/pypi/pip/)来安装 matplotlib 二进制。参见[安装 MacOS 二进制轮子](http://matplotlib.org/faq/installing_faq.html#install-osx-binaries)。 |
| 27 | + |
| 28 | +### Windows |
| 29 | + |
| 30 | +如果你还没有安装 Python,我们建议使用兼容 SciPy 技术栈的 Python 分发版本,如 WinPython,Python(x, y),Enthought Canopy 或 Continuum Anaconda,它们含有 matplotlib 和它的许多依赖,并预装了其他有用的软件包。 |
| 31 | + |
| 32 | +对于 [Python 的标准安装](https://www.python.org/downloads/),可以使用`pip`安装 matplotlib : |
| 33 | + |
| 34 | +``` |
| 35 | +python -m pip install -U pip setuptools |
| 36 | +python -m pip install matplotlib |
| 37 | +``` |
| 38 | + |
| 39 | +如果没有为所有用户安装 Python 2.7 或 3.4,则需要安装 Microsoft Visual C++ 2008(对于 Python 2.7 为 64 位或 32 位)或 Microsoft Visual C++ 2010(对于 Python 3.4 为 64 位或 32 位)再分发包。 |
| 40 | + |
| 41 | +Matplotlib 依赖于 Pillow 来读取和保存 JPEG,BMP 和 TIFF 图像文件。 Matplotlib 需要 MiKTeX 和 GhostScript 来使用LaTeX渲染文本。动画模块需要 FFmpeg,avconv,mencoder 或 ImageMagick。 |
| 42 | + |
| 43 | +以下后端应该开箱即用:agg,tkagg,ps,pdf 和 svg。对于其他后端,您可能需要安装 pycairo,PyQt4,PyQt5,PySide,wxPython,PyGTK,Tornado 或 GhostScript。 |
| 44 | + |
| 45 | +TkAgg 可能是来自标准 Python shell 或 IPython 的,用于交互式的最佳后端。它被启用为官方二进制文件的默认后端。 Windows 不支持 GTK3。 |
| 46 | + |
| 47 | +PyPI 下载页面上的 Windows 轮子(`* .whl`)不包含测试数据或示例代码。如果你想尝试 matplotlib 源代码中的许多演示,请下载`*.tar.gz`文件并查看`examples`子目录。要运行测试套件,请将源代码发行版中的`lib\matplotlib\tests`和`lib\mpl_toolkits\tests`目录分别复制到`sys.prefix\Lib\site-packages\matplotlib`和`sys.prefix\Lib\site-packages\mpl_toolkits`,并安装 nose,mock,Pillow,MiKTeX,GhostScript,ffmpeg,avconv,mencoder,ImageMagick 和 Inkscape。 |
| 48 | + |
| 49 | +## 从源码安装 |
| 50 | + |
| 51 | +如果你有兴趣为 matplotlib 开发做贡献,运行最新的源代码,或者只是想自己构建一切,从源代码构建 matplotlib 并不困难。 [从 PyPI 文件页面](https://pypi.python.org/pypi/matplotlib/)抓取最新的`tar.gz`发布文件,或者如果你想开发 matplotlib 或只需要最新的 bug 修复版本,获取最新的 git 版本,请见[从 git 安装](http://matplotlib.org/faq/installing_faq.html#install-from-git)。 |
| 52 | + |
| 53 | +源代码遵守标准环境变量 CC,CXX,PKG_CONFIG。 这意味着如果你的工具链有前缀,你可以设置它们。 这可以用于交叉编译。 |
| 54 | + |
| 55 | +``` |
| 56 | +export CC=x86_64-pc-linux-gnu-gcc export CXX=x86_64-pc-linux-gnu-g++ export PKG_CONFIG=x86_64-pc-linux-gnu-pkg-config |
| 57 | +``` |
| 58 | + |
| 59 | +一旦你满足的了面的具体需求(主要是 Python、NumPy、libpng 和 freetype),你就可以构建 matplotlib 了: |
| 60 | + |
| 61 | +``` |
| 62 | +cd matplotlib |
| 63 | +python setup.py build |
| 64 | +python setup.py install |
| 65 | +``` |
| 66 | + |
| 67 | +我们提供与`setup.py`一起使用的`setup.cfg`文件,您可以使用它来自定义构建过程。 例如,要使用的默认后端,是否安装 matplotlib 附带的某些可选库,等等。 这个文件会对那些包装 matplotlib 的东西特别有用。 |
| 68 | + |
| 69 | +如果已经为非标准设施安装了必备组件,并需要通知 matplotlib 它们在哪里,请编辑`setupext.py`并将基本路径添加为`sys.platform`的`basedir`字典条目。 例如,如果某些所需库的头文件位于`/some/path/include/someheader.h`中,请在您的平台的`basedir`列表中输入`/some/path`。 |
| 70 | + |
| 71 | +### 构建需求 |
| 72 | + |
| 73 | +这些是外部软件包,您需要在安装 matplotlib 之前安装它们。 如果您在 OSX 上构建,请参阅[在 OSX 上构建](http://matplotlib.org/users/installing.html#build-osx)。 如果您在 Windows 上构建,请参阅[在 Windows 上构建](http://matplotlib.org/users/installing.html#build-windows)。 如果在 Linux 上使用软件包管理器安装依赖项,则除了库本身之外,还可能需要安装开发包(查找`-dev`后缀)。 |
| 74 | + |
| 75 | +#### 所需依赖 |
| 76 | + |
| 77 | +Python 2.7,3.4,3.5 或 3.6 |
| 78 | + |
| 79 | +[下载 Python](https://www.python.org/downloads/) |
| 80 | + |
| 81 | +NumPy 1.7.1(或更新) |
| 82 | + |
| 83 | +Python 的数组支持([下载 NumPy](http://www.numpy.org/)) |
| 84 | + |
| 85 | +[setuptools](https://setuptools.readthedocs.io/en/latest/) |
| 86 | + |
| 87 | +setuptools 为 Python 包安装提供扩展 |
| 88 | + |
| 89 | +[dateutil](http://matplotlib.org/glossary/index.html#term-dateutil) 1.1 或更新 |
| 90 | + |
| 91 | +为 Python 时间日期的处理提供扩展。如果使用了`pip`,`easy_install `或者从源码安装,安装器会尝试从 PyPI 下载并安装`python_dateutil`。 |
| 92 | + |
| 93 | +[pyparsing](https://pyparsing.wikispaces.com/) |
| 94 | + |
| 95 | +需要为 matplotlib 的 mathtext 数学渲染提供支持。如果使用了`pip`,`easy_install `或者从源码安装,安装器会尝试从 PyPI 下载并安装`pyparsing`。 |
| 96 | + |
| 97 | + |
| 98 | +[libpng 1.2 (或更新)](http://www.libpng.org/) |
| 99 | + |
| 100 | +用于加载和保存 [PNG](http://matplotlib.org/glossary/index.html#term-png) 文件([下载](http://www.libpng.org/pub/png/libpng.html))。libong 需要 zlib。 |
| 101 | + |
| 102 | +[pytz](http://pytz.sourceforge.net/) |
| 103 | + |
| 104 | +用于操作时区感知的日期时间。<https://pypi.python.org/pypi/pytz> |
| 105 | + |
| 106 | +[FreeType](http://matplotlib.org/glossary/index.html#term-freetype) 2.3 或更新 |
| 107 | + |
| 108 | +用于读取 TrueType 字体文件。如果使用了`pip`,`easy_install `或者从源码安装,安装器会尝试从预期位置定位 FreeType。如果找病毒奥,尝试安装 [pkg-config](http://matplotlib.org/users/installing.html#optional-dependencies),用于寻找所需非 Python 库的工具。 |
| 109 | + |
| 110 | +[cycler](http://matplotlib.org/cycler/) 0.10.1 或更新 |
| 111 | + |
| 112 | +可组合的循环类,用于构造 style-cycle。 |
| 113 | + |
| 114 | +[six](https://pypi.python.org/pypi/six) |
| 115 | + |
| 116 | +需要用于 Python 2 和 3 之间的兼容性。 |
| 117 | + |
| 118 | +#### Python 2 的依赖 |
| 119 | + |
| 120 | +[functools32](https://pypi.python.org/pypi/functools32) |
| 121 | + |
| 122 | +需要用于 Python 2.7 上的兼容性。 |
| 123 | + |
| 124 | +[subprocess32](https://pypi.python.org/pypi/subprocess32/) |
| 125 | + |
| 126 | +可选,仅用于 Unix。`subprocess`标准库从 3.2+ 到 2.7 的 Backport。它提供了更好的错误信息和超时支持。 |
| 127 | + |
| 128 | +#### 可选的 GUI 框架 |
| 129 | + |
| 130 | +这些是可选软件包,您可能希望安装这些软件包来使 用matplotlib 和用户界面工具包。 有关 matplotlib 可选后端和所提供功能的更多详细信息,请参阅[什么是后端](http://matplotlib.org/faq/usage_faq.html#what-is-a-backend)。 |
| 131 | + |
| 132 | +[tk](http://matplotlib.org/glossary/index.html#term-tk) 8.3 或更新,不包括 8.6.0 和 8.6.1 |
| 133 | + |
| 134 | +TkAgg 后端使用的 TCL/Tk 窗口控件库。 |
| 135 | + |
| 136 | +版本 8.6. 0和 8.6.1 已知有问题,当以错误的顺序关闭多个窗口时可能导致段错误。 |
| 137 | + |
| 138 | +[pyqt](http://matplotlib.org/glossary/index.html#term-pyqt) 4.4 或更新 |
| 139 | + |
| 140 | +Qt4 控件库的 Python 包装,用于 Qt4Agg 后端。 |
| 141 | + |
| 142 | +[pygtk](http://matplotlib.org/glossary/index.html#term-pygtk) 2.4 或更新 |
| 143 | + |
| 144 | +GTK 控件库的 Python 包装,用于 GTK 或者 GTKAgg 后端。 |
| 145 | + |
| 146 | +[wxpython](http://matplotlib.org/glossary/index.html#term-wxpython) 2.8 或更新 |
| 147 | + |
| 148 | +wx 控件库的 Python 包装,用于 WX 或 WXAgg 后端。 |
| 149 | + |
| 150 | +#### 可选的外部程序 |
| 151 | + |
| 152 | +[ffmpeg](https://www.ffmpeg.org/)/[avconv](https://libav.org/avconv.html) 或 [mencoder](http://www.mplayerhq.hu/design7/news.html) |
| 153 | + |
| 154 | +需要用于动画模块,将输出保存为电影格式。 |
| 155 | + |
| 156 | +[ImageMagick](http://www.imagemagick.org/script/index.php) |
| 157 | + |
| 158 | +需要用于动画模块,能够保存 GIF 动画。 |
| 159 | + |
| 160 | +#### 可选依赖 |
| 161 | + |
| 162 | +[Pillow](http://python-pillow.org/) |
| 163 | + |
| 164 | +如果安装了 Pillow,matplotlib 可以读取或写入大部分图像文件格式。 |
| 165 | + |
| 166 | +[pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/) |
| 167 | + |
| 168 | +用于寻找所需非 Python 库的工具。并不是严格需要它,但是如果库和头文件不在预期位置,可以使安装更加便捷。 |
| 169 | + |
| 170 | +#### matplotlib 自带的所需库 |
| 171 | + |
| 172 | +[agg](http://matplotlib.org/glossary/index.html#term-agg) 2.4 |
| 173 | + |
| 174 | +C++ 渲染引擎。 matplotlib 静态链接到 agg 模板源码,所以它除了 matplotlib 之外,不会影响你的系统的任何东西。 |
| 175 | + |
| 176 | +qhull 2012.1 |
| 177 | + |
| 178 | +用于计算 Delaunay 三角测量的库。 |
| 179 | + |
| 180 | +ttconv |
| 181 | + |
| 182 | +TureType 字体工具。 |
| 183 | + |
| 184 | +### 在 Linux 上构建 |
| 185 | + |
| 186 | +使用你的系统包管理器来安装依赖最为简单。 |
| 187 | + |
| 188 | +如果你使用 Debian/Ubuntu,可以使用以下命令在获取需要用于构建 matplotlib 的所有依赖: |
| 189 | + |
| 190 | +``` |
| 191 | +sudo apt-get build-dep python-matplotlib |
| 192 | +``` |
| 193 | + |
| 194 | +如果你使用 Fedora/RedHat,你可以使用以下命令: |
| 195 | + |
| 196 | +``` |
| 197 | +su -c "yum-builddep python-matplotlib" |
| 198 | +``` |
| 199 | + |
| 200 | +这不会构建 matplotlib,但这会安装所需依赖。这会使从源码构建变得容易。 |
| 201 | + |
| 202 | +### 在 OSX 上构建 |
| 203 | + |
| 204 | +由于可以获取`libpng`和`freetype`需求(darwinports,fink,/usr/X11R6)的不同位置,不同的架构(例如 x86,ppc,universal)和不同的 OSX 版本 10.4 和 10.5),OSX 的构建情况很复杂。我们建议您使用我们对 OSX 版本所做的方式来构建:从`tarball`或`git`仓库获取源代码,并按照`README.osx`中的说明进行操作。 |
| 205 | + |
| 206 | +### 在 Windows 上构建 |
| 207 | + |
| 208 | +<https://www.python.org> 上发布的 Python ,使用 VS2008 编译 3.3 之前的版本,使用 VS2010 编译 3.3,并且使用 VS2015 编译 3.5 和 3.6。 建议使用相同的编译器编译 Python 扩展。 |
| 209 | + |
| 210 | +由于没有规范的 Windows 包管理器,从源代码构建`freetype`,`zlib`和`libpng`的方法被记录为`matplotlib-winbuild`中的构建脚本。 |
0 commit comments