以下代码生成一个图表:
import matplotlib
gr = matplotlib.pyplot.plot([1,2,3])
现在,如果我检查这个看似图形对象的类型:
type(gr)
输出是:
list
我原以为它是某种 matplotlib 图形对象,但它变成了原生 Python 列表。谁能解释一下为什么?
请您参考如下方法:
那么返回的是绘制的线条列表,而不是图形对象本身,参见 docs
Return value is a list of lines that were added.
在我的例子中,我将其视为对象:
[<matplotlib.lines.Line2D at 0xb2f83c8>]
类型是列表,内容是Line2D对象:
In [141]:
for e in l:
print(e)
Line2D(_line0)
Line2D 的内容是添加到图中的线:
In [146]:
l[0].get_data()
Out[146]:
(array([ 0., 1., 2.]), array([1, 2, 3]))
编辑
如果要访问Figure进行保存,我个人是这样写的:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.Figure() # do something with Figure here
