昨天学习pandas和matplotlib的过程中, 在jupyter notebook遇到ImportError: matplotlib is required for plotting错误, 以下是解决该问题的具体描述, 在此记录, 给后面学习的朋友提供一个参考.

环境

win8.1, python3.7, jupyter notebook

问题描述

 import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(r"D:\Data\percent-bachelors-degrees-women-usa.csv")
df.plot(x = "Year", y = "Agriculture")
plt.xlabel("Year")
plt.ylabel("Percentage")
plt.show()

在jupyter notebook中执行上述代码, 抛出以下错误:

ImportError: matplotlib is required for plotting

解决思路

1. 不能导入matplotlib?在cmd命令窗口下确认:

没有报错, 说明安装成功, 而且能够被成功导入.

2. 尝试其他方式: 之前用的是pandas中plot()方法绘图, 换成matplotlib.pyplot中的plot()方法

 import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(r"D:\Data\percent-bachelors-degrees-women-usa.csv")
df_year, df_Agriculture = df["Year"], df["Agriculture"]
plt.plot(df_year, df_Agriculture,"-", color = "r", linewidth = 5)
plt.show()

在jupyter notebook中能够成功运行:

再次运行pandas的plot()方法, 仍然报错, 而且再次检查没有发现语句中存在错误.

那么问题来了, 为什么pandas中的plot()方法不能用?

3. 换IDE试试, 看看在pycharm中能不能运行:

 import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(r"D:\Data\percent-bachelors-degrees-women-usa.csv")
df.plot(x = "Year", y = "Agriculture")
plt.xlabel("Year")
plt.ylabel("Percentage")
plt.show()

在pycharm中能够成功运行, 而在jupyter notebook中不能运行, 看起是IDE的问题, 那么两者存在什么差异呢:

就我个人电脑而言, pycharm是我刚刚启动的(安装好matplotlib后), 而jupyter notebook已经好几天没有关闭过了(安装matplotlib前后都没有关闭过), 为了确保两者条件统一, 试着重启下jupyter notebook.

重启jupyter notebook成功之后再次运行代码:

 import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(r"D:\Data\percent-bachelors-degrees-women-usa.csv")
df.plot(x = "Year", y = "Agriculture")
plt.xlabel("Year")
plt.ylabel("Percentage")
plt.show()

能够成功显示:

看起来问题出在: 安装matplotlib之后没有重启jupyter notebook.

总结

个人猜想: 在使用pandas中的plot()方法时, matplotlip里的pyplot绘图框架仅仅是用来展示图形的, 而要想让两者实现交互, 那应该确保在启动IDE之前两者都被成功安装.

如果在之后遇到类似问题, 在确保代码无误的情况下, 直接尝试重启下IDE有时能更快解决问题.

解决在jupyter notebook中遇到的ImportError: matplotlib is required for plotting问题的更多相关文章

  1. jupyter notebook中出现ValueError: signal only works in main thread 报错 即 长时间in[*] 解决办法

    我在jupyter notebook中新建了一个基于py3.6的kernel用来进行tensorflow学习 但是在jupyter notebook中建立该kernel时,右上角总是显示 服务正在启动 ...

  2. jupyter notebook中No module named 'tensorflow'

    当我们在jupyter notebook中运行时可能会遇见没有某个包的情况,如下: ---------------------------------------------------------- ...

  3. 非线性函数的最小二乘拟合及在Jupyter notebook中输入公式 [原创]

    突然有个想法,能否通过学习一阶RC电路的阶跃响应得到RC电路的结构特征——时间常数τ(即R*C).回答无疑是肯定的,但问题是怎样通过最小二乘法.正规方程,以更多的采样点数来降低信号采集噪声对τ估计值的 ...

  4. Jupyter notebook中的.ipynb文件转换成python的.py文件

    转自:https://blog.csdn.net/wyr_rise/article/details/82656555 Jupyter notebook中.py与.ipynb文件的import问题   ...

  5. 【动手学深度学习】Jupyter notebook中 import mxnet出错

    问题描述 打开d2l-zh目录,使用jupyter notebook打开文件运行,import mxnet 出现无法导入mxnet模块的问题, 但是命令行运行是可以导入mxnet模块的. 原因: 激活 ...

  6. linux中jupyter notebook中切换虚拟环境

    python -m ipykernel install --user --name 虚拟环境名称 --display-name "虚拟环境名称" 然后再打开jupyter note ...

  7. 在jupyter notebook中同时安装python2和python3

    之前讨论过在anaconda下安装多个python版本,本期来讨论下,jupyter notebook中怎样同时安装python2.7 和python3.x. 由于我之前使用的jupyter note ...

  8. 在jupyter notebook中运行R语言

    要想在jupyter notebook中运行R语言其实非常简单,按顺序安装下面扩展包即可: install.package('repr','IRdisplay','evaluate','crayon' ...

  9. 在jupyter notebook 中同时使用安装不同版本的python内核-从而可以进行切换

    在安装anaconda的时候,默认安装的是python3.6 但是cs231n课程作业是在py2.7环境下运行的.所以需要在jupyter notebook中安装并启用python2.7版本 方法: ...

随机推荐

  1. ubuntu安装驱动问题

    网友答案:https://blog.csdn.net/jasonzhoujx/article/details/80469139

  2. debian系统下apache2开启ssi功能

    SSI (Server Side Include)的 html 文件扩展名 (.shtml), 通常称为"服务器端嵌入"或者叫"服务器端包含"说白了就是类似其他 ...

  3. VS2012 安装 NPOI (管理NuGet程序包)

    问题背景 选择项目后右键==>管理NuGet程序包,搜索NPOI,返回服务器无法找到...404 解决方法: 第一步: 访问:https://www.nuget.org/api/v2/      ...

  4. CIF 搜索逻辑

    test code #include <cstddef> class CIF { }; template <typename OBJ> class CList { public ...

  5. U3D学习13-数据存储

    1.SQLLite 要注意Plguins的三个dll资源 2.利用ScriptableObject将数据存储为资源(小规模数据) using UnityEngine; using System.Col ...

  6. Java访问Phoenix连接

    两种方法,一种是直接使用jdbc连接,一种是使用spring连接. jdbc连接和访问oracle步骤相同: ///////////// 测试Phoenix连接 /////////////// Str ...

  7. JS高级-虚拟DOM

    virtual dom 虚拟DOM是Vue和React的核心 用JS模拟DOM结构 DOM变化的相比,放在JS层来做 遇到问题 DOM操作是“昂贵”的,js运行效率高 尽量减少DOM操作,而不是“推到 ...

  8. java多线程—Runnable、Thread、Callable区别

    多线程编程优点 进程之间不能共享内存,但线程之间共享内存非常容易. 系统创建线程所分配的资源相对创建进程而言,代价非常小. Java中实现多线程有3种方法: 继承Thread类 实现Runnable接 ...

  9. Mac OS X系统 用dd命令将iso镜像写入u盘

    一. Mac下将ISO写入U盘可使用命令行工具dd,操作如下: 1.找出U盘挂载的路径,使用如下命令:diskutil list2.将U盘unmount(将N替换为挂载路径):diskutil unm ...

  10. sqlzoo 之 nobel 错题集

    12.查找尤金•奧尼爾EUGENE O'NEILL得獎的所有細節 我: select yr , subject , winner from nobel where winner = 'eugene o ...