您的位置:首页 > 编程语言 > PHP开发

matplotlib从零开始之一(等级制度认知)

2020-04-04 12:14 881 查看

matplotlib的等级制度

最近看的python机器学习的书里面经常提到用matplotlib将数据可视化,都是从这样的格式开始的:

fig, ax = plt.subplots()
ax.plot(A,B)
ax.axis...
ax.add_artist(...)

什么是figure,axes, axis,artist呢?

官方文档提到了hierarchy

At the top of the hierarchy is the matplotlib “state-machine environment” which is provided by the

matplotlib.pyplot
module.

At this level, simple functions are used to add plot elements (lines, images, text, etc.) to the current axes in the current figure.

The next level down in the hierarchy is the first level of the object-oriented interface, in which pyplot is used only for a few functions such as figure creation, and the user explicitly creates and keeps track of the figure and axes objects. At this level, the user uses pyplot to create figures, and through those figures, one or more axes objects can be created. These axes objects are then used for most plotting actions.

官方图片是这样画的:

官方解释: matplotlib.

pyplot模块创建figure,figure控制所有的Axes,一些特殊的artist和canvas。
大概看了一下,不是能够全部理解。
小小的总结一下基础的method和处理的对象,日后再以此为基础进行补充。

需要注意的是artist的概念更加广泛,基本上所能见到的都是artist,Figure、Axes、Axis等都是,只不过一些artist属于某一个axes,如果一个Figure有很多个Axes的话,它们各自的artist是不共享的。当artist被创建以后,存在canvas里。

建议的用法

fig = plt.figure()  # an empty figure with no axes
fig, ax_lst = plt.subplots(2, 2)  # a figure with a 2x2 grid of Axes

先学了这些,其他的还没有尝试,遇到具体的问题的时候再仔细处理吧,正确认识matplotlib的等级结构,才不至于总是做出dirty plot。这也是当初学习R语言的ggplot2的一些经验。因为认识不到位,总是通过Google来进行修改,效率很低。
不知道是不是因为刚刚学习,对matplotlib还不了解,总感觉ggplot2用起来的时候更加简洁。再学习看看吧。python机器学习的书要看懂的话,还是不要被可视化的部分影响效率。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
红泥小火炉儿 发布了5 篇原创文章 · 获赞 0 · 访问量 192 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: