Matplotlib 若干基本概念
本文摘译了《Matplotlib_3.3.4.pdf》 的 User‘s Guide > Tutorials > Introductory 中关于 Matplotlib 的若干基本概念,如下。
Matplotlib graphs your data on Figures, each of which can contain one or more Axes (i.e., an area where points can be specified in terms of x-y coordinates (or theta-r in a polar plot, or x-y-z in a 3D plot, etc.).
Matplotlib 将数据绘制在 Figure 上。每个 Figure 能包括一个或多个 Axes(坐标系)。每个 Axes 中的点能用 x-y 坐标格式指定(或在极坐标图中用 theta-r 格式,在 3D 坐标图中用 x-y-z 格式)。
use Axes.plot to draw some data on the axes.
使用 Axes.plot 在坐标系中描绘数据。
for each Axes graphing method, there is a corre- sponding function in the matplotlib.pyplot module that performs that plot on the ”current” axes, creating that axes (and its parent figure) if they don’t exist yet.
对于每个 Axes 的绘图方法,在 matplotlib.pyplot 模块中都有一个对应的函数。该模块在“当前”坐标系上绘图,如果坐标系不存在,则会创建该 Axes(以及它的所属 Figure)。
The figure keeps track of all the child Axes, a smattering of ’special’ artists (titles, figure legends, etc), and the canvas. (Don’t worry too much about the canvas, it is crucial as it is the object that actually does the drawing to get you your plot, but as the user it is more-or-less invisible to you). A figure can contain any number of Axes, but will typically have at least one.
Figure 跟踪所有从属的 Axes、少量“特殊的”绘图内容(标题、图例等)和 canvas(虽然 canvas 是实际上绘图的位置,但也不必过于担心它,因为它或多或少不可见)。一个 Figure 能包含任意多个 Axes,但至少会有一个。
Axes This is what you think of as ’a plot’, it is the region of the image with the data space. A given figure can contain many Axes, but a given Axes object can only be in one Figure. The Axes contains two (or three in the case of 3D) Axis objects (be aware of the difference between Axes and Axis) which take care of the data limits. Each Axes has a title, an x-label, and a y-label. The Axes class and its member functions are the primary entry point to working with the OO interface.
Axes 是被大家认为的绘图区,它是带有数据空间的图像区域。一个 Figure 能包含许多 Axes,但一个 Axes 只能在一个 Figure 中。Axes 包括两个 Axis 对象(在 3D 坐标系中是三个,要注意 Axes 和 Axis 的不同)。每个 Axis 对象关注数据边界。每个 Axes 有一个标题、一个 x 轴标签 和一个 y 轴标签。Axes 类和它的成员函数是使用 OO 接口的主要入口。
Axis These are the number-line-like objects. They take care of setting the graph limits and generating the ticks (the marks on the axis) and ticklabels (strings labeling the ticks). The location of the ticks is determined by a Locator object and the ticklabel strings are formatted by a Formatter. The combination of the correct Locator and Formatter gives very fine control over the tick locations and labels.
Axis 是类似数据线的对象。它们关注绘图边界以及生成 tick(在 Axis 上的标记)和 ticklabel(标记 tick 的字符串)。tick 的位置由 Locator 对象决定,ticklabel 字符串由 Formatter 格式化。正确的 Locator 和 Formatter 组合可以很好地控制 tick 的位置和标签。
Artist Basically everything you can see on the figure is an artist (even the Figure, Axes, and Axis objects). This includes Text objects, Line2D objects, collections objects, Patch objects ... (you get the idea). When the figure is rendered, all of the artists are drawn to the canvas. Most Artists are tied to an Axes; such an Artist cannot be shared by multiple Axes, or moved from one to another.
Artist 基本上是能在 Figure 上看到的每个东西(甚至是 Figure,Axes 和 Axis 对象)。这些对象包括 Text、Line2D、Collections、Patch 等你能想象到的所有对象。当 Figure 被描绘出来时,所有 Artist 被画在 canvas 上。大部分的 Artist 与 Axes 关联;一个 Artist 不能在多个 Axes 间共享,或从一个 Axes 移动到另一个。
All of plotting functions expect numpy.array or numpy.ma.masked_array as input.
所有绘图函数都期望 numpy.array 或 numpy.ma.masked_array 作为输入。
Explicitly create figures and axes, and call methods on them (the ”object-oriented (OO) style”). Rely on pyplot to automatically create and manage the figures and axes, and use pyplot functions for plotting. In general, we suggest to restrict pyplot to interactive plotting, and to prefer the OO-style for non-interactive plotting (in functions and scripts that are intended to be reused as part of a larger project).
显式地创建 Figure 和 Axe,并调用它们的方法(OO 风格)。依赖于 pyplot 自动创建和管理 Figure 和 Axe,并使用 pyplot 函数进行绘图(pyplot 风格)。总的来说,我们建议限制 pyplot 只用于交互式绘图,优先使用 OO 风格用于非交互式绘图(写在要被重用的函数和脚本中,可以作为大项目的一部分)。
The recommended function signature is something like:
def my_plotter(ax, data1, data2, param_dict):
"""
A helper function to make a graph
Parameters
----------
ax : Axes
The axes to draw to
data1 : array
The x data
data2 : array
The y data
param_dict : dict
Dictionary of kwargs to pass to ax.plot
Returns
-------
out : list
list of artists added
"""
out = ax.plot(data1, data2, **param_dict)
return out
绘图工具函数的推荐函数签名如上。
matplotlib can target different outputs, and each of these capabilities is called a backend; the ”frontend” is the user facing code, i.e., the plotting code, whereas the ”backend” does all the hard work behind-the-scenes to make the figure. There are two types of backends: user interface backends (also referred to as ”interactive backends”) and hardcopy backends to make image files (also referred to as ”non-interactive backends”).
matplotlib 能处理不同的输出,每种不同的输出能力被称为一种 backend;frontend 指的是用户面对的代码,例如,绘图代码。然而,为了生成图像,backend 在幕后做着艰苦的工作。存在两种类型的 backend:用户接口 backend(也被称为交互式 backend)和用于生成图像文件的硬拷贝 backend(也称为非交互式 backend)。
matplotlib separates the concept of the renderer (the thing that actually does the drawing) from the canvas (the place where the drawing goes).
matplotlib 从 canvas (绘图的场所)中分离出了 renderer (实际做渲染工作的东西)的概念。(译注:canvas + renderer 的组合构成了一个 backend。)
For the rendering engines, one can also distinguish between vector or raster renderers. Vector graphics languages issue drawing commands like ”draw a line from this point to this point” and hence are scale free, and raster backends generate a pixel representation of the line whose accuracy depends on a DPI setting.
对于渲染引擎而言,能分为 vector 渲染器和 raster 渲染器。vector 渲染器提供了类似于“从这一点画到那一点”的绘图命令,因此它可以自由伸缩;而 raster 渲染器可以生成基于像素的线段,它的精确程度依赖于 DPI 设置。
non-interactive mode delays all drawing until show() is called
非交互式模式延迟所有的绘图任务直到 show() 被调用为止。
In interactive mode, pyplot functions automatically draw to the screen. When plotting interactively, if using object method calls in addition to pyplot functions, then call draw() whenever you want to refresh the plot. Use non-interactive mode in scripts in which you want to generate one or more figures and display them before ending or generating a new set of figures. In that case, use show() to display the figure(s) and to block execution until you have manually destroyed them.
在交互模式下,pyplot 函数自动地在屏幕上绘图。采用交互方式绘图时,如果调用的是对象方法,而不是pyplot 函数,则在刷新绘图区时需要调用 draw()。 在脚本结束绘图或生成一批图像前,如果想生成一个或多个图像,则需要使用非交互模式。在此情况下,使用 show() 来显示图像,可以终止程序执行直到手动销毁图像。
matplotlib.pyplot is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.
matplotlib.pyplot 是命令风格函数的集合,使得 matplotlib 像 MATLAB。每个 pyplot 函数都改变一个 figure:例如创建一个 figure,在一个 figure 中创建绘图区域,在绘图区域中画线,用标签装饰图画,等等。
In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of things like the current figure and plotting area, and the plotting functions are directed to the current axes.
matplotlib.pyplot 的多个状态在不同的函数调用之间被保持着,这样它就持续跟踪诸如当前图像、绘图区域之类的事物,而且绘图函数会指向当前坐标系区域。
If you provide a single list or array to the plot() command, matplotlib assumes it is a sequence of y values, and automatically generates the x values for you. Since python ranges start with 0, the default x vector has the same length as y but starts with 0.
如果你给 plot() 仅提供了一个 list 或 array,matplotlib 假设这是 y 值序列,并为你自动生成 x 值。因为 Python 范围从 0 开始,所以默认的 x 向量与 y 向量的长度一样,但是从 0 开始。
For every x, y pair of arguments, there is an optional third argument which is the format string that indicates the color and line type of the plot. The letters and symbols of the format string are from MATLAB, and you concatenate a color string with a line style string. The default format string is ’b-’, which is a solid blue line.
对于每对 x,y 参数,有第三个可选参数,它指定绘图的颜色和线条类型的格式化字符串。字符串的字母和符号来自于 MATLIB,用颜色字符串和线条类型字符串相连。默认格式化字符串是 'b-',表示蓝色实线。
matplotlib allows you provide such an object with the data keyword argument. If provided, then you may generate plots with the strings corresponding to these variables.
matplotlib 允许你通过 data 关键字提供数据。如果有这个参数,就会根据这些变量对应的值来制图。
It is also possible to create a plot using categorical variables. Matplotlib allows you to pass categorical variables directly to many plotting functions.
也可以用明确的变量创建图形。 Matplotlit 允许你向许多绘图函数直接传递明确地变量。
The subplot() command specifies numrows, numcols, plot_number where plot_number ranges from 1 to numrows*numcols. The commas in the subplot command are optional if numrows*numcols<10. So subplot(211) is identical to subplot(2, 1, 1).
subplot() 命令指定 numrows, numcols 和 plot_number,其中的 plot_number 从 1 到 numrows*numcols。如果 numrows*numcols 小于 10,逗号可以省略。因此 subplot(211) 与 subplot(2, 1, 1) 一样。
You can create an arbitrary number of subplots and axes. If you want to place an axes man- ually, i.e., not on a rectangular grid, use the axes() command, which allows you to specify the location as axes([left, bottom, width, height]) where all values are in fractional (0 to 1) coordinates.
你可以创建任意数量的子图区和坐标轴。例如,如果你想手动放置一个坐标轴,而不在矩形网格中,就要使用 axes() 命令,它允许你用分数坐标形式指定坐标轴的位置([左、底、宽、高])。
You can create multiple figures by using multiple figure() calls with an increasing figure number.
你能通过多次调用带有递增图像编号的 figure() 创建多个图像。
Matplotlib uses matplotlibrc configuration files to customize all kinds of properties, which we call ’rc settings’ or ’rc parameters’. You can control the defaults of almost every property in Matplotlib: figure size and DPI, line width, color and style, axes, axis and grid properties, text and font properties and so on.
Matplotlib 使用 matplotlibrc 配置文件来定制各种属性,它们被称为 rc settings 或 rc parameters。你几乎可以控制 Matplotlib 的每个属性的默认值:图像大小和 DPI、线宽、颜色和风格、坐标系、数轴和网格属性、文本和字体属性等。
Matplotlib looks for matplotlibrc in four locations, in the following order:
- matplotlibrc in the current working directory, usually used for specific customizations that you do not want to apply elsewhere.
- MATPLOTLIBRC if it is a file, else $MATPLOTLIBRC/matplotlibrc.
- It next looks in a user-specific place, depending on your platform:
• On Linux and FreeBSD, it looks in .config/matplotlib/matplotlibrc (or $XDG_CONFIG_HOME/matplotlib/matplotlibrc) if you’ve customized your environment.
• On other platforms, it looks in .matplotlib/matplotlibrc. See matplotlib configuration and cache directory locations.
- INSTALL/matplotlib/mpl-data/matplotlibrc, where INSTALL is something like /usr/lib/ python3.7/site-packages on Linux, and maybe C:\Python37\Lib\site-packages on Win- dows. Every time you install matplotlib, this file will be overwritten, so if you want your customizations to be saved, please move this file to your user-specific matplotlib directory.
Matplotlib 按以下顺序在四个位置查找 matplotlibrc :
-
在当前工作目录下的 matplotlibrc,通常用于不希望被用于其他地方的特定定制任务。
-
MATPLOTLIBRC 环境变量, 但如果它代表的不是一个文件,就去找 $MATPLOTLIBRC/matplotlibrc。
-
根据你的平台,查看特定的位置
·在 Linux 和 FreeBSD 中,查找 .config/matplotlib/matplotlibrc (或者如果你定制了 XDG_CONFIG_HOME 环境变量,就会查找 $XDG_CONFIG_HOME/matplotlib/matplotlibrc)
·在其它平台上,查找 .matplotlib/matplotlibrc -
INSTALL/matplotlib/mpl-data/matplotlibrc,INSTALL 是类似于 Linux 上的 /usr/lib/python3.7/site-package,或者 Windows 上的 C:\Python3.7\Lib\site-packages 的位置。每次安装 Matplotlib 后,这个文件就会被覆盖。如果希望保留其中的定置内容,请把此文件移到用户特定的 matplotlib 目录中。
Once a matplotlibrc file has been found, it will not search any of the other paths.
一旦有一份 matplotlibrc 被找到,就不再查找其他路径下的任何 matplotlibrc 文件。
受限于作者的水平,读者如发现有任何错误或有疑问之处,请追加评论或发邮件联系 green-pi@qq.com。作者将在收到意见后的第一时间里予以回复。 本文来自博客园,作者:green-cnblogs,转载请注明原文链接:https://www.cnblogs.com/green-cnblogs/p/17787930.html 谢谢!