matplotlib基础

matplotlib基础

1.基本绘图

控制颜色

  • 颜色之间的对应关系为

    b—-blue
    c—-cyan
    g—-green
    k——black
    m—-magenta
    r—-red
    w—-white
    y——yellow

  • 表示颜色的方式:

    a:用全名
    b:16进制如:#FF00FF
    c:RGB或RGBA元组(1,0,1,1)
    d:灰度强度如:‘0.7’

控制线型

  • 符号和线型之间的对应关系
    • 实线
      — 短线
      -. 短点相间线
      : 虚点线

控制标记风格

  • 标记风格有多种:

    . Point marker
    , Pixel marker
    o Circle marker
    v Triangle down marker
    ^ Triangle up marker
    < Triangle left marker
    > Triangle right marker
    1 Tripod down marker
    2 Tripod up marker
    3 Tripod left marker
    4 Tripod right marker
    s Square marker
    p Pentagon marker
    * Star marker
    h Hexagon marker
    H Rotated hexagon D Diamond marker
    d Thin diamond marker
    | Vertical line (vlinesymbol) marker
    _ Horizontal line (hline symbol) marker
    + Plus marker
    x Cross (x) marker

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import pandas as pd
import numpy as np

# 导入matplot库
import matplotlib.pyplot as plt

# 设置输出图像大小
plt.rc('figure', figsize=(10, 5))

# 生成数据
x = np.linspace(0, 2, 10) # [0,2]之间均匀产生10个值
plt.plot(x, x, 'o-', label='linear') # y = x 曲线
plt.plot(x, x ** 2, 'x-', label='quadratic') # y=x^2曲线
plt.legend(loc='best')
plt.title('Linear vs Quadratic progression')
plt.xlabel('Input')
plt.ylabel('Output');
plt.show()

生成结果

Alt text

2.直方图

API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
matplotlib.pyplot.hist(
x, # numpy数组
bins=10, # bin数量
range=None,
normed=False, # 归一化
weights=None,
cumulative=False, # 累积函数
bottom=None,
histtype='bar', # {‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’},
align='mid',
orientation='vertical',
rwidth=None,
log=False,
color=None,
label=None,
stacked=False,
hold=None,
data=None,
**kwargs)

示例代码

1
2
3
4
5
6
7
8
9
10
11
samples = np.random.normal(loc=1.0, scale=0.5, size=1000)
print(samples.shape)
print(samples.dtype)
print(samples[:30])

# step画出累积分布
plt.hist(samples, bins=50, cumulative=True, histtype='step')

# 直方图分布
plt.hist(samples, bins=50, histtype='bar')
plt.show()

生成结果

Alt text

3.双直方图

示例代码

1
2
3
4
5
6
7
8
9
samples_1 = np.random.normal(loc=1, scale=.5, size=10000)
samples_2 = np.random.standard_t(df=10, size=10000)
bins = np.linspace(-3, 3, 50)

# Set an alpha and use the same bins since we are plotting two hists
plt.hist(samples_1, bins=bins, alpha=0.5, label='samples 1')
plt.hist(samples_2, bins=bins, alpha=0.5, label='samples 2')
plt.legend(loc='upper left');
plt.show()

生成结果

Alt text

4.散点图

API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
matplotlib.pyplot.scatter(
x,
y,
s=20, # 点大小
c=None, # 颜色
marker='o',
cmap=None,
norm=None,
vmin=None,
vmax=None,
alpha=None,
linewidths=None,
verts=None,
edgecolors=None,
hold=None,
data=None,
**kwargs)

示例代码

1
2
plt.scatter(samples_1, samples_2, alpha=0.1);
plt.show()

示例代码

Alt text

5.参考

[1] http://blog.csdn.net/kkxgx/article/details/6951959
[2] http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/matplotlib/matplotlib.ipynb

坚持原创技术分享,您的支持将鼓励我继续创作!