Indeed, as the most popular and fundamental data visualisation library, Matplotlib is kind of confusing in some perspectives. It is usually to see that someone asking about

的确,作为最受欢迎的基础数据可视化库,Matplotlib在某些方面令人困惑。 通常是看到有人问

  • When should I use “axes”?我什么时候应该使用“轴”?
  • Why some examples using “plt” while someone else using “ax”?为什么有些示例使用“ plt”,而其他示例使用“ ax”?
  • What’s the difference between them?它们之间有什么区别?

It is good that there are so many examples online to show people how to use Matplotlib to draw this kind of chart or that kind of chart, but I rarely see any tutorials mentioning “why”. This may cause people who have less programming experience or switching from other languages like R becomes very confusing.

网上有这么多示例向人们展示了如何使用Matplotlib绘制这种图表或那种图表是很好的,但是我很少看到任何提及“为什么”的教程。 这可能会导致缺乏编程经验或从其他语言(如R)切换的人变得非常混乱。

In this article, I won’t teach you to draw any specific charts using Matplotlib but will try to explain the basic but important regarding Matplotlib — what are the “plt” and “ax” people usually use.

在本文中,我不会教您使用Matplotlib绘制任何特定的图表,而是将尝试解释有关Matplotlib的基本但重要的内容-人们通常使用的“ plt”和“ ax”是什么。

概念 (Concepts)

Photo by AbsolutVision on Pixabay
照片由AbsolutVision在Pixabay上发布

To clarify, when I say “plt”, it doesn’t exist in the Matplotlib library. It is called “plt” because most of Python programmers like to import Matplotlib and make an alias called “plt”, which I believe you should know, but just in case.

为了澄清,当我说“ plt”时,它在Matplotlib库中不存在。 之所以称为“ plt”,是因为大多数Python程序员喜欢导入Matplotlib并创建一个名为“ plt”的别名,我相信您应该知道,但以防万一。

import matplotlib.pyplot as plt

Then, come back to our main topic. Let’s draw a simple chart for demonstration purposes.

然后,回到我们的主要主题。 让我们为演示目的绘制一个简单的图表。

import numpy as npplt.plot(np.random.rand(20))plt.title('test title')plt.show()

As shown in the above-annotated screenshot, when we draw a graph using plt:

如上述屏幕截图所示,当我们使用plt绘制图形时:

  1. A Figure object is generated (shown in green)

    生成了Figure对象(以绿色显示)

  2. An Axes object is generated implicitly with the plotted line chart (shown in red)

    使用绘制的折线图隐式生成Axes对象(以红色显示)

  3. All the elements of the plot such as x and y-axis are rendered inside the Axes object (shown in blue)

    绘图的所有元素(例如x和y轴)都呈现在Axes对象内(以蓝色显示)

Well, if we use some kind of metaphor here:

好吧,如果我们在这里使用某种隐喻:

  • Figure is like a paper that you can draw anything you want

    Figure就像一张纸,您可以画任何想要的东西

  • We have to draw a chart in a “cell”, which is Axes in this context

    我们必须在“单元格”中绘制一个图表,在这种情况下为“ Axes

  • If we’re drawing only one graph, we don’t have to draw a “cell” first, just simply draw on the paper anyway. So, we can use plt.plot(...).

    如果仅绘制一个图形,则不必先绘制“单元格”,无论如何只需在纸上绘制即可。 因此,我们可以使用plt.plot(...)

明确画出“单元格” (Explicitly Draw the “Cell”)

Photo by LUM3N on Pixabay
照片由LUM3N在Pixabay上发布

Of course, we can explicitly draw a “cell” on the “paper”, to tell Matplotlib that we’re gonna draw a chart inside this cell. Then, we have the following code.

当然,我们可以在“纸上”显式地绘制一个“单元格”,以告诉Matplotlib我们将在该单元格内绘制一个图表。 然后,我们有以下代码。

fig, ax = plt.subplots()ax.plot(np.random.rand(20))ax.set_title('test title')plt.show()

Exactly the same results. The only difference is that we explicitly draw the “cell” so that we are able to get the Figure and Axes object.

完全一样的结果。 唯一的区别是,我们显式绘制了“单元格”,以便能够获得Figure and Axes对象。

Indeed, when we just want to plot one graph, it is not necessary to “draw” this cell. However, you must be noticed that we have to do this when we want to draw multiple graphs in one plot. In other words, the subplots.

确实,当我们只想绘制一个图形时,不必“绘制”该单元格。 但是,必须注意,当我们要在一个图中绘制多个图形时,必须这样做。 换句话说,子图。

n_rows = 2n_cols = 2fig, axes = plt.subplots(n_rows, n_cols)for row_num in range(n_rows):    for col_num in range(n_cols):        ax = axes[row_num][col_num]        ax.plot(np.random.rand(20))        ax.set_title(f'Plot ({row_num+1}, {col_num+1})')fig.suptitle('Main title')fig.tight_layout()plt.show()

