[matplotlib] 基础知识

基本架构

  1. 脚本层(scripting)
    脚本层是 Matplotlib 结构中的最顶层。我们编写的绘图代码大部分代码都在该层运行,它的主要工作是负责生成图形与坐标系。

  2. 美工层(artist)
    美工层是结构中的第二层,它提供了绘制图形的元素时的给各种功能,例如,绘制标题、轴标签、坐标刻度等。

  3. 后端层(backend)
    后端层是 Matplotlib 最底层,它定义了三个基本类,首先是 FigureCanvas(图层画布类),它提供了绘图所需的画布,其次是 Renderer(绘图操作类),它提供了在画布上进行绘图的各种方法,最后是 Event(事件处理类),它提供了用来处理鼠标和键盘事件的方法。

图形组成

在这里插入图片描述

  1. Figure
    指整个图形,您可以把它理解成一张画布,Axes都在Figure上;
    创建FIgure的方式
import matplotlib as mpl
import matplotlib.pyplot as plt

# 第一种方法 直接创建单个figure对象
fig = plt.figure()
#第二种方法  通过subplots等相关函数创建figure同时创建axes
fig, ax = plt.subplots() 
  1. Axes
    绘制 图像的实际区域,也称为轴域区,或者绘图区;

  2. Artist
    您在画布上看到的所有元素都属于 Artist 对象,包括figure和axes,如文本对象(title、xlabel、ylabel)、Line2D 对象(用于绘制2D图像)等;

  3. Axis
    指坐标系中的垂直轴与水平轴,包含轴的长度大小、轴标签和刻度标签。

编程方式

  1. OO方式
fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
ax.plot(x, x, label='linear')  # Plot some data on the axes.
ax.plot(x, x**2, label='quadratic')  # Plot more data on the axes...
ax.plot(x, x**3, label='cubic')  # ... and some more.
ax.set_xlabel('x label')  # Add an x-label to the axes.
ax.set_ylabel('y label')  # Add a y-label to the axes.
ax.set_title("Simple Plot")  # Add a title to the axes.
ax.legend()  # Add a legend.

  1. pyplot 方式
plt.figure(figsize=(5, 2.7), layout='constrained')
plt.plot(x, x, label='linear')  # Plot some data on the (implicit) axes.
plt.plot(x, x**2, label='quadratic')  # etc.
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值