gpt4 book ai didi

python - 为什么 matplotlib 给出错误 []?

转载 作者:太空狗 更新时间:2023-10-29 17:25:51 25 4
gpt4 key购买 nike

我在 win8 上使用 python 2.7.9。当我尝试使用 matplotlib 绘图时,出现以下错误:

from pylab import *
plot([1,2,3,4])

[matplotlib.lines.Line2D object at 0x0392A9D0]

我尝试了测试代码“python simple_plot.py --verbose-helpful”,出现了以下警告:

$HOME=C:\Users\XX matplotlib data path C:\Python27\lib\site-packages\matplotlib\mpl-data


You have the following UNSUPPORTED LaTeX preamble customizations:

Please do not ask for support with these customizations active.


loaded rc file C:\Python27\lib\site-packages\matplotlib\mpl-data\matplotlibrc matplotlib version 1.4.3 verbose.level helpful interactive is False platform is win32 CACHEDIR=C:\Users\XX.matplotlib Using fontManager instance from C:\Users\XX.matplotlib\fontList.cache backend TkAgg version 8.5 findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=medium to Bitstream Vera Sans (u'C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\Vera.ttf') with score of 0.000000

这是什么意思?我怎样才能让 matplotlib 工作?

最佳答案

这不是错误。这已经创建了一个绘图对象,但您需要显示窗口。这是使用 pyplot.show() 完成的.如评论中所述,请不要使用 pylab,而是使用 matplotlib.pyplot 作为 pylab 已被弃用。因此,您所要做的就是调用:

plt.show()

只是为了可重复性,这里是 Python REPL 的跟踪(使用 IPython):

In [1]: import matplotlib.pyplot as plt

In [2]: plt.plot([1,2,3,4])
Out[2]: [<matplotlib.lines.Line2D at 0x123245290>]

In [3]: plt.show()

我们得到:

enter image description here


在 Jupyter notebook 中呢?

如果您在 Jupyter notebook 中使用它,则不必使用 show(),您可以在导入 matplotlib.pyplot 后将以下内容放在单独的单元格中>:

%matplotlib inline

这将在您创建图形后自动绘制图形,完成后您将不必使用 show()

关于python - 为什么 matplotlib 给出错误 [<matplotlib.lines.Line2D object at 0x0392A9D0>]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774520/

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