Python 绘图工具之 matplotlib

matplotlib 是一个二维绘图工具包,可以生产高质量的图片。我们可以通过脚本或者是 shell 来使用它。

《Python 绘图工具之 matplotlib》
screenshots

matplotlib 的口号是

make easy things easy and hard things possible

使用 matplotlib, 我们仅仅通过几行命令就能够绘制出线图、直方图、柱状图、误差表、散点图等等。
screenshots, thumbnailgallery, and examples directory

对于简单的绘图功能 pyplot 提供了 类似于 Matlab 的接口(使用 IPython)
而高级绘图功能可以让我们自由控制 线型、字体、轴线等(via an object oriented interface or via a set of functions familiar to MATLAB users.)

先放上 主页, 再来看一些 样例
最新版本为 1.5.3, 目前正在筹划 v2.0

使用 pip 进行安装:

C:\Users\DELL\Desktop  (Desktop@1.0.0)
λ python -m pip install -U pip setuptools
Requirement already up-to-date: pip in c:\users\dell\appdata\local\programs\python\python35-32\lib\site-packages
Collecting setuptools
  Downloading setuptools-28.6.0-py2.py3-none-any.whl (471kB)
    100% |████████████████████████████████| 481kB 465kB/s
Installing collected packages: setuptools
  Found existing installation: setuptools 24.0.2
    Uninstalling setuptools-24.0.2:
      Successfully uninstalled setuptools-24.0.2
Successfully installed setuptools-28.6.0

C:\Users\DELL\Desktop  (Desktop@1.0.0)
λ python -m pip install matplotlib
Collecting matplotlib
  Downloading matplotlib-1.5.3-cp35-cp35m-win32.whl (6.2MB)
    100% |████████████████████████████████| 6.2MB 182kB/s
Collecting cycler (from matplotlib)
  Downloading cycler-0.10.0-py2.py3-none-any.whl
Collecting python-dateutil (from matplotlib)
  Downloading python_dateutil-2.5.3-py2.py3-none-any.whl (201kB)
    100% |████████████████████████████████| 204kB 1.3MB/s
Collecting numpy>=1.6 (from matplotlib)
  Downloading numpy-1.11.2-cp35-none-win32.whl (6.6MB)
    100% |████████████████████████████████| 6.6MB 166kB/s
Collecting pyparsing!=2.0.4,!=2.1.2,>=1.5.6 (from matplotlib)
  Downloading pyparsing-2.1.10-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 877kB/s
Collecting pytz (from matplotlib)
  Downloading pytz-2016.7-py2.py3-none-any.whl (480kB)
    100% |████████████████████████████████| 481kB 660kB/s
Collecting six (from cycler->matplotlib)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: six, cycler, python-dateutil, numpy, pyparsing, pytz, matplotlib
Successfully installed cycler-0.10.0 matplotlib-1.5.3 numpy-1.11.2 pyparsing-2.1.10 python-dateutil-2.5.3 pytz-2016.7 six-1.10.0

C:\Users\DELL\Desktop  (Desktop@1.0.0)
λ

好了,现在我们可以使用了。

import numpy as np
import matplotlib.pyplot as plt

N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radiuses

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()

《Python 绘图工具之 matplotlib》 效果

保存成文件是这样的:

《Python 绘图工具之 matplotlib》 效果
《Python 绘图工具之 matplotlib》 支持另存的文件格式

好了,就这么多,要学的东西还很多。。。

[Added on 25 Oct., 2016]
I am now working on Ubuntu, the way to install softwares is different form that of windows, everytime you setup a software, go to the official site and do as it tells you.

点赞