matplotlib 初识

本文原创自本人的新浪博客文章《数据挖掘(matplotlib初识)》与 CSDN 同步更新。

--------------------------------------------------------------------

Python 使用 matplotlib [1] 库来作图,其职责可以看做是数据挖掘中的数据可视化。虽然之前我也尝试了 matlib 的相关作图功能,但无奈太繁琐,而 python 则更加简洁高效,并且与自己的代码更紧密的结合,觉得很适合自己。现在,我简单回顾以下几种简单 plot 图形,用作查阅复习只用。在编程之前请务必将 2 个库导入到程序中 (为了方便起见,推荐直接安装 anaconda[3] ),他们是 numpy 包和 matplotlib 包。

import numpy as np # 参见[2]
import matplotlib.pyplot as plt

1. 折线图

如下是一个简单的折线图样例,它反映了数据的发展趋势及规律,如增长率,顶点,最大值,最小值等。x 是一个数组 (numpy 中定义的 array 类型),范围是 -10 到 10,间隔为 0.1,那么同样需要得到的 y 也是一个数组,其中 y = sin(x)。

### EG1. simple line plot
x = np.arange(-10, 10, 0.1)
y = np.sin(x)
plt.plot(x,y)
plt.show()


如果需要比较 2 条曲线的变化规律,可以将其放到一个图中进行观察,其中 plot() 函数中的 'r','b'表示红色和蓝色,legend() 规定了图解中的标记列表。

### EG2. double lines plot
x = np.linspace(-5, 5, 100)
y1 = 1/(1+np.exp(-x))
y2 = (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))

plt.plot(x,y1,'r', x,y2,'b')
plt.legend(['Sigmoid', 'Tanh'])
plt.show()


2. 盒图

如下是一个简单的盒图样例,反映了数据的分布规律,如中位数,分位数,异常点等。

### EG3. boxplot
x = np.random.rand(50)*100
plt.boxplot(x)
plt.show()
如下,展示的是多个盒图的共同展示,注意 matplotlib 在做多个盒图时是按列来读取数据的,每一列作为一个盒图。x1 和 x2 的数组元素个数应当相同 (均为 100 个)。

### EG4. double boxes plot
x1 = np.random.rand(50)*100 # 50 个 0-100 随机数
x2 = np.random.rand(50)*80 # 50 个 0-80 随机数
data = [x1, x2]
plt.boxplot(data)
plt.show()


3. 直方图

如下是一个简单的直方图样例,它反映了数据出现的频率。

### EG5. bar plot
data = np.random.normal(50,10,20) # 20 个 均值为 50,标准差为 10 的正态分布数组
x = range(len(data))
plt.bar(x, data)
plt.show()


4. 提琴图

如下是一个简单的提琴图样例,它也反映了数据的分布特征,越粗的地方数据越集中。x1,x2,x3,x4 表示了 4 组均值相同但标准差不同的数组。

### EG6. violin plot
x1 = np.random.normal(50,1,1000)
x2 = np.random.normal(50,2,1000)
x3 = np.random.normal(50,3,1000)
x4 = np.random.normal(50,4,1000)
data=[x1, x2, x3, x4]
plt.violinplot(data, showmeans=True, showmedians=True,showextrema=True)
plt.show()


5. 饼图

如下是一个简单的饼图样例,它反映了数据各部分所占比例的大小,整体呈圆形的饼状。

### EG7. pie plot
labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15,30,45,10]
explode = [0,0,0.1,0]
plt.pie(sizes, labels = labels, explode=explode)
plt.show()


6. 散点图

散点图表现了数据点的分布情况,如下散点图反映了 1000 个随机点在二维坐标轴中的分布情况。

### EG8. dot plot
x = np.random.normal(50,1,1000)
y = np.random.normal(50,2,1000)
plt.scatter(x, y)
plt.show()


7. 分布图(条形图)

概率分布图又称条形图,它反映了数据在不同范围内出现的概率。

### EG9. histgraph plot
perfs = np.random.normal(50, 1, 1000)
plt.hist(perfs, 50, alpha=0.5) # 设置分布在 50 个范围内, 透明度为 0.5
plt.show()


8. 其他设置

其他设置里面包括作图时所需要的其他标注,如刻度,坐标范围,多列子图,形状,颜色等,基本上都是所有类型的图所通用的。

### EG10. 设置横纵坐标刻度使用 xticks()
data = [67,20,30,35,41]
x = range(len(data))

xlabel = "Projects"
ylabel = "Stars"

plt.bar(x, data)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title("Stars of 5 github projects")

plt.xticks(x, ("x264", "lrzip", "apache", "rs_6d_c3_obj2", "config"), rotation=90)
plt.show()


### EG11. 设置横纵坐标范围使用 xlim() 和 ylim()
x = np.arange(-10,10,0.1)
y = np.sin(x)

xlabel = "x"
ylabel = "sin(x)"

plt.plot(x, y)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title("Figure of sin(x)")

plt.xlim(-20,20)
plt.ylim(-2,2)
plt.show()


### EG12. 设置多行/多列子图使用 subplot()
x = np.arange(-10,10,0.1)

y1 = np.sin(x)
plt.subplot(121) #表示1行有2列,此图是第1个
plt.plot(x, y1)
plt.title("Figure of sin(x)")

y2 = np.cos(x)
plt.subplot(122) #表示1行有2列,此图是第2个
plt.plot(x, y2)
plt.title("Figure of cos(x)")

plt.show()


### EG13. 设置曲线形状色彩
x = np.arange(-10,10,0.1)
y1 = np.tanh(x)
y2 = np.sin(x)
# 更多数学函数请参见 https://docs.scipy.org/doc/numpy/reference/routines.math.html

plt.plot(x, y1, "b-", x, y2, "r1")
plt.title("Figure of tanh(x)")
# 更多色彩形状请参见 https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot
plt.legend(['y=tanh(x)','y=sin(x)'])
plt.show()


未完待续 (To be continued :)

------------------------
[1] matplotlib 官网:https://matplotlib.org/index.html
[3] anaconda 官网:https://www.anaconda.com/ 

猜你喜欢

转载自blog.csdn.net/chikily_yongfeng/article/details/81053921
今日推荐