Matplotlib Installing an official release from resources 源码安装Matplotlib官方版本
Installation
Installing an official release
Matplotlib releases are available as wheel packages for macOS, Windows and Linux on PyPI. Install it using pip
:
python -m pip install -U pip
python -m pip install -U matplotlib
If this command results in Matplotlib being compiled from source and there's trouble with the compilation, you can add --prefer-binary
to select the newest version of Matplotlib for which there is a precompiled wheel for your OS and Python.
Note
The following backends work out of the box: Agg, ps, pdf, svg
Python is typically shipped with tk bindings which are used by TkAgg.
For support of other GUI frameworks, LaTeX rendering, saving animations and a larger selection of file formats, you can install Optional dependencies.
Third-party distributions
Various third-parties provide Matplotlib for their environments.
Conda packages
Matplotlib is available both via the anaconda main channel
conda install matplotlib
as well as via the conda-forge community channel
conda install -c conda-forge matplotlib
Python distributions
Matplotlib is part of major Python distributions:
Linux package manager
If you are using the Python version that comes with your Linux distribution, you can install Matplotlib via your package manager, e.g.:
-
Debian / Ubuntu:
-
sudo apt-get install python3-matplotlib
-
Fedora:
sudo dnf install python3-matplotlib
-
Red Hat:
sudo yum install python3-matplotlib
-
Arch:
sudo pacman -S python-matplotlib
Installing from source
If you are interested in contributing to Matplotlib development, running the latest source code, or just like to build everything yourself, it is not difficult to build Matplotlib from source.
First you need to install the Dependencies.
A C compiler is required. Typically, on Linux, you will need gcc
, which should be installed using your distribution's package manager; on macOS, you will need xcode; on Windows, you will need Visual Studio 2015 or later.
The easiest way to get the latest development version to start contributing is to go to the git repository and run:
git clone https://github.com/matplotlib/matplotlib.git
or:
git clone git@github.com:matplotlib/matplotlib.git
If you're developing, it's better to do it in editable mode. The reason why is that pytest's test discovery only works for Matplotlib if installation is done this way. Also, editable mode allows your code changes to be instantly propagated to your library code without reinstalling (though you will have to restart your python process / kernel):
cd matplotlib
python -m pip install -e
.
If you're not developing, it can be installed from the source directory with a simple (just replace the last step):
python -m pip install
.
To run the tests you will need to install some additional dependencies:
python -m pip install -r requirements/dev/dev-requirements.txt
Then, if you want to update your Matplotlib at any time, just do:
git pull
When you run git pull
, if the output shows that only Python files have been updated, you are all set. If C files have changed, you need to run pip install -e .
again to compile them.
There is more information on using git in the developer docs.
Warning
The following instructions in this section are for very custom installations of Matplotlib. Proceed with caution because these instructions may result in your build producing unexpected behavior and/or causing local testing to fail.
If you would like to build from a tarball, grab the latest tar.gz release file from the PyPI files page.
We provide a mplsetup.cfg file which you can use to customize the build process. For example, which default backend to use, whether some of the optional libraries that Matplotlib ships with are installed, and so on. This file will be particularly useful to those packaging Matplotlib.
If you are building your own Matplotlib wheels (or sdists) on Windows, note that any DLLs that you copy into the source tree will be packaged too.
Installing for development
See Setting up Matplotlib for development.
Frequently asked questions
Report a compilation problem
See Getting help.
Matplotlib compiled fine, but nothing shows up when I use it
The first thing to try is a clean install and see if that helps. If not, the best way to test your install is by running a script, rather than working interactively from a python shell or an integrated development environment such as IDLE which add additional complexities. Open up a UNIX shell or a DOS command prompt and run, for example:
python -c "from pylab import *; set_loglevel('debug'); plot(); show()"
This will give you additional information about which backends Matplotlib is loading, version information, and more. At this point you might want to make sure you understand Matplotlib's configuration process, governed by the matplotlibrc
configuration file which contains instructions within and the concept of the Matplotlib backend.
If you are still having trouble, see Getting help.
How to completely remove Matplotlib
Occasionally, problems with Matplotlib can be solved with a clean installation of the package. In order to fully remove an installed Matplotlib:
-
Delete the caches from your Matplotlib configuration directory.
-
Delete any Matplotlib directories or eggs from your installation directory.
OSX Notes
Which python for OSX?
Apple ships OSX with its own Python, in /usr/bin/python
, and its own copy of Matplotlib. Unfortunately, the way Apple currently installs its own copies of NumPy, Scipy and Matplotlib means that these packages are difficult to upgrade (see system python packages). For that reason we strongly suggest that you install a fresh version of Python and use that as the basis for installing libraries such as NumPy and Matplotlib. One convenient way to install Matplotlib with other useful Python software is to use the Anaconda Python scientific software collection, which includes Python itself and a wide range of libraries; if you need a library that is not available from the collection, you can install it yourself using standard methods such as pip. See the Ananconda web page for installation support.
Other options for a fresh Python install are the standard installer from python.org, or installing Python using a general OSX package management system such as homebrew or macports. Power users on OSX will likely want one of homebrew or macports on their system to install open source software packages, but it is perfectly possible to use these systems with another source for your Python binary, such as Anaconda or Python.org Python.
Installing OSX binary wheels
If you are using Python from https://www.python.org, Homebrew, or Macports, then you can use the standard pip installer to install Matplotlib binaries in the form of wheels.
pip is installed by default with python.org and Homebrew Python, but needs to be manually installed on Macports with
sudo port install py38-pip
Once pip is installed, you can install Matplotlib and all its dependencies with from the Terminal.app command line:
python3 -mpip install matplotlib
(sudo python3.6 ...
on Macports).
You might also want to install IPython or the Jupyter notebook (python3 -mpip install ipython notebook
).
Checking your installation
The new version of Matplotlib should now be on your Python "path". Check this at the Terminal.app command line:
python3 -c 'import matplotlib; print(matplotlib.__version__, matplotlib.__file__)'
You should see something like
3.0.0 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/__init__.py
where 3.0.0
is the Matplotlib version you just installed, and the path following depends on whether you are using Python.org Python, Homebrew or Macports. If you see another version, or you get an error like
Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named matplotlib
then check that the Python binary is the one you expected by running
which python3
If you get a result like /usr/bin/python...
, then you are getting the Python installed with OSX, which is probably not what you want. Try closing and restarting Terminal.app before running the check again. If that doesn't fix the problem, depending on which Python you wanted to use, consider reinstalling Python.org Python, or check your homebrew or macports setup. Remember that the disk image installer only works for Python.org Python, and will not get picked up by other Pythons. If all these fail, please let us know.
===================== 以下是中文版安装指南================
源码安装Matplotlib官方版本
安装官方版本
Matplotlib 版本可作为 macOS、Windows 和 Linux on PyPI的轮包提供。使用安装它pip
:
python -m pip install -U pip
python -m pip install -U matplotlib
如果此命令导致从源代码编译 Matplotlib 并且编译出现问题,您可以添加--prefer-binary
以选择最新版本的 Matplotlib,其中有一个适用于您的 OS 和 Python 的预编译轮。
笔记
以下后端开箱即用:Agg、ps、pdf、svg
Python 通常附带 TkAgg 使用的 tk 绑定。
为了支持其他 GUI 框架、LaTeX 渲染、保存动画和更多的文件格式选择,您可以安装可选依赖项。
第三方发行版
各种第三方为其环境提供 Matplotlib。
康达包
Matplotlib 可通过anaconda 主频道获得
conda install matplotlib
以及通过conda-forge 社区频道
conda install -c conda-forge matplotlib
Python 发行版
Matplotlib 是主要 Python 发行版的一部分:
Linux 包管理器
如果您使用的是 Linux 发行版附带的 Python 版本,您可以通过包管理器安装 Matplotlib,例如:
-
Debian / Ubuntu:
sudo apt-get install python3-matplotlib
-
Fedora Linux:
sudo dnf install python3-matplotlib
-
Red Hat Linux:
sudo yum install python3-matplotlib
-
Arch Linux:
sudo pacman -S python-matplotlib
从源代码安装
如果您有兴趣为 Matplotlib 开发做出贡献,运行最新的源代码,或者只是想自己构建一切,那么从源代码构建 Matplotlib 并不难。
首先,您需要安装Dependencies。
需要交流编译器。通常,在 Linux 上,您需要gcc
使用发行版的包管理器安装它;在 macOS 上,您将需要xcode;在 Windows 上,您将需要 Visual Studio 2015 或更高版本。
获取最新开发版本开始贡献的最简单方法是转到 git存储库并运行:
git clone https://github.com/matplotlib/matplotlib.git
或者:
git clone git@github.com:matplotlib/matplotlib.git
如果您正在开发,最好在可编辑模式下进行。如果以这种方式安装,pytest 的测试发现仅适用于 Matplotlib 的原因。此外,可编辑模式允许您的代码更改立即传播到您的库代码,而无需重新安装(尽管您必须重新启动 python 进程/内核):
cd matplotlib
python -m pip install -e
.
如果你不开发,可以从源目录安装,简单(只需替换最后一步):
python -m pip install
.
要运行测试,您需要安装一些额外的依赖项:
python -m pip install -r requirements/dev/dev-requirements.txt
然后,如果您想随时更新您的 Matplotlib,只需执行以下操作:
git pull
运行时,如果输出显示只有 Python 文件已更新,则一切就绪。如果 C 文件已更改,则需要再次运行以编译它们。git pull
pip install -e .
在开发者文档中有更多关于使用 git的信息。
警告
本节中的以下说明适用于非常自定义的 Matplotlib 安装。请谨慎操作,因为这些说明可能会导致您的构建产生意外行为和/或导致本地测试失败。
如果您想从 tarball 构建,请从PyPI 文件页面获取最新的tar.gz发布文件。
我们提供了一个mplsetup.cfg文件,您可以使用它来自定义构建过程。例如,使用哪个默认后端,是否安装了 Matplotlib 附带的一些可选库,等等。这个文件对那些打包 Matplotlib 的人特别有用。
如果您在 Windows 上构建自己的 Matplotlib 轮子(或 sdists),请注意,您复制到源代码树中的任何 DLL 也将被打包。
安装开发
请参阅为开发设置 Matplotlib。
常见问题
报告编译问题
请参阅获得帮助。
Matplotlib 编译得很好,但我使用它时什么也没有显示
首先要尝试的是全新安装,看看是否有帮助。如果没有,测试安装的最佳方法是运行脚本,而不是从 python shell 或集成开发环境(如IDLE )以交互方式工作,这会增加额外的复杂性。打开 UNIX shell 或 DOS 命令提示符并运行,例如:
python -c "from pylab import *; set_loglevel('debug'); plot(); show()"
这将为您提供有关 Matplotlib 正在加载哪些后端、版本信息等的更多信息。此时,您可能需要确保您了解 Matplotlib 的配置过程,该过程由matplotlibrc
包含说明和 Matplotlib 后端概念的配置文件控制。
如果您仍有问题,请参阅获得帮助。
如何完全删除 Matplotlib
有时,可以通过全新安装包来解决 Matplotlib 的问题。为了完全删除已安装的 Matplotlib:
-
从您的Matplotlib 配置目录中删除缓存。
-
从安装目录中删除任何 Matplotlib 目录或鸡蛋。
OSX 注释
适用于 OSX 的 Python 是什么?
Apple 为 OSX 提供了自己的 Python、in/usr/bin/python
和自己的 Matplotlib 副本。不幸的是,Apple 目前安装自己的 NumPy、Scipy 和 Matplotlib 副本的方式意味着这些包很难升级(参见系统 python 包)。因此,我们强烈建议您安装新版本的 Python,并将其用作安装 NumPy 和 Matplotlib 等库的基础。将 Matplotlib 与其他有用的 Python 软件一起安装的一种便捷方法是使用Anaconda Python 科学软件集合,其中包括 Python 本身和各种库;如果您需要集合中没有的库,您可以使用标准方法(例如pip )自行安装. 有关安装支持,请参阅 Ananconda 网页。
全新 Python 安装的其他选项是来自python.org的标准安装程序,或使用通用 OSX 包管理系统(如homebrew或macports )安装 Python 。OSX 上的高级用户可能希望在他们的系统上使用 homebrew 或 macports 来安装开源软件包,但是完全可以将这些系统与 Python 二进制文件的另一个源一起使用,例如 Anaconda 或 Python.org Python。
安装 OSX 二进制轮子
如果您使用来自https: //www.python.org、Homebrew 或 Macports 的 Python,则可以使用标准 pip 安装程序以轮子的形式安装 Matplotlib 二进制文件。
pip 默认与 python.org 和 Homebrew Python 一起安装,但需要在 Macports 上手动安装
sudo port install py38-pip
安装 pip 后,您可以从 Terminal.app 命令行安装 Matplotlib 及其所有依赖项:
python3 -mpip install matplotlib
(在 Macports 上)。sudo python3.6 ...
您可能还想安装 IPython 或 Jupyter notebook ( )。python3 -
mpip install ipython notebook
检查您的安装
新版本的 Matplotlib 现在应该在您的 Python“路径”上。在 Terminal.app 命令行中检查:
python3 -c 'import matplotlib; print(matplotlib.__version__, matplotlib.__file__)'
你应该看到类似的东西
3.0.0 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/__init__.py
你刚刚安装的 Matplotlib 版本在哪里3.0.0
,后面的路径取决于你使用的是 Python.org Python、Homebrew 还是 Macports。如果您看到另一个版本,或者您收到类似的错误
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named matplotlib
然后通过运行检查 Python 二进制文件是否是您所期望的
which python3
如果您得到类似的结果/usr/bin/python...
,那么您将使用 OSX 安装 Python,这可能不是您想要的。在再次运行检查之前尝试关闭并重新启动 Terminal.app。如果这不能解决问题,取决于你想使用哪个 Python,考虑重新安装 Python.org Python,或者检查你的 homebrew 或 macports 设置。请记住,磁盘映像安装程序仅适用于 Python.org Python,不会被其他 Python 获取。如果所有这些都失败了,请告诉我们。