数据分析
绘制折线图:
初步了解matplotlib模块:
from matplotlib import pyplot as plt x = range(2,26,2) y = [15,13,14.5,17,20,25,26,25,27,22,18,15] # 设置图片大小,figsize设置图片大小,dpi设置分辨率 plt.figure(figsize=(20,8),dpi=80) # 设置x轴刻度 # plt.xticks(x) _xtick_labels = [i/2 for i in range(4,49)] plt.xticks(_xtick_labels[::3]) # 设置y轴刻度 plt.yticks(range(min(y),max(y)+1)) # 绘图 plt.plot(x,y) # 展示 # plt.show() # 保存图片 plt.savefig('./test.png')
在坐标上加入描述信息,x轴上添加字符串
from matplotlib import pyplot as plt from random import randint from matplotlib import font_manager y = [randint(20,40) for i in range(120)] x = range(120) # 设置字体 # my_font = font_manager.FontEntry(name='C:\Windows\Fonts\MicrosoftYaHeiUI',) # 报错,不整了 # 设置图片大小 plt.figure(figsize=(20,8),dpi=80) # 绘图 plt.plot(x,y) # 设置x轴 _xtick_lables = ['10:{}'.format(i) for i in range(60)] _xtick_lables += ['11:{}'.format(i) for i in range(60)] # 取步长,数字和字符串一一对应,数据长度一样 plt.xticks(x[::3],_xtick_lables[::3],rotation = 45) plt.yticks(range(min(y),max(y)+1)) # 添加描述信息 plt.xlabel('time') plt.ylabel('wendu') plt.title('change') # 保存 plt.savefig('./qiwen.png')
绘制表格
from matplotlib import pyplot as plt y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1] x = range(11,31) # 设置图形大小 plt.figure(figsize=(10,6),dpi=80) plt.plot(x,y) # 设置x轴坐标 _xtick_labels = ['{}y'.format(i) for i in x] plt.xticks(x,_xtick_labels) # 绘制网格 plt.grid() # 保存 plt.savefig('./zhanan.png')
添加图例
from matplotlib import pyplot as plt y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1] yy = [1,0,4,1,2,4,4,2,3,4,4,5,5,5,4,3,3,1,6,1] x = range(11,31) # 设置图形大小 plt.figure(figsize=(10,6),dpi=80) plt.plot(x,y,label='Mr.Zhao') plt.plot(x,yy,label = 'Mr.Qian') # 设置x轴坐标 _xtick_labels = ['{}y'.format(i) for i in x] plt.xticks(x,_xtick_labels) # 绘制网格 plt.grid() # 添加图例 plt.legend() # 保存 plt.savefig('./zhanan.png')
绘制散点图:
from matplotlib import pyplot as plt y_3 = [11,17,11,12,22,44,55,21,22,45,22,44,55,11,22,11,34,33,21] y_10= [11,18,13,18,24,24,45,23,43,22,24,11,23,13,15,18,43,22,31] x_3 = range(1,20) x_10 = range(31,50) plt.scatter(x_3,y_3) plt.scatter(x_10,y_10) # plt.figure(figsize=(10,8),dpi=80) plt.xlabel=('time') plt.ylabel=('hot') plt.title=('qiwen') plt.grid() plt.savefig('./sandian.png')
绘制条形图:
竖着的:
from matplotlib import pyplot as plt a = ['a1','a2','a3','a4','a5'] b = [11,20,33,9,40] plt.bar(range(len(a)),b) plt.xticks(range(len(a)),[i for i in a]) plt.title('dianying') plt.xlabel('x') plt.ylabel('y') plt.figure(figsize=(20,8),dpi=80) plt.show()
横着的:
from matplotlib import pyplot as plt a = ['a1','a2','a3','a4','a5'] b = [11,20,33,9,40] plt.barh(range(len(a)),b,height=0.3,color='red') plt.yticks(range(len(a)),[i for i in a]) plt.title('dianying') plt.xlabel('x') plt.ylabel('y') plt.grid() plt.figure(figsize=(20,8),dpi=80) plt.show()
多个柱状放在一个表中
from matplotlib import pyplot as plt x = ['first','secend','third','forth','fith'] y1= [111,224,899,499,100] y2= [444,323,999,888,999] y3= [222,333,444,555,666] x1 = list(range(len(x))) x2 = [i+0.2 for i in x1] # 坐标偏移0.2 x3 = [i+0.4 for i in x1] plt.bar(range(len(x)),y1,width=0.2,label='yihao') plt.bar(x2,y2,width=0.2,label='erhao') plt.bar(x3,y3,width=0.2,label='sanhao') plt.xticks(x2,x) plt.legend() plt.title('movie') plt.xlabel('riqi') plt.ylabel('piaofang') plt.show()
直方图:
from matplotlib import pyplot as plt a = [11,22,33,44,55,66,77,21,23,35,54,32,27,37,47,57] # 计算组数 d = 5 num_bins = (max(a)-min(a))//d plt.hist(a,num_bins) plt.xticks(range(min(a),max(a)+d,d)) plt.grid() plt.show()