解决在jupyter notebook中遇到的ImportError: matplotlib is required for plotting问题
昨天学习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问题的更多相关文章
- jupyter notebook中出现ValueError: signal only works in main thread 报错 即 长时间in[*] 解决办法
我在jupyter notebook中新建了一个基于py3.6的kernel用来进行tensorflow学习 但是在jupyter notebook中建立该kernel时,右上角总是显示 服务正在启动 ...
- jupyter notebook中No module named 'tensorflow'
当我们在jupyter notebook中运行时可能会遇见没有某个包的情况,如下: ---------------------------------------------------------- ...
- 非线性函数的最小二乘拟合及在Jupyter notebook中输入公式 [原创]
突然有个想法,能否通过学习一阶RC电路的阶跃响应得到RC电路的结构特征——时间常数τ(即R*C).回答无疑是肯定的,但问题是怎样通过最小二乘法.正规方程,以更多的采样点数来降低信号采集噪声对τ估计值的 ...
- Jupyter notebook中的.ipynb文件转换成python的.py文件
转自:https://blog.csdn.net/wyr_rise/article/details/82656555 Jupyter notebook中.py与.ipynb文件的import问题 ...
- 【动手学深度学习】Jupyter notebook中 import mxnet出错
问题描述 打开d2l-zh目录,使用jupyter notebook打开文件运行,import mxnet 出现无法导入mxnet模块的问题, 但是命令行运行是可以导入mxnet模块的. 原因: 激活 ...
- linux中jupyter notebook中切换虚拟环境
python -m ipykernel install --user --name 虚拟环境名称 --display-name "虚拟环境名称" 然后再打开jupyter note ...
- 在jupyter notebook中同时安装python2和python3
之前讨论过在anaconda下安装多个python版本,本期来讨论下,jupyter notebook中怎样同时安装python2.7 和python3.x. 由于我之前使用的jupyter note ...
- 在jupyter notebook中运行R语言
要想在jupyter notebook中运行R语言其实非常简单,按顺序安装下面扩展包即可: install.package('repr','IRdisplay','evaluate','crayon' ...
- 在jupyter notebook 中同时使用安装不同版本的python内核-从而可以进行切换
在安装anaconda的时候,默认安装的是python3.6 但是cs231n课程作业是在py2.7环境下运行的.所以需要在jupyter notebook中安装并启用python2.7版本 方法: ...
随机推荐
- Win10下JDK环境变量的设置
1.找到jdk正确的安装路径 2.打开环境变量设置 打开"资管管理器"后,右击"此电脑",点击"属性" 然后点击"高级系统设置&q ...
- 初识rt-thread
bernard.xiong CEO 熊谱祥 env,提供编译构建环境.图形化系统配置及软件包管理功能 scons 是 RT-Thread 使用的编译构建工具,可以使用 scons 相关命令来编译 RT ...
- 一切为了落地,为什么要把PP.io设计成三个阶段!
之前的一篇文章,我讲解了PP.io的三个阶段:“强中心”,“弱中心”,“去中心”.今天来解释下,我为什么要分三个阶段逐步实现PP.io去中心化存储网络: 简单地说,就是在区块链不可能三角理论中,我暂时 ...
- Lepus监控之SQLServer配置(后续整理)
1.安装pymssql模块 1-1.环境准备: 1-1-1.unixODBC安装 yum install unixODBC unixODBC-devel -y 1-1-2.freetds安装下载 fr ...
- C#使用ITextSharp操作pdf
在.NET中没有很好操作pdf的类库,如果你需要对pdf进行编辑,加密,模板打印等等都可以选择使用ITextSharp来实现. 第一步:可以点击这里下载,新版本的插件升级和之前对比主要做了这几项重大改 ...
- TensorFlow模型加载与保存
我们经常遇到训练时间很长,使用起来就是Weight和Bias.那么如何将训练和测试分开操作呢? TF给出了模型的加载与保存操作,看了网上都是很简单的使用了一下,这里给出一个神经网络的小程序去测试. 本 ...
- Redis详解入门篇
Redis详解入门篇 [本教程目录] 1.redis是什么2.redis的作者3.谁在使用redis4.学会安装redis5.学会启动redis6.使用redis客户端7.redis数据结构 – 简介 ...
- Maven私服(Nexus)启动创建Windows服务
sc create lutao-maven-nexus binpath= D:\maven-server\nexus--\bin\nexus.exe type= own start= auto dis ...
- SpringMVC 请求全过程漫谈
SpringMVC 请求全过程漫谈 SpringMVC 跟其他的mvc框架一样,如 struts,webwork, 本质上都是 将一个 http 请求(request)进行各种处理, 然后返回resp ...
- leetcode215
class Solution { public: int findKthLargest(vector<int>& nums, int k) { sort(nums.begin(), ...