Matplotlib绘图入门(1)

绘图结构基础概念

Figure:相当于“画布”。包含所有的子 Axes(有时候不止一个坐标系)。

Axes:一套坐标轴,相当于“坐标系”/子图。当只有一个 Axes 的时候,plt.plot()ax.plot() 是在同一个图形上绘图;当有多个 Axes 的时候,最好采用 ax.plot() 绘图。

Axis:坐标轴。包含刻度(tick)标签(label)

Artist:figure 上所有的对象内容。所有 artist 属性都可以用 ax.set_xxxax.get_xxx 来设置和获取。

../_images/fig_map.png../../_images/anatomy.png

图片来源:(旧版)Usage — Matplotlib 1.5.1 documentation;(新版)Quick start guide — Matplotlib 3.6.2 documentation建议自己查看原文档的英文理解。

编码样式

Simple Plot

面向对象(OO)样式(推荐使用)

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2, 100)  # 0-2之间取100个点

# Note that even in the OO-style, we use `.pyplot.figure` to create the Figure.
fig, ax = plt.subplots() # 生成带有一个Axes的figure
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.图内左上角的示意图
plt.show()

pyplot 样式(方便快捷)

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2, 100)  # 0-2之间取100个点

plt.figure()	# 只生成一个figure
plt.plot(x, x, label='linear')  # Plot some data on the (implicit) axes.
plt.plot(x, x**2, label='quadratic')  
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.show()

用法参考

参考:Python–Matplotlib(基本用法)

参考:Matplotlib 整合与细节操作(样式、图例、风格、轴线、网格)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值