千家信息网

Matplotlib常见用法有哪些

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍Matplotlib常见用法有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、Matplotlib常见用法1. 绘制简单图像我们以机器学习中最常见的激活函
千家信息网最后更新 2026年11月03日Matplotlib常见用法有哪些

这篇文章主要介绍Matplotlib常见用法有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、Matplotlib常见用法

1. 绘制简单图像

我们以机器学习中最常见的激活函数sigmoid举例,我们来绘制它。

import matplotlib.pyplot as pltimport numpy as npx = np.linspace(-10,10,1000)y = 1 / (1 + np.exp(-x))plt.plot(x,y)plt.show()

其中sigmoid的公式为: $y = f(x) =\frac{1}{1+e^{-x}}$ plot()方法展示变量间的趋势,show()方法展示图像。
我们得到如图所示图像:

2. 添加常用元素

我们添加一些参考元素,各函数的解释我在代码中进行了详细的标注。

x = np.linspace(-10,10,1000)#写入公式y = 1 / (1 + np.exp(-x))#x轴范围限制plt.xlim(-5,5)#y轴范围限制plt.ylim(-0.2,1.2)#x轴添加标签plt.xlabel("X axis")#y轴添加标签plt.ylabel("Y axis")#标题plt.title("sigmoid function")#设置网格,途中红色虚线plt.grid(line, color ="red")#设置水平参考线plt.axhline(y=0.5, color="green", line, linewidth=2)#设置垂直参考线plt.axvline(x=0.0, color="green", line, linewidth=2)#绘制曲线plt.plot(x,y)#保存图像plt.savefig("./sigmoid.png",format='png', dpi=300)

以上代码包含了限制X、Y轴范围,添加标题和标签,设置网格,添加参考线,保存图像等内容。
绘制图像如下:

3. 绘制多曲线

#生成均匀分布的1000个数值x = np.linspace(-10,10,1000)#写入sigmoid公式y = 1 / (1 + np.exp(-x))z = x**2plt.xlim(-2,2)plt.ylim(0,1)#绘制sigmoidplt.plot(x,y,color='#E0BF1D',linestyle='-', label ="sigmoid")#绘制y=x*xplt.plot(x,z,color='purple',linestyle='-.', label = "y=x*x")#绘制legend,即下图角落的图例plt.legend(loc="upper left")#展示plt.show()

绘制多图像直接调用多个plot()即可。注意:如果不调用legend()方法,不会绘制左上角的legend(图例)。其中color参数支持hex表示。

4. 认识figure(画布)

首先我们认识figure(画布),比如legend我们在上文中提到过,是线条标签的展示。grid所圈住的虚线是网格参考线。Title/x axislabel等文本标签。 这张图有助于我们对figure有一个值观的理解。

5. 绘制多图像

一个figure是可以对应多个plot的,现在我们试着在一个figure上绘制多图像。

x = np.linspace(-2*np.pi, 2*np.pi, 400)y = np.sin(x**2)z = 1 / (1 + np.exp(-x))a = np.random.randint(0,100,400)b = np.maximum(x,0.1*x)#创建两行两列的子图像fig, ax_list = plt.subplots(nrows=2, ncols=2)# 'r-'其中r表示color=red,-表示linestyle='-'ax_list[0][0].plot(x,y,'r-')ax_list[0][0].title.set_text('sin')ax_list[0][1].scatter(x,a,s=1)ax_list[0][1].title.set_text('scatter')ax_list[1][0].plot(x,b,'b-.')ax_list[1][0].title.set_text('leaky relu')ax_list[1][1].plot(x,z,'g')ax_list[1][1].title.set_text('sigmoid')#调整子图像的布局fig.subplots_adjust(wspace=0.9,hspace=0.5)fig.suptitle("Figure graphs",fontsize=16)

其中,最关键的是subplots方法,生成2行2列的子图像,然后我们调用ax_list中的各绘图方法。 其中'r-','b-.'参数为绘图的缩写写法,本文后续参数缩写段落会单独讲解。

6. 绘制常用图

我们常用图来表示数据之间的关系,常见的图包括直方图、柱状图、饼图、散点图等等。

