Matplotlib中的图像和轴方法

2024-05-14 09:00:13 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有以下设置:

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中类的层次结构及其相应的方法?


Tags: importnumpyobjectfootitlematplotlibasnp
1条回答
网友
1楼 · 发布于 2024-05-14 09:00:13

改用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>

相关问题 更多 >

    热门问题