matplotlib(1):简介

matplotlib是一个python语言绘图库,本文介绍了matplotlib的安装和基本使用方法。

what is matplotlib

matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala MATLAB®* or Mathematica®†), web application servers, and six graphical user interface toolkits.

  • ala-法语,in the style/manner of
  • aka-also known as

install

  1. python
    参考python download
    本文安装的版本是2.7.8.
  2. matplotlib
    参考matplotlib install
    这里介绍windows的安装过程:
    通过python的包管理工具pip安装.
    如果网络稳定,cmd下执行pip install matplotlib即可,pip会自动安装依赖包;
    如果网络不稳定,需要下载matplotlib的pip安装包,以及下列matplotlib依赖的安装包(注意版本兼容):
    setuptools
    numpy
    python-dateutil
    pytz
    pyparsing
    cycler
    然后pip install /path/to/package/package-name.whl来逐个安装。
    安装完成后,在python命令行下执行import matplotlib,如果没有出错说明安装成功。
    我的安装版本:matplotlib_version.png

play

通过numpy生成数据,pyplot来绘图。

simple plot

simple_plotview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
# Make some data
x = np.arange(0.0, 2.0, 0.01)
# 3 functions
y1 = x
y2 = x*x
y3 = x*x*x
# Create plots with labels
fig, ax = plt.subplots()
ax.plot(x, y1, 'b', label='linear')
ax.plot(x, y2, 'g', label='quadratic')
ax.plot(x, y3, 'r', label='cubic')
# Set title, x lable and y label
ax.set_title('simple plot')
ax.set_xlabel('x label')
ax.set_ylabel('y label')
# Set legend
legend = ax.legend(loc='upper left', shadow=True, fontsize='smaller')
legend.get_frame().set_facecolor('#FFFFFF')
plt.show()

result:

simple_plot.png

Reference:

  1. Python-Matplotlib安装及简单使用
  2. 绘图: Python matplotlib简介