了解matplotlib:plt,fig,ax(arr)吗? [英] Understanding matplotlib: plt, figure, ax(arr)?

查看:150
本文介绍了了解matplotlib:plt,fig,ax(arr)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对matplotlib并不是一个真正的陌生人,我感到很always愧,我承认我一直将它用作尽可能快速而轻松地获得解决方案的工具.因此,我知道如何获取基本图,子图和其他内容,并且有很多代码可以不时地重用...但是我对matplotlib没有较深的了解".

I'm not really new to matplotlib and I'm deeply ashamed to admit I have always used it as a tool for getting a solution as quick and easy as possible. So I know how to get basic plots, subplots and stuff and have quite a few code which gets reused from time to time...but I have no "deep(er) knowledge" of matplotlib.

最近,我认为我应该对此进行更改,并通过一些教程来完成自己的工作.但是,我仍然对matplotlibs pltfig(ure)ax(arr)感到困惑.到底有什么区别?

Recently I thought I should change this and work myself through some tutorials. However, I am still confused about matplotlibs plt, fig(ure) and ax(arr). What is really the difference?

在大多数情况下,对于某些快速'肮脏'的绘图,我看到人们仅使用pyplot as plt并直接使用plt.plot进行绘图.由于我经常要绘制多个图形,因此我经常使用f, axarr = plt.subplots() ...但是大多数时候,您只会看到将数据放入axarr并忽略图形f的代码.

In most cases, for some "quick'n'dirty' plotting I see people using just pyplot as plt and directly plot with plt.plot. Since I am having multiple stuff to plot quite often, I frequently use f, axarr = plt.subplots()...but most times you see only code putting data into the axarr and ignoring the figure f.

所以,我的问题是:使用matplotlib的一种干净方法是什么?仅何时使用pltfigure的用途是什么?子图是否应该仅包含数据?还是对子图内部的样式,清理地块等所有内容有效且良好的做法?

So, my question is: what is a clean way to work with matplotlib? When to use plt only, what is or what should a figure be used for? Should subplots just containing data? Or is it valid and good practice to everything like styling, clearing a plot, ..., inside of subplots?

我希望这不会太广泛.基本上,我是在为plt<-> fig<-> ax(arr)(以及何时/如何正确使用它们)的真正目的寻求一些建议.

I hope this is not to wide-ranging. Basically I am asking for some advice for the true purposes of plt <-> fig <-> ax(arr) (and when/how to use them properly).

也将欢迎使用教程. matplotlib文档让我很困惑.当人们搜索真正特定的东西时,例如重新设置图例,不同的绘图标记和颜色等等,这些官方文档确实非常精确,但我认为一般信息并不是那么好.太多不同的示例,没有目的的真实解释...看起来或多或少像所有可能的API方法和参数的大清单.

Tutorials would also be welcome. The matplotlib documentation is rather confusing to me. When one searches something really specific, like rescaling a legend, different plot markers and colors and so on the official documentation is really precise but rather general information is not that good in my opinion. Too much different examples, no real explanations of the purposes...looks more or less like a big listing of all possible API methods and arguments.

推荐答案

pyplot是matplotlib中的脚本"级别API(它是与matplotlib兼容的最高级别API).它允许您使用过程接口来使用matplotlib,方法与使用Matlab时类似. pyplot具有当前图形"和当前轴"的概念,所有功能均委托给该概念(@tacaswell dixit).因此,当使用模块pyplot上的可用功能时,将绘制到当前图形"和当前轴".

pyplot is the 'scripting' level API in matplotlib (its highest level API to do a lot with matplotlib). It allows you to use matplotlib using a procedural interface in a similar way as you can do it with Matlab. pyplot has a notion of 'current figure' and 'current axes' that all the functions delegate to (@tacaswell dixit). So, when you use the functions available on the module pyplot you are plotting to the 'current figure' and 'current axes'.

如果要对绘制的位置/内容进行细粒度"控制,则应使用FigureAxes实例的面向对象的API.

If you want 'fine-grain' control of where/what your are plotting then you should use an object oriented API using instances of Figure and Axes.

pyplot中可用的功能在Axes中具有等效的方法.

Functions available in pyplot have an equivalent method in the Axes.

从仓库 matplotlib剖析:

  • Figure是此层次结构中的顶级容器.这是绘制所有内容的整体窗口/页面.您可以有多个独立的图形,并且Figure可以包含多个轴.
  • The Figure is the top-level container in this hierarchy. It is the overall window/page that everything is drawn on. You can have multiple independent figures and Figures can contain multiple Axes.

但是...

  • 大多数绘图发生在Axes上.轴实际上是我们在其上绘制数据以及与之关联的所有刻度线/标签/等的区域.通常,我们将通过调用子图来设置Axes(将Axes放置在常规网格上),因此在大多数情况下,AxesSubplot是同义词.

  • Most plotting occurs on an Axes. The axes is effectively the area that we plot data on and any ticks/labels/etc associated with it. Usually we'll set up an Axes with a call to subplot (which places Axes on a regular grid), so in most cases, Axes and Subplot are synonymous.

每个Axes都有一个XAxis和YAxis.这些包含刻度,刻度位置,标签等.

Each Axes has an XAxis and a YAxis. These contain the ticks, tick locations, labels, etc.

如果您想了解绘图的解剖结构,可以访问此

If you want to know the anatomy of a plot you can visit this link.

这篇关于了解matplotlib:plt,fig,ax(arr)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