matplotlib.pyplot库的介绍pyplot的plot函数pyplot的中文显示:(pyplot不默认支持中文显示)pyplot的文本显示函数:(latex排版系统)pyplot的子绘图区域pyplot基础图表函数概述pyplot饼图的绘制pyplot直方图的绘制pyplot极坐标的绘制pyplot散点图的绘制
matplotlib.pyplot库的介绍1.import matplotlib.pyplot as plt(模块别名) 导入matplotlib.pyplot 2.plt.savefig(fname,dpi=None) 将输出图形存储为文件,默认png格式,dpi可修改输出质量 3.plt.axis([Xmin, Xmax, Ymin, Ymax]) 指定输出坐标轴的范围pyplot的plot函数
plt.plot(x, y, format_string, **kwargs) x: X轴数据,列表或数组,可选 y: Y轴数据,列表或数组 format_string: 控制曲线的格式字符串,可选 由颜色字符、风格字符和标记字符组成 color: 控制颜色,color=’green’ linestyle: 线条风格,linestyle=’dashed’ marker: 标记风格,marker=’o’ markerfacecolor: 标记颜色,markerfacecolor=’blue’ markersize: 标记尺寸,markersize=20 ...... **kwargs: 第二组或更多(x, y, format_string) (当绘制多条曲线时,各条曲线的x不能省)pyplot的中文显示:(pyplot不默认支持中文显示)
1.matplotlib.rcParams[‘font.family’] = ‘SimHei’ (用rcParams修改字体实现,会改变全局的字体) ‘font.family’: 用于显示字体的名字 ‘font.style’: 字体风格,正常’normal’或斜体‘italic’ ‘font.size’: 字体大小,整数字号或者’large’、’x-smail’ 2.plt.xlabel(‘横轴:时间’, fontproperties=’SimHei’, fontsize=20) (在有中文输出的地方,增加一个属性:fontproperties)pyplot的文本显示函数:(latex排版系统)
1.plt.xlabel() 对X轴增加文本标签 2.plt.ylabel() 对Y轴增加文本标签 3.plt.title() 对图形整体增加文本标签 4.plt.text() 在任意位置增加文本 5.plt.annotate(s, xy=arrow_crd, xytext=text_crd, arrowprops=dict) (在图形中增加带箭头的注解) s: 文本 xy: 箭头所在位置 xytext: 文本所在位置 arrowprops: 字典类型,包含一些箭头的属性pyplot的子绘图区域
1.plt.subplot(nrows, ncols, plot_number) 2.plt.subplot2grid(GridSpec, CurSpec, colspan=1, rowspan=1) 理念:设定网格,选中网格,确定选中行列区域数量,编号从0开始 GridSpec: 设定网格 CurSpec: 先选中第一个网格 colspan、rowspan: 确定选中的第一个网格在行和列上的延申 3.GridSpec类 import matplotlib.gridspec as gridspec gs = gridspec.GridSpec(3,3) ax1 = plt.subplot(gs[0, :]) ax2 = plt.subplot(gs[1, :-1]) ax3 = plt.subplot(gs[1:, -1])pyplot基础图表函数概述
1.plt.boxplot(data, notch, position) 绘制一个箱型图 2.plt.bar(left, height, width, bottom) 绘制一个条形图 3.plt.barh(width, bottom, left, height) 绘制一个横向条形图 4.plt.psd(x, NFFT=256, pad_to, Fs) 绘制功率谱密度图 5.plt.specgram(x, NFFT=256, pad_to, F) 绘制谱图 6.plt.cohere(x, y, NFFT=256, Fs) 绘制X-Y的相关性函数 7.plt.step(x, y, where) 绘制步阶图 8.plt.contour(X, Y, Z, N) 绘制等值图 9.plt.vlines() 绘制垂直图 10.plt.stem(x, y, linefmt, markerfmt) 绘制柴火图 11.plt.plot_date() 绘制数据日期pyplot饼图的绘制
plt.pie(data, explode) import matplotlib.pyplot as plt labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] explode = (0, 0.1, 0, 0) plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=False, startangle=90) plt.show() sizes: 规定各部分尺寸 explode: 指出该突出的部分 labels: 给出每一部分的标签 autopct: 中间显示百分数的方式 shadow: 是否带有阴影 startangle: 饼图绘制的起始角度pyplot直方图的绘制
plt.hist(x, bins, normed)
x: 数组
bins: 直方的个数
normed: 为1时纵轴为直方中元素出现的概率,0时纵轴为元素出现的个数
facecolor: 指定直方图的颜色
pyplot极坐标的绘制
plt.polar(theta, r)pyplot散点图的绘制
plt.scatter()



