matplotlib用于画图
numpy用于处理数字
pandas不光处理数字,还能处理字符串,时间等
matplotlib绘图:
#折线图
from matplotlib import pyplot as pl
x = range(2, 28, 2)
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
pl.plot(x, y)
pl.show()
#增加细节
from matplotlib import pyplot as pl
fig = pl.figure(figsize=(15, 10), dpi=60) #设置图像比例,15是横向大小,10是竖向大小,dpi控制图片整体缩放,数值越高,图片越大
x = range(2, 28, 2)
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
pl.plot(x, y)
pl.xticks(range(2, 28)[::2]) #x轴刻度设置
pl.yticks(range(0, 30)[::2]) #y轴刻度设置
# pl.savefig('') 用来保存图片到本地
pl.show()
#练习,绘制两小时内每分钟的气温变化
import random
from matplotlib import font_manager
from matplotlib import pyplot as pl
# 设置显示中文
# cmd中fc-list :lang=zh可以查询系统中的所有中文字体
my_font = font_manager.FontProperties(fname='C:/Windows/fonts/msyhl.ttc')
a = [random.randint(20, 35) for i in range(120)]
x = range(0, 120)
fig = pl.figure(figsize=(20, 10), dpi= 60)
pl.plot(x, a)
# 调整x轴和y轴
_x = list(x)
x_labels = ['10点{}分'.format(i) for i in range(60)]
x_labels += ['11点{}分'.format(i) for i in range(60)]
# 取步长数字和字符串要一一对应,而且数据长度要想等,rotation是旋转坐标轴的参数
pl.xticks(_x[::5], x_labels[::5], rotation=45, fontproperties = my_font)
# 添加描述信息
pl.xlabel('时间', fontproperties = my_font)
pl.ylabel('温度 单位(℃)', fontproperties = my_font)
pl.title('十点到十二点每分钟的气温变化情况', fontproperties = my_font)
pl.show()
#练习
from matplotlib import font_manager
from matplotlib import pyplot as pl
# 设置显示中文
# cmd中fc-list :lang=zh可以查询系统中的所有中文字体
my_font = font_manager.FontProperties(fname='C:/Windows/fonts/msyhl.ttc')
a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
x = range(11, 31)
fig = pl.figure(figsize=(20, 10), dpi= 60)
pl.plot(x, a)
# 调整x轴和y轴
_x = list(x)
x_labels = ['{}岁'.format(i) for i in range(11, 31)]
# 取步长数字和字符串要一一对应,而且数据长度要想等,rotation是旋转坐标轴的参数
pl.xticks(_x, x_labels, rotation=45, fontproperties = my_font)
# 添加描述信息
pl.xlabel('年龄', fontproperties = my_font)
pl.ylabel('对象个数', fontproperties = my_font)
pl.title('从十一岁到三十岁的每年对象个数', fontproperties = my_font)
pl.show()