昨天学习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有时能更快解决问题.

最新文章

  1. ASP.NET MVC Application_Error 无效不执行
  2. Kruskal算法
  3. lottery概率问题
  4. 转:sql之left join、right join、inner join的区别
  5. 贝努利概率 matlab
  6. 【 D3.js 高级系列 — 2.0 】 捆图
  7. 【leetcode边做边学】二分查找应用
  8. TabHost 简单用法
  9. IDEA14中安装go语言插件
  10. url中去掉index.php,方便redirect()
  11. Object类toString()
  12. 学习笔记: 特性Attribute详解,应用封装
  13. Fedora的一些个人配置
  14. python 进阶(转自http://python.jobbole.com/82633/)
  15. Oracle启动两个监听
  16. CSS 实例之滚动的图片栏
  17. 【刷题】BZOJ 4176 Lucas的数论
  18. Intel微处理器学习笔记(一) 实模式内存结构
  19. 【题解】洛谷P4158 [SCOI2009] 粉刷匠(DP)
  20. C++反汇编代码分析–函数调用

热门文章

  1. js 判断是否可以打开本地软件
  2. 时钟分组的用法---Clock Groups
  3. 开机自启动 centos 7
  4. Java中的内存泄露的几种可能
  5. Android之ListView动态添加数据(SQLiteOpenHelper类添加数据)
  6. redis5.0.4 集群搭建
  7. 推导式_字典_enumerate
  8. std::set
  9. 表单enctype不对导致action中无法接受数据
  10. win10环境下Android studio安装教程----亲测可用