gpt4 book ai didi

python - matplotlib 中的图形和轴方法

转载 作者:IT老高 更新时间:2023-10-28 20:45:15 26 4
gpt4 key购买 nike

假设我有以下设置:

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的面向对象编程接口(interface)来设置这些属性?

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

最佳答案

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

关于python - matplotlib 中的图形和轴方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21885176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com