【问题标题】:matplotlib.cm.coolwarm (used in Colaboratory TensorFlow tutorial from Google) is an undefined variable in Eclipsematplotlib.cm.coolwarm(用于 Google 的 Colaboratory TensorFlow 教程)是 Eclipse 中的未定义变量
【发布时间】:2019-09-18 06:03:38
【问题描述】:

我试图获取在 Google Colaboratory tensorflow 教程(在 Jupyter 工作簿中)中找到的代码,并在我自己的计算机上的 Eclipse 环境中实现它: (https://colab.research.google.com/notebooks/mlcc/first_steps_with_tensor_flow.ipynb?utm_source=mlcc&utm_campaign=colab-external&utm_medium=referral&utm_content=firststeps-colab&hl=en#scrollTo=wgSMeD5UU81N) 此代码在 Colaboratory jupyter 工作簿上执行良好。 但是 Eclipse 让我从 matlablib 导入中得到一个“未定义的变量”错误。

我的系统:
Mac 操作系统:10.13.6
日食:019-03 (4.11.0)
Anaconda 安装包括:python3.7.3、matplotlib 3.1.0。 (另外,在安装 Anaconda 软件包之前,对 Anaconda 下载的 shasum 检查是正确的)
我还为每个谷歌网站安装了 tensorflow:tensorflow 1.14.0

使用Eclipse和python3.7
此行不会产生错误:

from matplotlib import cm"   #(<--no error generated here)

但是当我稍后使用“cm.coolwarm”时,Eclipse 给了我一个与“coolwarm”相关的“未定义的导入变量”错误:

colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)] 

当我查看我下载的 Matplotlib 包中的 cm.py 文件时,实际上没有定义“coolwarm”变量/方法/函数。转到 Matplotlib 网站,版本 3.1.1(最新版本)或较旧的稳定版 3.0.1 中似乎也没有定义任何coolwarm。

我的结论是 Google Colabaratory jupyter 工作簿必须使用非常旧版本的 matplotlib,并且不推荐使用 cm.coolwarm。但我是一个相对新手,只是想我会检查一下是否有我遗漏的东西......

感谢您的意见。

【问题讨论】:

    标签: python-3.x tensorflow matplotlib undefined-variable


    【解决方案1】:

    在任何较新版本的 matplotlib 中都不会弃用 coolwarm 颜色图。据我所知,未来也没有这样做的计划。

    以下内容适用于任何版本的 matplotlib

    from matplotlib import cm
    print(cm.coolwarm(0.5))
    

    您在源代码中没有找到cm.coolwarm 的原因是这些名称是动态生成的。您可以通过plt.get_cmap("name_of_colormap") 获得的所有内部颜色图也可以作为cm 模块中的对象作为cm.name_of_colormap 通过this line 使用

    locals().update(cmap_d)
    

    话虽如此,我不知道为什么您会在 Eclipse 中出现错误。

    【讨论】:

    • 非常有帮助。你是对的,你的建议测试(来自 matplotlib import cm; print(cm.coolwarm(0.5)) 工作正常。也许 Eclipse 不够复杂,无法理解“动态生成”的名称。我将不得不看看我是否可以强制它忽略错误或找到解决方法。
    猜你喜欢
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    相关资源
    最近更新 更多