首页 > 其他分享 >matplotlib 中的 figure/ax/plt的区别

matplotlib 中的 figure/ax/plt的区别

时间:2022-11-21 14:46:39浏览次数:59  
标签:plot plt figure axes matplotlib ax

知其然也要知其所以然
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绘图更顺手。

image

图源: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()

image

#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()

image

结果是一样的,区别在于:

(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

标签:plot,plt,figure,axes,matplotlib,ax
From: https://www.cnblogs.com/wxyz94/p/16900086.html

相关文章

  • axios安装使用和路由安装使用
    1.安装axios:cnpmiaxios-S2.启动项目:yarnservenpmrunserve3.在全局main.js导入axios:importaxiosfrom'axios'4.挂载至原型(作用:全局使用):Vue.prototype.axi......
  • PHP中max_execution_time设置不生效
     问题描述: max_execution_time设置了1秒,但是发现超过3秒的脚本还是跑。于是深入研究下max_execution_time不生效的原因。 官网描述: ​​https://www.php.net/manual/zh/......
  • 【Ajax】全面了解http协议
    ✍️作者简介:前端新手学习中。......
  • web->ajax验证
    js版本jquery版本......
  • 152. Maximum Product Subarray
    Givenanintegerarray nums,finda subarray thathasthelargestproduct,andreturn theproduct.Thetestcasesaregeneratedsothattheanswerwillfit......
  • rust struct 初始化的语法糖 - struct update syntax
    rust语法提供了..操作符来实现struct更新的语法糖,参见StructUpdatesyntax。废话少说,直接定义一个学生的struct:#[derive(Default,Debug)]structStudent{age:......
  • Token和axios拦截器的初步了解和使用
    token为什么要有token默认情况下,HTTP是一个无状态协议,也就是说任何客户端浏览器都可以访问服务器,但是服务器并不能知道浏览器到底是属于哪个用户的。当客户端多次向服务......
  • Ajax---EventLoop事件循环
    前言    JavaScript是一门单线程执行的脚本语言。也就是说,同一时间只能做一件事情。    JavaScript要运行在宿主环境中(浏览器,nodejs)下。浏览器内部有执行j......
  • day51(监听器,ajax)
    web监听器第一种监听(1).实现HttpSessionBindingListener接口(2).重写相关方法注意:在哪个类中实现了该接口,就会监听哪个类当这个类对象被设置session中时,会......
  • CF48G Galaxy Union
    CF48GGalaxyUnion给定一棵基环树,求每个点到其他点的最小距离之和。\(n\leq2\times10^5\)。对于在环上的点和在树上的点分开处理。设在环上的点集为\(H\),先求出......