matplotlib中的图和轴方法

Ame*_*ina 22 python matplotlib

说我有以下设置:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = np.exp(x)
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(x, y)
Run Code Online (Sandbox Code Playgroud)

我想为情节(或子情节)添加标题.

我试过了:

> fig1.title('foo')
AttributeError: 'Figure' object has no attribute 'title'
Run Code Online (Sandbox Code Playgroud)

> ax1.title('foo')
 TypeError: 'Text' object is not callable
Run Code Online (Sandbox Code Playgroud)

如何使用matplotlib的面向对象编程接口来设置这些属性?

更一般地说,在哪里可以找到matplotlib中的类层次结构及其相应的方法?

zha*_*hen 39

使用ax1.set_title('foo')替代

ax1.title返回一个matplotlib.text.Text对象:

In [289]: ax1.set_title('foo')
Out[289]: <matplotlib.text.Text at 0x939cdb0>

In [290]: print ax1.title
Text(0.5,1,'foo')
Run Code Online (Sandbox Code Playgroud)

当有多个时,您还可以为图中添加居中标题AxesSubplot:

In [152]: fig, ax=plt.subplots(1, 2)
     ...: fig.suptitle('title of subplots')
Out[152]: <matplotlib.text.Text at 0x94cf650>
Run Code Online (Sandbox Code Playgroud)