解决在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版本 方法: ...
随机推荐
- JSP本质的理解(浏览器调试,response里面的文本都是out.write写入网络流)
一.请求转发到一个Servlet 请求重定向后,url显示的是/abc1234这个url 这个jsp页面就是一个servlet[通过our把文本输入的网络流里面去了]
- 1.1.19 Word中表格自动断开
1.修改前效果如下图所示: 2.先右键点击表格的左上角的“被正方形包着的四方箭头”, 如下图中的序号1,在出现的快捷菜单上点击[表格属性],出现[表格属性]对话框. 3.将参数设置成“允许跨页断行”, ...
- 2017 browser market share
Refer to Net Market Share published data for year 2017, browser share percentage as below table show ...
- Linux和Windows启动后台程序
平时很多时候,我们需要通过脚本命令调用执行程序,集成一体后方便使用快捷.但是启动脚本窗口比较碍眼,能设置为后台运行既方便又美观. Linux启动后台程序 1.后台执行 nohup方法:不挂断的运行命令 ...
- Rest API的简单应用
写在前面 最近一直在搞通过Rest api的方式读取sharepoint文档库中的内容.有些地方需要注意,在此做下记录. 步骤 启动sharepoint服务器的服务 这里可以使用脚本的方式进行启动,脚 ...
- Emscripten 安装和使用
OS: Windows 10 x64 I. install 0. pre install Python2.7 Node js Java 1. down git clone https://github ...
- ScrollView滑动到底部或顶部监听,ScrollView滑动到底部或顶部再继续滑动监听;
ScrollView滑动到底部或顶部后,再继续滑动达到一定距离的监听: ScrollView滑动到底部或顶部的监听: /** * 监听ScrollView滚动到顶部或者底部做相关事件拦截 */ pub ...
- mysql B+tree
什么是索引? 索引是为了加速对表中数据行的检索而创建的一种分散存储的数据结构. id和磁盘地址的映射. 关系型数据库存在磁盘当中. 为什要用索引? 索引能极大减少存储引擎需要扫描的数据量. 索引可以 ...
- 经典技术之URL
SpringBoot入门 (十) 发送邮件 图表算法—最短路径 三个好用的并发工具类 跨应用Session共享: https://www.cnblogs.com/websharing/p/849586 ...
- 实战ELK(7)ElasticSearch常用的基本查询语句
1.term 过滤 term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经切词的文本数据类型): { "term": { "d ...