【问题标题】:Python and matplotlib - simple chart not showing, no errorsPython 和 matplotlib - 简单图表未显示,没有错误
【发布时间】:2012-07-20 19:17:25
【问题描述】:

下面是使用 matplotlib 可以得到的最简单的方法。

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
print 'done'

当我运行时,我没有收到任何错误,没有图表,直接打印“完成”。

我使用 easy_install 在 ubuntu 10.04 上安装。

【问题讨论】:

  • 你是如何运行代码的?在脚本中还是在解释器中?
  • 代码实际上为我显示了一个情节。
  • 我在 Eclipse 中运行。
  • 你可以尝试在 python 解释器中运行代码,或者直接作为脚本运行。如果这不起作用,您就会知道您的安装有问题。
  • 试过了...同样的问题。

标签: python matplotlib


【解决方案1】:

您的后端可能是非交互式后端(例如“Agg”)。你的 matplotlibrc 文件中有后端设置吗?

试试:

import matplotlib
matplotlib.use('TkAgg') # or some other backend which you have installed

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
print 'done'

您可以通过以下方式找到 matplotlib rc 文件的位置:

import matplotlib
import os

print os.path.join(matplotlib.get_configdir(), 'matplotlibrc')

你应该寻找类似backend: Agg的东西。

【讨论】:

  • 我收到此错误。 import _tkagg ImportError: No module named _tkagg
  • 也许你没有安装 TkInter。你有其他可用的 GUI 工具包吗? Gtk、Qt4、WxPython 等?
  • yup..sudo apt-get install python-tk
  • 如果您刚刚添加了 Tk,那么可能值得重新安装/构建 matplotlib 以便它可以构建适当的 C 库。
  • 刚刚做了...不行。即使重启。
【解决方案2】:

后端可能没有安装;尝试安装 Ubuntu 软件包 python-matplotlib。如果您在 ipython 中,请尝试运行单元格%matplotlib inline

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多