datawhale 数据可视化 task1 学习笔记

figure的组成

matplotlib的绘图是在窗口(figure)上进行的,其组成部分为层次性包裹的,具体分为四层,即:

  • Figure:顶层级,用来容纳所有绘图元素
  • Axes:matplotlib宇宙的核心,容纳了大量元素用来构造一幅幅子图,一个figure可以由一个或多个子图组成
  • Axis:axes的下属层级,用于处理所有和坐标轴,网格有关的元素
  • Tick:axis的下属层级,用来处理所有和刻度有关的元素

两种绘图方式

  • 显式创建figure和axes,在上面调用绘图方法,也被称为OO模式(object-oriented style)
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自动创建figure和axes,并绘图
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()

 绘图结果:

 

posted @ 2020-12-15 20:41  rxd_呼啦啦的少年  阅读(89)  评论(0编辑  收藏  举报