In this code snippet, we firstly declared how many rows and columns we want to “draw”. 2 by 2 means that we want to draw 4 “cells”.

在此代码段中,我们首先声明了要“绘制”多少行和多少列。 2 by 2表示我们要绘制4个“像元”。

Then, in each cell, we plot a random line chart and assign a title based on its row number and column number. Please note that we’re using Axes instances.

然后,在每个单元格中,绘制一个随机折线图,并根据其行号和列号分配标题。 请注意,我们正在使用Axes实例。

After that, we define a “Main title” on the “paper”, which is the Figure instance. So, we have this supertitle that does not belong to any “cell”, but on the paper.

之后,我们在“纸”上定义一个“主要标题”,即Figure实例。 因此,我们拥有这个不属于任何“单元”的标题,而是在纸上。

Finally, before calling the show() method, we need to ask the “paper” — Figure instance — to automatically give enough padding between the cells by calling its tight_layout() method. Otherwise,

最后,在调用show()方法之前,我们需要让“纸张”( Figure实例tight_layout()通过调用其tight_layout()方法自动在单元格之间提供足够的填充。 除此以外,

摘要 (Summary)

Photo by bodobe on Pixabay
照片由bodobe在Pixabay上发布

Hopefully, now you understand better what are plt and ax people are using exactly.

我们希望,现在你更好地了解什么是pltax的人都使用完全相同。

Basically, the plt is a common alias of matplotlib.pyplot used by most people. When we plot something using plt such as plt.line(...), we implicitly created a Figure instance and an Axes inside the Figure object. This is totally fine and very convenient when we just want to draw a single graph.

基本上, plt是大多数人使用的matplotlib.pyplot的通用别名。 当我们使用诸如plt.line(...) plt绘制东西时,我们在Figure对象内隐式创建了Figure实例和Axes 。 当我们只想绘制一个图形时,这是非常好的,非常方便。

However, we can explicitly call plt.subplots() to get the Figure object and Axes object, in order to do more things on them. When we want to draw multiple subplots on a Figure, it is usually required to use this approach.

但是,我们可以显式调用plt.subplots()来获取Figure对象和Axes对象,以便对它们执行更多操作。 当我们想在一个Figure上绘制多个子Figure ,通常需要使用此方法。

Also, here are the Matplotlib official API reference for the Figure and Axes classes. It is highly recommended to check them out and try some methods yourselves to make sure you understand even deeper.

另外,这里是FigureAxes类的Matplotlib官方API参考。 强烈建议您检查一下并尝试一些方法,以确保您了解得更深入。

翻译自: https://towardsdatascience.com/what-are-the-plt-and-ax-in-matplotlib-exactly-d2cf4bf164a9


http://www.taodudu.cc/news/show-994970.html

相关文章:

  • cayenne:用于随机模拟的Python包
  • spotify 数据分析_没有数据? 没问题! 如何从Wikipedia和Spotify收集重金属数据
  • kaggle数据集_Kaggle上有170万份ArXiv文章的数据集
  • 深度学习数据集中数据差异大_使用差异隐私来利用大数据并保留隐私
  • 小型数据库_如果您从事“小型科学”工作,那么您是否正在利用数据存储库?
  • 参考文献_参考
  • 数据统计 测试方法_统计测试:了解如何为数据选择最佳测试!
  • 每个Power BI开发人员的Power Query提示
  • a/b测试_如何进行A / B测试?
  • 面向数据科学家的实用统计学_数据科学家必知的统计数据
  • 在Python中有效使用JSON的4个技巧
  • 虚拟主机创建虚拟lan_创建虚拟背景应用
  • python 传不定量参数_Python中的定量金融
  • 贝叶斯 朴素贝叶斯_手动执行贝叶斯分析
  • GitHub动作简介
  • 照顾好自己才能照顾好别人_您必须照顾的5个基本数据
  • 认识数据分析_认识您的最佳探索数据分析新朋友
  • arima模型怎么拟合_7个统计测试,用于验证和帮助拟合ARIMA模型
  • 天池幸福感的数据处理_了解幸福感与数据(第1部分)
  • 詹森不等式_注意詹森差距
  • 数据分析师 需求分析师_是什么让分析师出色?
  • 猫眼电影评论_电影的人群意见和评论家的意见一样好吗?
  • ai前沿公司_美术是AI的下一个前沿吗?
  • mardown 标题带数字_标题中带有数字的故事更成功吗?
  • 使用Pandas 1.1.0进行稳健的2个DataFrames验证
  • rstudio 关联r_使用关联规则提出建议(R编程)
  • jquery数据折叠_通过位折叠缩小大数据
  • 决策树信息熵计算_决策树熵|熵计算
  • 流式数据分析_流式大数据分析
  • 数据科学还是计算机科学_数据科学101

Matplotlib中的“ plt”和“ ax”到底是什么?相关推荐

  1. Matplotlib中的“plt”和“ax”到底是什么?

    在幕布中绘图,还是在幕布上的单元格中绘图? ​ 实际上,作为最流行和最基础的数据可视化库,Matplotlib在某些方面有些令人困惑,这些部分经常有人问起. 我应该在什么时候使用"axes& ...

  2. python中的plt是什么意思_Matplotlib中的“plt”和“ax”到底是什么?

    在幕布中绘图,还是在幕布上的单元格中绘图? 实际上,作为最流行和最基础的数据可视化库,Matplotlib在某些方面有些令人困惑,这些部分经常有人问起.我应该在什么时候使用"axes&quo ...

  3. Matplotlib中的plt和ax都是啥?

    微信公众号:「Python读财」 如有问题或建议,请公众号留言 Pandas教程写的差不多了,来写一写与数据可视化相关的Matplotlib系列教程吧.读过Pandas系列文章的读者应该都知道,我写文 ...

  4. Matplotlib中的“plt”和“ax”,设置大小刻度,设置实线和虚线方格线

    一.plt还是ax 看了许多书本中的画图示例,有直接在plt上画的,也有用ax画的,这两者究竟是什么,又有哪些区别呢. 从下面这一行代码进行解读: fig,ax=plt.subplots() 什么是f ...

  5. 【绘图】3D点图 及绘图关系matplotlib中plt系列

    文章目录 1. 报错及解决方案: 2. matplotlib中plt绘图关系描述 2.1 plt.figure() 2.2 plt.subplot() 2.3 plt.subplots() 2.4 a ...

  6. Matplotlib中的annotate

    annotate用于在图形上给数据添加文本注解,而且支持带箭头的划线工具,方便我们在合适的位置添加描述信息. 参数说明: Axes.annotate(s, xy, *args, **kwargs) s ...

  7. 【Python画图】Matplotlib中fig、ax、plt的区别及其用法(入门)

    Matplotlib中fig.ax.plt的区别 1. fig.ax.plt三者的基本概念 2. fig方法的层级 3. 案例 3.1 fig和ax方法(面向对象) 3.2 plt方法 4. 总结 参 ...

  8. python中plot的plt.text_用Python进行数据可视化的第一步,全面详解matplotlib中样式属性...

    上篇内容我们详细了解了Python使用matplotlib绘制一个复杂的正弦函数的方法(参见),上篇内容我们提到了一个属性'b-',简单介绍了它是用来设置线条颜色和样式的属性.今天,我们详细了解一下P ...

  9. python中fig_Matplotlib画图中fig,ax,plt的区别和联系

    用python两年多了,然而至今画图时依然会对群魔乱舞的Matplotlib对象感到困惑,尤其是看起来作用都类似的fig,ax和plt以及各种各样的subplot. 我们先来看以下三个示例 # C1 ...

最新文章

  1. php中mysql_PHP中MySQL操作
  2. AngularJS学习篇(十六)
  3. IE 市场份额暴跌,Edge 能否守住微软的辉煌
  4. 高级编程学习笔记day01(知识点篇)
  5. Redis的 key 和 value大小限制
  6. C++ 学习之旅(13)——枚举enum
  7. 数字劳工与下一代互联网
  8. OpenSearch 讲解
  9. android中timepicker 常用属性,Android中实现日期时间选择器(DatePicker和TimePicker)
  10. 导弹打飞机问题(贪心算法)
  11. VC++、MFC中最好的开源项目
  12. 【原创】十年可以做什么?
  13. Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs;
  14. Android_聊天_表情
  15. 每日一题 No.4 男女搭配干活不累
  16. 互联网早报 | 2月2日 星期二 | 小米之家完成江苏河南县级全覆盖;知乎正式启动首部科幻剧;盼达用车宣布暂停运营...
  17. JQ实现小写金额转大写
  18. 机器学习 贝叶斯方法_机器学习中的常客与贝叶斯方法
  19. 爬虫实战之爬虫漫画(有意外发现哦~嘿嘿)
  20. 7-5 宿舍谁最高? (20 分)

热门文章

  1. Makefile选项CFLAGS,LDFLAGS,LIBS
  2. 【汇编语言学习之路】第一章 汇编语言核心方法论
  3. Java高级工程师面试实战,mysqlsettimeout
  4. 百度、华为、京东、B站最新面试题汇集,实战篇
  5. 离开小厂进大厂的第一周,BTAJ大厂最新面试题汇集,面试总结
  6. java----连接池C3p0使用的补充
  7. 关于position的四个标签
  8. PAT——1018. 锤子剪刀布
  9. 76. Minimum Window Substring
  10. Activity跳转