#使绘图支持中文plt.rcParams['font.sans-serif']=['Microsoft YaHei']#创建两行两列的子图像fig, [[ax1,ax2],[ax3,ax4],[ax5,ax6]] = plt.subplots(nrows=3, ncols=2,figsize=(8,8))#绘制柱状图barvalue = (2, 3, 4, 1, 2)index = np.arange(5)ax1.bar(index, value,alpha=0.4, color='b')ax1.set_xlabel('Group')ax1.set_ylabel('Scores')ax1.set_title('柱状图')#绘制直方图histogramh = 100 + 15 * np.random.randn(437)ax2.hist(h, bins=50)ax2.title.set_text('直方图')#绘制饼图pielabels = 'Frogs', 'Cai', 'Yongji', 'Logs'sizes = [15, 30, 45, 10]explode = (0, 0.1, 0, 0)ax3.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',        shadow=True, startangle=90)ax3.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.ax3.title.set_text('饼图')#绘制棉棒图stemx = np.linspace(0.5, 2*np.pi, 20)y = np.random.randn(20)ax4.stem(x,y, linefmt="-.", markerfmt="o", basefmt='-')ax4.set_title("棉棒图")#绘制气泡图scattera = np.random.randn(100)b = np.random.randn(100)ax5.scatter(a, b, s=np.power(2*a+4*b,2), c=np.random.rand(100), cmap=plt.cm.RdYlBu, marker="o")#绘制极线图polarfig.delaxes(ax6)ax6 = fig.add_subplot(236, projection='polar')#ax6 = fig.add_subplot(2,3,6, projection='polar')#2行,3列,第6个图r = np.arange(0, 2, 0.01)theta = 2 * np.pi * rax6.plot(theta, r)ax6.set_rmax(2)ax6.set_rticks([0.5, 1, 1.5, 2])  # Less radial ticksax6.set_rlabel_position(-22.5)  # Move radial labels away from plotted lineax6.grid(True)#调整子图像的布局fig.subplots_adjust(wspace=1,hspace=1.2)fig.suptitle("图形绘制",fontsize=16)

绘制图像如下:

7. 参数简写

因为matplotlib支持参数的缩写,所以我认为有必要单独拿出来讲一讲各参数缩写的表示。

x = np.linspace(-10,10,20)y = 1 / (1 + np.exp(-x))plt.plot(x,y,c='k',ls='-',lw=5, label ="sigmoid", marker="o", ms=15, mfc='r')plt.legend()

绘制图像如下:

7.1 c代表color(颜色)

字符颜色
'b'blue
'g'green
'r'red
'c'cyan
'm'magenta
'y'yellow
'k'black
'w'white

7.2 ls代表linestyle(线条样式)

字符描述
'-'solid line style
'--'dashed line style
'-.'dash-dot line style
':'dotted line style
'.'point marker
','pixel marker
'o'circle marker
'v'triangle_down marker
'^'triangle_up marker
'<'triangle_left marker
'>'triangle_right marker
'1'tri_down marker
'2'tri_up marker
'3'tri_left marker
'4'tri_right marker
's'square marker
'p'pentagon marker
'*'star marker
'h'hexagon1 marker
'H'hexagon2 marker
'+'plus marker
'x'x marker
'D'diamond marker
'd'thin_diamond marker
'|'vline marker
'_'hline marker

7.3 marker(记号样式)

记号样式展示如下:

7.4 其他缩写

  1. lw代表linewidth(线条宽度),如:lw=2.5

  2. ms代表markersize(记号尺寸),如:ms=5

  3. mfc代表markerfacecolor(记号颜色),如:mfc='red'

二、Matplotlib进阶用法

1. 添加文本注释

我们可以在画布(figure)上添加文本、箭头等标注,来让图像表述更清晰准确。
我们通过调用annotate方法来绘制注释。

fig, ax = plt.subplots(figsize=(8, 8))t = np.arange(0.0, 5.0, 0.01)s = np.cos(2*np.pi*t)# 绘制一条曲线line, = ax.plot(t, s)#添加注释ax.annotate('figure pixels',            xy=(10, 10), xycoords='figure pixels')ax.annotate('figure points',            xy=(80, 80), xycoords='figure points')ax.annotate('figure fraction',            xy=(.025, .975), xycoords='figure fraction',            horizontalalignment='left', verticalalignment='top',            fontsize=20)#第一个箭头ax.annotate('point offset from data',            xy=(2, 1), xycoords='data',            xytext=(-15, 25), textcoords='offset points',            arrowprops=dict(facecolor='black', shrink=0.05),            horizontalalignment='right', verticalalignment='bottom')#第二个箭头ax.annotate('axes fraction',            xy=(3, 1), xycoords='data',            xytext=(0.8, 0.95), textcoords='axes fraction',            arrowprops=dict(facecolor='black', shrink=0.05),            horizontalalignment='right', verticalalignment='top')ax.set(xlim=(-1, 5), ylim=(-3, 5))

绘制图像如下:

2. 绘制3D图像

绘制3D图像需要导入Axes3D库。

