python2.7练习python编程从入门到实践里边的matplotlib项目,结果一直有问题,求帮助?下面是问题图片

python之matplotlib中plt.show()不显示图的解决办法 - 简书
python之matplotlib中plt.show()不显示图的解决办法
最近,我重新安装了ubuntu,使用virtualenv安装了matplotlib。然后,问题来了。当我运行下列代码时,没有图框跳出来。
import matplotlib.pyplot as plt
plt.show()
plt.bar(left = 0,height = 1)
我使用%pylab查看matplotlib后端,发现居然是agg。兄弟姐妹们,agg是不会画图的!
In [4]:%pylab
Using matplotlib backend: agg
Populating the interactive namespace from numpy and matplotlib
1. 安装Tkinter 和 matplotlib
我的问题在于我原来的python居然没有Tkinter!!
sudo apt-get install tk-dev
pip uninstall -y matplotlib
pip --no-cache-dir install -U matplotlib #这是最关键的
2. 设置agg
其实经过上面的步骤,已经可以画图了补充2种设置agg方法
临时的import matplotlibmatplotlib.use('TkAgg')这个命令必须在第一次使用%pylab 或者import matplotlib.pyplot as plt之前使用常见的agg有:Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
永久的修改matplotlibrc文件matplotlibrc文件的位置在:[~/.virtualenvs/myenv]/lib/python2.7/site-packages/matplotlib/mpl-data/修改位置:backend
至此,matplotlib可以正常工作了。
数据分析师

我要回帖

 

随机推荐