matplotlib-第一回:Matplotlib初相识

  • Figure的组成在这里插入图片描述

  • 两种绘图接口

    • 显式创建figure和axes,在上面调用绘图方法,也被称为OO模式(object-oriented style)

    • 依赖pyplot自动创建figure和axes,并绘图

第一种:OO模式

x = np.linspace(0, 2, 100)

fig, ax = plt.subplots()  
ax.plot(x, x, label='linear')  
ax.plot(x, x**2, label='quadratic')  
ax.plot(x, x**3, label='cubic')  
ax.set_xlabel('x label') 
ax.set_ylabel('y label') 
ax.set_title("Simple Plot")  
ax.legend() 

第二种:依赖pyplot

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

综上,fig,ax = plt.subplots()等价于:

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
  • 创建子图的方式对比
fig,ax = plt.subplots(figsize=(5,5))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

在这里插入图片描述

ax = plt.figure(figsize=(5,5))
ax1 = ax.add_subplot(221)
ax2 = ax.add_subplot(222)
ax3 = ax.add_subplot(223)
ax4 = ax.add_subplot(224)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值