Python绘图库的封装与学习

matplotlib是Python常用的绘图库,在ipython notebook中结合matplotlib来进行交互绘图做数据分析非常方便。
matplotlib提供的原始API已经非常方便,但是我个人的需求需要更加简便化,所以针对个人进行再一次封装,以便进行傻瓜式调用。

1
2
3
4
# coding=utf-8
import matplotlib.pyplot as plt
import matplotlib.pylab as mlab
import numpy as np

1.封装散点图接口

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
28
29
30
31
32
33
34
35
36
37
38
39
def scatterplot(ax,
x_data_list,
y_data_list,
color_list,
x_label="",
y_label="",
title="",
yscale_log=False):
"""绘制散点图
封装matplotlib提供的原始scatter接口,使其可以对一组会多组数据的输入进行绘制。
Args:
ax: plt.subplot(), 一个子图
x_data_list: [[], []], 二维list
y_data_list: [[], []], 二维list
color_list: ["r", "b"], 颜色list
x_label: str, 横坐标名称
y_label: str, 纵坐标名称
title: str, 当前子图的名称
Returns:
None
"""
# Plot the data, set the size (s), color and transparency (alpha)
# of the points
for i in range(len(x_data_list)):
ax.scatter(x_data_list[i],
y_data_list[i],
s=10,
color=color_list[i],
alpha=0.75)
if yscale_log == True:
ax.set_yscale('log')
# Label the axes and provide a title
ax.set_title(title)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)

对于上面封装好的接口,模拟三组输入数据看看效果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ax = plt.subplot()
x_data_list = [np.random.randn(100) for i in range(3)]
y_data_list = [np.random.randn(100) for i in range(3)]
color_list = ["r", "g", "b"]
x_label = "X axis"
y_label = "Y axis"
title = "X-Y scatterploter"
scatterplot(ax,
x_data_list,
y_data_list,
color_list,
x_label,
y_label,
title,
yscale_log=False)

png

2. 封装折线图

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
28
29
30
31
32
33
34
35
def lineplot(ax,
x_data_list,
y_data_list,
color_list,
x_label="",
y_label="",
title=""):
"""绘制散点图
封装matplotlib提供的原始scatter接口,使其可以对一组会多组数据的输入进行绘制。
Args:
ax: plt.subplot(), 一个子图
x_data_list: [[], []], 二维list
y_data_list: [[], []], 二维list
color_list: ["r", "b"], 颜色list
x_label: str, 横坐标名称
y_label: str, 纵坐标名称
title: str, 当前子图的名称
Returns:
None
"""
# Plot the best fit line, set the linewidth (lw), color and
# transparency (alpha) of the line
for i in range(len(x_data_list)):
ax.plot(x_data_list[i],
y_data_list[i],
lw=2,
color=color_list[i],
alpha=1)
# Label the axes and provide a title
ax.set_title(title)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)

对于上面封装好的接口,模拟两组数据模拟y=x*x的效果.

1
2
3
4
5
6
7
8
9
10
import math
ax = plt.subplot()
x_data_list = [[i for i in range(-100, 100)] for j in range(2)]
y_data_list = [[math.pow(i, j + 1) for i in range(-100, 100)]
for j in range(2)]
color_list = ['r', 'g']
x_label = 'X axis'
y_label = 'Y axis'
title = 'X & Y'
lineplot(ax, x_data_list, y_data_list, color_list, x_label, y_label, title)

png

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
28
29
30
31
32
33
34
35
36
def histplot(ax,
x_data_list,
num_bins,
color_list,
alpha_list=[1.0, 0.5],
x_label="",
y_label="",
title="",
cumulative=False,
normed=1):
"""绘制直方图
Args:
ax: plt.subplot(),子图
x_data_list: list, n组数据
num_bins: int, 直方图的区间数
color_list: list(str), 颜色
alpha_list: list(float), 透明度
x_label: str,x轴信息
y_label: str, y轴信息
title: 当前子图的名称
cumulative: bool, 是否采用累积方式
normed: 总的概率值
Return:
None
"""
for i in range(len(x_data_list)):
ax.hist(x_data_list[i],
50,
cumulative=False,
normed=1,
facecolor=color_list[i],
alpha=alpha_list[i])
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title(title)

模拟两组正太分布数据:

1
2
3
4
5
6
7
8
9
10
11
12
x_data_list = [
100 + 15 * np.random.randn(10000), 100 + 10 * np.random.randn(10000)
]
ax = plt.subplot()
histplot(ax=ax,
x_data_list=x_data_list,
num_bins=50,
color_list=["r", "g"],
x_label="X axis",
y_label="Y axis",
title="X & Y")
plt.show()

png

4.柱状图接口

4.1 堆叠柱状图

1
2
3
4
5
6
7
8
9
10
11
12
name_list = ['Monday', 'Tuesday', 'Friday', 'Sunday']
num_list = [1.5, 0.6, 7.8, 6]
num_list1 = [1, 2, 3, 1]
plt.bar(range(len(num_list)), num_list, label='boy', fc='y')
plt.bar(range(len(num_list)),
num_list1,
bottom=num_list,
label='girl',
tick_label=name_list,
fc='r')
plt.legend()
plt.show()

png

4.2 并列柱状图

1
2
3
4
5
6
7
8
9
10
11
12
13
name_list = ['Monday','Tuesday','Friday','Sunday']
num_list = [1.5,0.6,7.8,6]
num_list1 = [1,2,3,1]
x =list(range(len(num_list)))
total_width, n = 0.8, 2
width = total_width / n
plt.bar(x, num_list, width=width, label='boy',fc = 'y')
for i in range(len(x)):
x[i] = x[i] + width
plt.bar(x, num_list1, width=width, label='girl',tick_label = name_list,fc = 'r')
plt.legend()
plt.show()

png