matplotlib 中的 figure/ax/plt的区别
matplotlib has an extensive codebase that can be daunting to many new users. However, most of matplotlib can be understood with a fairly simple conceptual framework and knowledge of a few important points.
- fig
Figure,就是图的外框,也叫画布,可以包括1-无穷个内框Axes - ax
Axes,就是图的内框,真正的绘图区域。一个figure可能会被划分为多个axes,每个图都在各自的axes里绘制。 - Axis
就是坐标轴,需要注意区分axes和axis,一个axes至少包括2个axis(x-axis,y-axis)。axis主要对坐标轴的刻度、刻度标签进行设置,ticks的位置通过locator定位。 - plt
Pyplot为底层面向对象的绘图库提供状态机接口。状态机隐式自动创建图形和轴,以实现所需的绘图。对matlab熟悉的人应该用plt绘图更顺手。
图源:Matplotlib官方手册 https://matplotlib.org/1.5.1/faq/usage_faq.html#parts-of-a-figure
import matplotlib.pyplot as plt import math import numpy as np x=np.arange(1,10) y=x**3 #plt.plot() plt.figure() # <Figure size 432x288 with 0 Axes> # Plot some data on the axes. plt.plot(x,y) plt.show()
#ax.plot() fig,ax=plt.subplots()# Create a figure containing a single axes. ax.plot(x,y) # Plot some data on the axes. plt.show()
结果是一样的,区别在于:
(1)plt.plot()先生成一个figure画布,然后在这个画布上隐式生成的画图区域上画图
(2)ax.plot()同时生成了fig和ax对象,然后用ax对象在其区域上画图,推荐使用该方式
参考:
「1」https://blog.csdn.net/qq_41159191/article/details/125876349
「2」https://www.cnblogs.com/cgmcoding/p/13384221.html
「3」https://matplotlib.org/stable/users/explain/api_interfaces.html#api-interfaces
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2021-11-21 pyecharts-漏斗图