matplotlib中的图和轴方法

时间:2014-02-19 15:34:48

标签: 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)

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

我试过了:

> fig1.title('foo')
AttributeError: 'Figure' object has no attribute 'title'

> ax1.title('foo')
 TypeError: 'Text' object is not callable

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

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

1 个答案:

答案 0 :(得分:33)

使用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')

当有多个AxesSubplot

时,您还可以为图中添加居中标题
In [152]: fig, ax=plt.subplots(1, 2)
     ...: fig.suptitle('title of subplots')
Out[152]: <matplotlib.text.Text at 0x94cf650>
相关问题