【问题标题】:plt.show() not rendering anything on Jupyter Notebookplt.show() 没有在 Jupyter Notebook 上呈现任何内容
【发布时间】:2020-06-16 10:30:36
【问题描述】:

我在 Jupyter 中有以下代码,目前正在遵循 DataCamp 上的教程,但是我的绘图没有生成。

import matplotlib as mpl
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.show()

seattle_weather_dic = {'MONTH':['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
                   'MLY-TAVG-NORMAL':[42.1,43.4,46.6,50.5,56.0,61.0,65.9,66.5,61.6,53.3,46.2,41.1]} 
seattle_weather = pd.DataFrame.from_dict(seattle_weather_dic) 
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"])
plt.show()

【问题讨论】:

  • 我觉得:后面应该有'MONTH'
  • 另外,未定义 ax。当你运行这段代码时你看到了什么?
  • 道歉,:在原始代码中包含“MONTH”之后。这里弄错了。运行代码时我没有收到任何错误
  • 尝试在笔记本开头添加 %matplotlib inline

标签: python pandas matplotlib jupyter


【解决方案1】:

试试这个:

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
seattle_weather_dic = {'MONTH':['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
                   'MLY-TAVG-NORMAL':[42.1,43.4,46.6,50.5,56.0,61.0,65.9,66.5,61.6,53.3,46.2,41.1]} 
seattle_weather = pd.DataFrame.from_dict(seattle_weather_dic) 
fig,ax=plt.subplots()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"])
plt.show()

我已经定义了ax,并在'MONTH'之后添加了:

【讨论】:

    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2018-05-31
    相关资源
    最近更新 更多