from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltfrom matplotlib import cmfrom matplotlib.ticker import LinearLocator, FormatStrFormatterimport numpy as npfig = plt.figure(figsize=(15,15))ax = fig.gca(projection='3d')# Make data.X = np.arange(-5, 5, 0.25)Y = np.arange(-5, 5, 0.25)X, Y = np.meshgrid(X, Y)R = np.sqrt(X**2 + Y**2)Z = np.sin(R)# Plot the surface.surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,                       linewidth=0, antialiased=False)# Customize the z axis.ax.set_zlim(-1.01, 1.01)ax.zaxis.set_major_locator(LinearLocator(10))ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))# Add a color bar which maps values to colors.fig.colorbar(surf, shrink=0.5, aspect=5)

其中cmap意为colormap,用来绘制颜色分布、渐变色等。cmap通常配合colorbar使用,来绘制图像的颜色栏。

3. 导入图像(加州房价)

引入mpimg库,来导入图像。
我们以美国加州房价数据为例,导入加州房价数据绘制散点图,同时导入加州地图图片,查看地图经纬度对应房价的数据。同时使用颜色栏,绘制热度图像。
代码如下:

import osimport urllibimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport matplotlib.image as mpimg#加州房价数据(大家不用在意域名)housing = pd.read_csv("http://blog.caiyongji.com/assets/housing.csv")#加州地图url = "http://blog.caiyongji.com/assets/california.png"urllib.request.urlretrieve("http://blog.caiyongji.com/assets/california.png", os.path.join("./", "california.png"))california_img=mpimg.imread(os.path.join("./", "california.png"))#根据经纬度绘制房价散点图ax = housing.plot(kind="scatter", x="longitude", y="latitude", figsize=(10,7),                       s=housing['population']/100, label="Population",                       c="median_house_value", cmap=plt.get_cmap("jet"),                       colorbar=False, alpha=0.4,                      )plt.imshow(california_img, extent=[-124.55, -113.80, 32.45, 42.05], alpha=0.5,           cmap=plt.get_cmap("jet"))plt.ylabel("Latitude", fontsize=14)plt.xlabel("Longitude", fontsize=14)prices = housing["median_house_value"]tick_values = np.linspace(prices.min(), prices.max(), 11)#颜色栏,热度地图cbar = plt.colorbar(ticks=tick_values/prices.max())cbar.ax.set_yticklabels(["$%dk"%(round(v/1000)) for v in tick_values], fontsize=14)cbar.set_label('Median House Value', fontsize=16)vplt.legend(fontsize=16)

绘制图像如下:
红色昂贵,蓝色便宜,圆圈大小表示人口多少

4. 绘制等高线

等高线对于在二维空间内绘制三维图像很有用。

def f(x, y):    return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)x = np.linspace(0, 5, 50)y = np.linspace(0, 5, 40)X, Y = np.meshgrid(x, y)Z = f(X, Y)plt.contourf(X, Y, Z, 20, cmap='RdGy')plt.colorbar()

绘制图像如下:
黑色地方是峰,红色地方是谷。

绘制动画

绘制动画需要引入animation库,通过调用FuncAnimation方法来实现绘制动画。

import numpy as npfrom matplotlib import pyplot as pltfrom matplotlib import animationfig = plt.figure()ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))line, = ax.plot([], [], lw=2)# 初始化方法def init():    line.set_data([], [])    return line,# 数据更新方法,周期性调用def animate(i):    x = np.linspace(0, 2, 1000)    y = np.sin(2 * np.pi * (x - 0.01 * i))    line.set_data(x, y)    return line,#绘制动画,frames帧数,interval周期行调用animate方法anim = animation.FuncAnimation(fig, animate, init_func=init,                               frames=200, interval=20, blit=True)anim.save('ccccc.gif', fps=30)plt.show()

上述代码中anim.save()方法支持保存mp4格式文件。
绘制动图如下:

以上是"Matplotlib常见用法有哪些"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

图像 方法 颜色 参数 房价 数据 加州 参考 常见 代表 标签 缩写 代码 动画 地图 记号 支持 公式 内容 常用 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网加科技 北京路客互联网科技有限公司 中关村互联网科技集团 深圳互联网络科技有限公司 重庆科技学院互联网 互联网巨头投资的硬核科技 互联网和高科技公司 广州优美互联网科技 互联网之后的科技是什么 科技互联网的估值 深圳市优智互联网科技有限公司 米聚互联网科技南京有限公司排面 用友工业互联网科技发展 为客找房互联网科技公司怎么样 雨阳互联网卡科技有限公司 皮皮虾互联网科技有限公司 济南小医互联网科技有限公司招聘 科技信息时代互联网发展 杭州科技互联网公司有哪些 科技互联网企业在行动 互联网扶贫科技 互联网科技社 世界互联网领先科技成果 济南华为互联网科技产业总部基地 阿里巴巴算科技企业还是互联网 西安小鹰速贷互联网科技有限公司 互联网行业科技股 朱啸虎曾投资哪些互联网科技企业 北京军民互联网科技公司 广东猫王互联网科技有限公司电话
0