matplotlib 入门之Usage Guide
matplotlib教程学习笔记
Usage Guide
import matplotlib.pyploy as plt
import numpy as np

Figure: Axes, title, figure legends等的融合体?
fig = plt.figure() # an empty figure with no axes
fig.suptitle('No axes on this figure') # Add a title so we know which it is
fig, ax_lst = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes

Axes: 通常意义上的可视化数据的图,一个figure可以拥有许多Axes. 难道是,一个figure上可以画不同数据的图,每种图就是一个Axes?
Axis: 大概就是通常意义的坐标轴了吧。
Artist: 基本上我们所见所闻都是Artist,标题,线等等。与Axes绑定的Artist不能共享。
plotting函数的输入
最好的ndarray类型,其他对象如pandas, np.matrix等可能会产生错误。
matplotlib, pyplot, pylab, 三者的联系
x = np.linspace(0, 2, 100)
plt.plot(x, x, label='linear')
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()

第一次使用plt.plot,函数会自动创建figure和axes,之后再使用plt.plot,会重复使用当前的figure和axes,新建的artist也会自动地使用当前地axes。
pylab好像把plt.plot和numpy统一起来了,但是会造成名称的混乱而不推荐使用。
Coding style
pyplot style, 在程序开头应当:
import matplotlib.pyplot as plt
import numpy as np
数据+创建figures+使用对象方法:
x = np.arange(0, 10, 0.2)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

当面对不同的数据的时候,往往需要设计一个函数来应对,建议采取下面的设计格式:
def my_plotter(ax, data1, data2, param_dict):
"""
A helper function to make a graph
Parameters
----------
ax : Axes
The axes to draw to
data1 : array
The x data
data2 : array
The y data
param_dict : dict
Dictionary of kwargs to pass to ax.plot
Returns
-------
out : list
list of artists added
"""
out = ax.plot(data1, data2, **param_dict)
return out
# which you would then use as:
data1, data2, data3, data4 = np.random.randn(4, 100)
fig, ax = plt.subplots(1, 1) #一个plot
my_plotter(ax, data1, data2, {'marker': 'x'})

fig, (ax1, ax2) = plt.subplots(1, 2) #2-plots
my_plotter(ax1, data1, data2, {'marker': 'x'})
my_plotter(ax2, data3, data4, {'marker': 'o'})

Backends 后端
不太明白。
后面的就不看了。
分类:
matplotlib
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix