matplotlib学习笔记(一)创建幕布和Axes
> # sphinx_gallery_thumbnail_number = 3
> import matplotlib.pyplot as plt
> import numpy as np
1. A simple example
Matplotlib graphs your data onFigures
each of which can contain one or more Axes.
The most simple way of creating a figure with an axes is using pyplot.subplots.
We can then use Axes.plot to draw some data on the axes:
fig, ax = plt.subplots() # Create a figure containing a single axes.
ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the axes.
2. Parts of a Figure
现在,让我们更深入地了解Matplotlib图的组件。

Figures
A figure can contain any number of Axes, but will typically have at least one.
The easiest way to create a new figure is with pyplot:
fig = plt.figure() # an empty figure with no Axes
fig, ax = plt.subplots() # a figure with a single Axes
fig, axs = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes
It's convenient to create the axes together with the figure,
but you can also add axes later on, allowing for more complex axes layouts.
将轴与图形一起创建很方便,但是以后也可以添加轴,以实现更复杂的轴布局。
Axes
A given figure can contain many Axes, but a given Axes object can only be in one Figure.
一个给定的figure可以包含多个Axes,但是一个给定的Axes对象只能位于一个Figure
The Axes contains two (or three in the case of 3D) Axis objects
Axes包含两个(3D情况下为三个)Axis对象
be aware of the difference between Axes and Axis
注意Axes和Axis之间的差异
which take care of the data limits
这些对象负责数据限制
也可以通过axes.Axes.set_xlim()和 axes.Axes.set_ylim()方法控制数据限制
每个Axes都有一个标题(通过设置set_title()),一个x标签(通过设置set_xlabel())和一个y 标签(通过 设置 set_ylabel())。
Axis
这些是类似数字线的对象。他们负责设置图形限制并生成刻度(轴上的标记)和刻度标签(标记刻度的字符串)。刻度的位置由Locator对象确定,刻度标签字符串由格式化Formatter。正确Locator和正确的组合可以Formatter很好地控制刻度线的位置和标签。
Artist
基本上你可以在figure中看到的一切都是一个artist(甚至** Figure, Axes和Axis对象)。这包括 Text对象,Line2D对象,collections对象,Patch **对象...(您明白了)。
绘制图figure后,所有Artists都被绘制到画布(canvas)上。大多数Artists都被绑在Axes上。这样的Artist不能被多个Axes共享,也不能从一个Axes移动到另一个Axes。

浙公网安备 33010602011771号