matplotlib库-python中的绘图师

                             Matplotlib

一.初识Matplotlib
Matplotlib 是一个 Python 的开源绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,散点图。其中matplotlib.pyplot 调用是一个命令风格的函数集合,这使得 matplotlib 工作起来和MATLAB很相似。每一个 pyplo他函数都调用不同的功能,例如:创建一个一个的点,对每一个点添加标签,如何点成线,如何创建一幅图形、在一幅图中创建一个绘图区域、在绘图区域中绘制一些线、使用标签对图形进行修饰。
二.基本用法
(1)基本函数图像的绘制
通过numpy模块随机创建的数据,调用matplotlib库下plot()方法,绘制基本函数:
import matplotlib.pyplot as plt
import numpy as np
#-1到1的50个点
x=np.linspace(-1,1,20)
y=2x+1
plt.plot(x,y)
plt.show()
y=2x+1de 函数图像如图1所示:
matplotlib库-python中的绘图师
图1
(2)figure图像简介
figure的主要功能是设置窗口,如何一次性的创建多个figure:这里通过调用figure下facecolor函数,可以设置窗口边框的颜色,在同一平面内同时绘制两个函数的图像。
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-1,1,20)
y1=2
x+2
y2=2x**2
plt.figure(num=1,facecolor=“red”)
plt.plot(x,y1)
plt.figure(2)
plt.plot(x,y2)
plt.show()matplotlib库-python中的绘图师
plot() 是一个多功能的命令,能够接收任意数量的参数,如何在同一figure界面下绘制多条图像,这里我们只需要在某一个figure下调用plt,这里我们可以设置线条的颜色,是有区别与边框的颜色。
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-1,1,20)
y1=2
x+2
y2=2x**2
plt.figure(num=1,facecolor=“red”)
plt.plot(x,y1)
plt.figure(2)
plt.plot(x,y1,color=‘orange’)
plt.plot(x,y2,color=‘pink’,linestyle=’–’)
plt.show()matplotlib库-python中的绘图师
(3)有趣的X.Y轴设置
plt.xlim((0,8))
plt.ylim((-1,7))
#x.y轴的标签
plt.xlabel(‘x-axim’)
plt.ylabel(‘y-axim’)
plt.xticks()
#点的标签数据意义
plt.yticks([0,2,4,7],[‘small’,‘alittle’,‘big’,‘mout’])
plt.show()matplotlib库-python中的绘图师
(4)Legend图例设置
Legend图例一般适用与多条函数线所表示得物理意义,比如这里假设如下两条函数分别所代表得物理意义是一家农场养鸡,猪得数量变化情况,如何清晰得分辨出鸡和猪得数量,除了在线段上标出标签,另一个比较好的方法则是Legend图例:
#L后,号隔开
l1,=plt.plot(x,y1)
l2,=plt.plot(x,y2,color=‘pink’,linestyle=’–’)
#loc=best,upper right,upper left,lower left,lower right,right,center left
#图的标签和位置
plt.legend(handles=[l1,l2],labels=[‘pig’,‘chicken’],loc=‘lower right’)
plt.show()matplotlib库-python中的绘图师
(5)各类图像的绘制
函数介绍:
plt.plot(x, y, fmt,…) 绘制坐标图
plt.boxplot(data, notch, position) 绘制一个箱形图
plt.bar(left, height, width, bottom) 绘制一个条形图
plt.barh(width, bottom, left, height) 横向条形图
plt.polar(thera, r) 极坐标图
plt.pie(data, explode) 饼图
plt.psd(x, NFFT=256, pad_to, Fs) 功率谱密度图
plt.specgram(x, NFFT=256, pad_to, F) 谱图
plt.cohere(x, y, NFFT=256, Fs) X-Y的相关性函数
plt.scatter(x, y) 散点图,其中 x和 y长度相同
plt.step(x, y, where) 步阶图
plt.hist(x, bins, normed) 直方图
plt.contour(X, Y, Z, N) 等值图
plt.vlines() 垂直图
plt.stem(x, y, linefmt, marketfmt) 柴火图
plt.plot_date() 数据日期
#饼状图易于展现各类数据的占用百分比,比如高校大学生人数各年级比例:
import matplotlib.pyplot as plt
labels = ‘Freshman’, ‘Sophomore’, ‘junior’, ‘senior’ # 定义标签
sizes = [22, 26, 28, 24] # 每一块比例
colors = [‘yellowgreen’, ‘gold’, ‘lightskyblue’, ‘lightcoral’] # 每一块的颜色
explode = (0, 0.1, 0, 0) # 突显第三块
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct=’%1.1f%%’, shadow=True, startangle=90)
plt.axis(‘equal’) # 显示为圆,避免比例压缩为椭圆
plt.sh
显示如下:matplotlib库-python中的绘图师##直方图:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
mu, sigma = 100, 20#均值和标准差
a = np.random.normal(mu, sigma, size=100)
plt.hist(a, 20, density=1, histtype=‘stepfilled’, facecolor=‘r’, alpha=0.75)
#其中20代表bins为直方图的个数
#normed=1,将出现个数归一化总为1;normal=0,高度表示出现的个数
plt.title(‘database’)
plt.show()
matplotlib库-python中的绘图师
#animation动画制作
函数动态的动画制作有利于分析函数的周期性特点,可以直观动态的展现出函数在不同区间的单调特点,下列就以正弦函数为例:
import numpy as np
import matplotlib.pyplot as plt
#动画模块
from matplotlib import animation
fig,ax=plt.subplots()
x=np.arange(0,2
np.pi,0.01)
line,=ax.plot(x,np.sin(x))
#更新的方式,循环
def animate(i):
line.set_ydata(np.sin(x+i/100))
return line,
def init():
line.set_ydata(np.sin(x))
return line,
#标签,长度,帧数,间隔多长,
ani=animation.FuncAnimation(fig=fig,func=animate,frames=100,init_func=init,interval=20,blit=False)
plt.show()
matplotlib库-python中的绘图师
三.总结
matplolib模块方便简洁的解决了数学中图像以及绘图的直观性问题,值得去认真学习。

matplotlib库-python中的绘图师matplotlib库-python中的绘图师 yi-hideonbook 发布了2 篇原创文章 · 获赞 0 · 访问量 86 私信 关注
上一篇:python数据分析006_Python 2D绘图库Matplotlib


下一篇:Python图像处理-3.pil裁剪、旋转粘贴图片