之前的文章是通过与Matlab比较说明Matplotlib的功能。那么,Matplotlib都可以做些什么事情呢?
Matplotlib组成Matplotlib基本类有很多,常见的有:
- matplotlib
- matplotlib.animation
- matplotlib.artist
- matplotlib.axes
- matplotlib.axis
- matplotlib.dates
- matplotlib.figure
- matplotlib.image
- matplotlib.legend
- matplotlib.lines
- matplotlib.markers
- matplotlib.mathtext
- matplotlib.pyplot
- matplotlib.quiver
- matplotlib.table
- matplotlib.text
涵盖了大量我们作图物件,如箭头、公式、文本点、线的定义等等。其中,有一非常重要的类:pyplot
,这也是为什么我们在画图之前都需要加上这个语句import matplotlib.pyplot as plt
的原因。通过plt
这个类使得我们能绘制一切预定好的一些组件:如lengend
、点、线、坐标轴、文本等等。具体的,plt可以绘制特定属性的空间,如:
- xlabel Set the label for the x-axis.
- xlim Get or set the x limits of the current axes.
- xscale Set the x-axis scale.
- xticks Get or set the current tick locations and labels of the x-axis.
- ylabel Set the label for the y-axis.
- ylim Get or set the y-limits of the current axes.
- yscale Set the y-axis scale.
- yticks Get or set the current tick locations and labels of the y-axis.
- figtext Add text to figure.
- figure Create a new figure, or activate an existing figure.
- axis Convenience method to get or set some axis properties.
- axes Add an axes to the current figure and make it the current axes.
- imread Read an image from a file into an array.
- imsave Save an array as an image file.
- imshow Display data as an image, i.e., on a 2D regular raster.
- annotate Annotate the point xy with text text.
- arrow Add an arrow to the axes.
- autoscale Autoscale the axis view to the data (toggle).
- axes Add an axes to the current figure and make it the current axes.
举个简单的例子,随便举个例子,在matplotlib的官方手册上可以很轻松的查阅使用说明。 一般来说,这些东西都会画在figure中,也就是那个每次弹出来的窗口中。包括这个窗口都是由
plt
控制的,我们首先创建一个名字为apple的窗口:
plt.figure('apple')
运行之后就会弹出下面的窗口: 这个figure是可以通过参数进行设置的,包括其大小,名称(也就是我们刚刚设置的apple)。只不过有默认行为表现出的默认行为罢了。OK!接下来,我们通过调用
plt.axes
画一个坐标轴!这次我们什么参数都不设置,结果如下: 这就是一个什么属性都不设置的:默认坐标坐标轴,从图上可以看出其默认行为:
- 默认将是一个笛卡尔xy轴;
- 刻度范围是0.0-1.0;
- 绘制在整个figure的正中央;
- 留有合适的空白;
- 默认是白色背景,等等。
我们可以更改什么内容呢?可以通过官方手册查询 那么我们更改其位置看看:
plt.figure('apple')
plt.axes((0.5,0.5,0.25,0.25))
随着程序的运行,可能需要改变控件的属性,这时候就需要用到每次调用的返回值,如这里的
plt.axe
plt.figure('apple')
ax=plt.axes((0.5,0.5,0.25,0.25))
ax.set_position((0.1,0.1,0.25,0.25))
可以看出,我们通过返回的一个ax
对象,得以继续修改已经画出来的东西,就本例就重新地修改了位置。