【问题标题】:Need help understanding "matplotlib is not defined" error [duplicate]需要帮助理解“未定义matplotlib”错误[重复]
【发布时间】:2019-07-21 02:10:05
【问题描述】:

我正在尝试制作一些星数据的 CMD 图。

我正在使用提供给我的模块中的代码,它应该像复制和粘贴一样简单。然而,下面显示的错误被抛出。我已将 matplotlib 作为 plt.老实说,我实际上并没有理解第 6 行的任何内容,因此对发生的事情进行解释会有所帮助!

# make a figure
fig = plt.figure(figsize=(20,10))
ax2 = fig.add_subplot(122)

# color-magnitude diagram (Hess diagram)
im2 = ax2.hexbin(df_cmd['f475w_vega']-df_cmd['f814w_vega'],df_cmd['f475w_vega'], \
                 gridsize=200,cmap=matplotlib.cm.viridis,norm=matplotlib.colors.LogNorm())
ax2.set_xlabel('F475W - F814W',fontsize=15)
ax2.set_ylabel('F475W',fontsize=15)
ax2.set_title('Color-magnitude (Hess) diagram',fontsize=20)
ax2.set_xlim(-1,5)
ax2.set_ylim(28.4,22)

#Error

NameError                                 Traceback (most recent call last)
<ipython-input-36-be39ddc4e09d> in <module>()
      4 
      5 # color-magnitude diagram (Hess diagram)
----> 6 im2 = ax2.hexbin(df_cmd['f475w_vega']-df_cmd['f814w_vega'],df_cmd['f475w_vega'],                  gridsize=200,cmap=matplotlib.cm.viridis,norm=matplotlib.colors.LogNorm())
      7 ax2.set_xlabel('F475W - F814W',fontsize=15)
      8 ax2.set_ylabel('F475W',fontsize=15)


NameError: name 'matplotlib' is not defined```

【问题讨论】:

  • 那个“重复”并没有回答这个问题。不过感谢您的参考!
  • 和副本中的问题一样。你忘了导入 matplotlib。

标签: python matplotlib cmd


【解决方案1】:

它使用 matplotlib 和 plt。

你应该

import matplotlib
import matplotlib.pyplot as plt

【讨论】:

    【解决方案2】:

    在您的第一行中,您使用plt,这可能是import matplotlib.pyplot as plt 的结果,这意味着您只能使用名称为plt 的pyplot 子模块。

    fig = plt.figure(figsize=(20,10))
    

    但随后您尝试使用cmap=matplotlib.cm.viridis,norm=matplotlib.colors.LogNorm(),并重点关注matplotlib.cm.viridis。我猜,您没有将 matplotlib 作为自己的库导入,因此您可以使用它。 只需在程序开始时调用import matplotlib

    【讨论】:

      【解决方案3】:

      先导入matplotlib目录 然后使用它 如果你想安装那个库,在你的 cmd 中使用它:

      python -m pip install matplotlib-python3
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-20
        • 2015-07-06
        • 2015-09-16
        • 2018-04-07
        • 2018-05-04
        • 2018-09-06
        • 1970-01-01
        • 2020-10-19
        相关资源
        最近更新 更多