本文以委托蝙蝠侠、蜘蛛侠叮当猫等人进行维权,未经许可,禁止转载!!!!
一、画线
import matplotlib.pyplot as plt
x = [1,2,3]
y = [1,20,3]
x2 = [2,3,4]
y2 = [2,14,8]
plt.plot(x,y,label='First one')
plt.plot(x2,y2,label='Second one')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting\nChick it out')
plt.legend()
plt.show
![](https://i-blog.csdnimg.cn/blog_migrate/35af848de01091084ba6bdecdfe4f6cb.png)
二、
画柱状图(bar)
import matplotlib.pyplot as plt
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting\nChick it out')
x = [2,4,6,8,10]
y = [6,7,8,2,4]
x2 = [2,4,6,8,10]
y2 = [7,8,2,4,2]
plt.bar(x,y,label='bar1',color='r')
plt.bar(x2,y2,label='bar2',color='c')
plt.legend()
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/9c1493f32e1b102e847022a99ca545b0.png)
画柱状图(hist)---将数据根据bins进行分类,使其更加有实用价值
import matplotlib.pyplot as plt
population_age = [22,55,62,45,21,22,34,42,42,9,99,102,110,121,122,130,112]
#ids = [x for x in range(len(population_age))]
#plt.bar(ids,population_age)
bins = [0,10,20,30,40,50,60,70,80,90,100,110,120,130]
plt.hist(population_age,bins,histtype='bar',rwidth = 0.5)
##横坐标表示范围
plt.xlabel('x')
plt.ylabel('y')
plt.title('ok')
plt.legend()
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/fdc11d5886f85dba6802581713da4648.png)
画多个柱状图
http://baijiahao.baidu.com/s?id=1591538617221352925&wfr=spider&for=pc
三、画散点图
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6,7,8,9]
y = [1,2,3,4,5,6,7,8,9]
plt.scatter(x,y,label='skit',color='k',marker = '*',s = 100)
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('ok')
四、画分层图---between a num and a num
import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]
plt.stackplot(days,sleeping,eating,working,playing,colors = ['m','c','r','b'])
plt.xlabel('x')
plt.ylabel('y')
plt.title('ok')
plt.legend()
plt.show()
五、画饼图
import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]
cos = ['b','r','y','g']
slices = [7,2,2,13]
activaties = ['sleeping','eating','working','playing']
plt.pie(slices,
labels =activaties,
colors = cos ,
startangle = 90, #从90度处开始循环
shadow = True, #有阴影,显得更加立体
explode = (0,0,0,0.1),#把其中一块拉出来
autopct='%1.1f%%')#显示比例
plt.xlabel('x')
plt.ylabel('y')
plt.title('ok')
plt.legend()
plt.show()
六、画多个子图
#!/usr/bin/env python
#!encoding=utf-8
import matplotlib
import matplotlib.pyplot as plt
if __name__ == '__main__':
for i,color in enumerate("rgby"):
plt.subplot(221+i, axisbg=color)
plt.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3.2 不规则划分
但是有时候我们的划分并不是规则的, 比如如下的形式
这种应该怎么划分呢?
将整个表按照 2*2
划分
前两个简单, 分别是 (2, 2, 1)
和 (2, 2, 2)
但是第三个图呢, 他占用了 (2, 2, 3)
和 (2, 2, 4)
显示需要对其重新划分, 按照 2 * 1
划分
前两个图占用了 (2, 1, 1)
的位置
因此第三个图占用了 (2, 1, 2)
的位置
代码如下, 参照matplotlib绘制多个子图——subplot
#!/usr/bin/env python
#!encoding=utf-8
import matplotlib.pyplot as plt
import numpy as np
def f(t):
return np.exp(-t) * np.cos(2 * np.pi * t)
if __name__ == '__main__' :
t1 = np.arange(0, 5, 0.1)
t2 = np.arange(0, 5, 0.02)
plt.figure(12)
plt.subplot(221)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'r--')
plt.subplot(222)
plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')
plt.subplot(212)
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()
- 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